blob: 66a3d8ad027a1631ff9c7a28f11f23c6e930bad8 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_BOOTSTRAPPER_H_
6#define V8_BOOTSTRAPPER_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/factory.h"
Ben Murdoch257744e2011-11-30 15:57:28 +00009
Steve Blocka7e24c12009-10-30 11:49:00 +000010namespace v8 {
11namespace internal {
12
Steve Block44f0eee2011-05-26 01:26:41 +010013// A SourceCodeCache uses a FixedArray to store pairs of
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014// (OneByteString*, JSFunction*), mapping names of native code files
Steve Block44f0eee2011-05-26 01:26:41 +010015// (runtime.js, etc.) to precompiled functions. Instead of mapping
16// names to functions it might make sense to let the JS2C tool
17// generate an index for each native JS file.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018class SourceCodeCache final BASE_EMBEDDED {
Andrei Popescu31002712010-02-23 13:46:05 +000019 public:
Steve Block44f0eee2011-05-26 01:26:41 +010020 explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
Andrei Popescu31002712010-02-23 13:46:05 +000021
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 void Initialize(Isolate* isolate, bool create_heap_objects) {
23 cache_ = create_heap_objects ? isolate->heap()->empty_fixed_array() : NULL;
Steve Block44f0eee2011-05-26 01:26:41 +010024 }
25
26 void Iterate(ObjectVisitor* v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027 v->VisitPointer(bit_cast<Object**, FixedArray**>(&cache_));
Steve Block44f0eee2011-05-26 01:26:41 +010028 }
29
30 bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) {
31 for (int i = 0; i < cache_->length(); i+=2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 SeqOneByteString* str = SeqOneByteString::cast(cache_->get(i));
33 if (str->IsUtf8EqualTo(name)) {
Steve Block44f0eee2011-05-26 01:26:41 +010034 *handle = Handle<SharedFunctionInfo>(
35 SharedFunctionInfo::cast(cache_->get(i + 1)));
36 return true;
37 }
38 }
39 return false;
40 }
41
42 void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 Isolate* isolate = shared->GetIsolate();
44 Factory* factory = isolate->factory();
45 HandleScope scope(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +010046 int length = cache_->length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 Handle<FixedArray> new_array = factory->NewFixedArray(length + 2, TENURED);
Steve Block44f0eee2011-05-26 01:26:41 +010048 cache_->CopyTo(0, *new_array, 0, cache_->length());
49 cache_ = *new_array;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 Handle<String> str =
51 factory->NewStringFromAscii(name, TENURED).ToHandleChecked();
52 DCHECK(!str.is_null());
Steve Block44f0eee2011-05-26 01:26:41 +010053 cache_->set(length, *str);
54 cache_->set(length + 1, *shared);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055 Script::cast(shared->script())->set_type(type_);
Steve Block44f0eee2011-05-26 01:26:41 +010056 }
Andrei Popescu31002712010-02-23 13:46:05 +000057
58 private:
Steve Block44f0eee2011-05-26 01:26:41 +010059 Script::Type type_;
60 FixedArray* cache_;
61 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
Andrei Popescu31002712010-02-23 13:46:05 +000062};
63
Ben Murdoch61f157c2016-09-16 13:49:30 +010064enum GlobalContextType { FULL_CONTEXT, DEBUG_CONTEXT };
Andrei Popescu31002712010-02-23 13:46:05 +000065
Steve Blocka7e24c12009-10-30 11:49:00 +000066// The Boostrapper is the public interface for creating a JavaScript global
67// context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068class Bootstrapper final {
Steve Blocka7e24c12009-10-30 11:49:00 +000069 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 static void InitializeOncePerProcess();
71 static void TearDownExtensions();
72
Ben Murdoch3ef787d2012-04-12 10:51:47 +010073 // Requires: Heap::SetUp has been called.
Steve Block44f0eee2011-05-26 01:26:41 +010074 void Initialize(bool create_heap_objects);
75 void TearDown();
Steve Blocka7e24c12009-10-30 11:49:00 +000076
77 // Creates a JavaScript Global Context with initial object graph.
78 // The returned value is a global handle casted to V8Environment*.
Steve Block44f0eee2011-05-26 01:26:41 +010079 Handle<Context> CreateEnvironment(
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 v8::Local<v8::ObjectTemplate> global_object_template,
Ben Murdoch61f157c2016-09-16 13:49:30 +010082 v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
Ben Murdoch097c5b22016-05-18 11:27:45 +010083 GlobalContextType context_type = FULL_CONTEXT);
Steve Blocka7e24c12009-10-30 11:49:00 +000084
85 // Detach the environment from its outer global object.
Steve Block44f0eee2011-05-26 01:26:41 +010086 void DetachGlobal(Handle<Context> env);
Steve Blocka7e24c12009-10-30 11:49:00 +000087
88 // Traverses the pointers for memory management.
Steve Block44f0eee2011-05-26 01:26:41 +010089 void Iterate(ObjectVisitor* v);
Steve Blocka7e24c12009-10-30 11:49:00 +000090
Steve Block6ded16b2010-05-10 14:33:55 +010091 // Accessor for the native scripts source code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 template <class Source>
93 Handle<String> SourceLookup(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +000094
Steve Blocka7e24c12009-10-30 11:49:00 +000095 // Tells whether bootstrapping is active.
Steve Block44f0eee2011-05-26 01:26:41 +010096 bool IsActive() const { return nesting_ != 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +000097
Steve Blocka7e24c12009-10-30 11:49:00 +000098 // Support for thread preemption.
Ben Murdoch257744e2011-11-30 15:57:28 +000099 static int ArchiveSpacePerThread();
Steve Block44f0eee2011-05-26 01:26:41 +0100100 char* ArchiveState(char* to);
101 char* RestoreState(char* from);
102 void FreeThreadResources();
Leon Clarkee46be812010-01-19 14:06:41 +0000103
Andrei Popescu31002712010-02-23 13:46:05 +0000104 // Used for new context creation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 bool InstallExtensions(Handle<Context> native_context,
Steve Block44f0eee2011-05-26 01:26:41 +0100106 v8::ExtensionConfiguration* extensions);
107
108 SourceCodeCache* extensions_cache() { return &extensions_cache_; }
109
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000110 static bool CompileNative(Isolate* isolate, Vector<const char> name,
111 Handle<String> source, int argc,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100112 Handle<Object> argv[], NativesFlag natives_flag);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 static bool CompileBuiltin(Isolate* isolate, int index);
114 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
115 static bool CompileExtraBuiltin(Isolate* isolate, int index);
116 static bool CompileExperimentalExtraBuiltin(Isolate* isolate, int index);
117
118 static void ExportFromRuntime(Isolate* isolate, Handle<JSObject> container);
119 static void ExportExperimentalFromRuntime(Isolate* isolate,
120 Handle<JSObject> container);
121
Steve Block44f0eee2011-05-26 01:26:41 +0100122 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +0100124 typedef int NestingCounterType;
125 NestingCounterType nesting_;
126 SourceCodeCache extensions_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100127
128 friend class BootstrapperActive;
129 friend class Isolate;
130 friend class NativesExternalStringResource;
131
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 explicit Bootstrapper(Isolate* isolate);
133
134 static v8::Extension* free_buffer_extension_;
135 static v8::Extension* gc_extension_;
136 static v8::Extension* externalize_string_extension_;
137 static v8::Extension* statistics_extension_;
138 static v8::Extension* trigger_failure_extension_;
Ben Murdochc5610432016-08-08 18:44:38 +0100139 static v8::Extension* ignition_statistics_extension_;
Steve Block44f0eee2011-05-26 01:26:41 +0100140
141 DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
142};
143
144
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145class BootstrapperActive final BASE_EMBEDDED {
Steve Block44f0eee2011-05-26 01:26:41 +0100146 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147 explicit BootstrapperActive(Bootstrapper* bootstrapper)
148 : bootstrapper_(bootstrapper) {
149 ++bootstrapper_->nesting_;
Steve Block44f0eee2011-05-26 01:26:41 +0100150 }
151
152 ~BootstrapperActive() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153 --bootstrapper_->nesting_;
Steve Block44f0eee2011-05-26 01:26:41 +0100154 }
155
156 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 Bootstrapper* bootstrapper_;
158
Steve Block44f0eee2011-05-26 01:26:41 +0100159 DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
Steve Blocka7e24c12009-10-30 11:49:00 +0000160};
161
Steve Blockd0582a62009-12-15 09:54:21 +0000162
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163class NativesExternalStringResource final
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 : public v8::String::ExternalOneByteStringResource {
Steve Blockd0582a62009-12-15 09:54:21 +0000165 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166 NativesExternalStringResource(const char* source, size_t length)
167 : data_(source), length_(length) {}
168 const char* data() const override { return data_; }
169 size_t length() const override { return length_; }
Steve Blockd0582a62009-12-15 09:54:21 +0000170
Steve Blockd0582a62009-12-15 09:54:21 +0000171 private:
172 const char* data_;
173 size_t length_;
174};
175
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176} // namespace internal
177} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
179#endif // V8_BOOTSTRAPPER_H_