Line data Source code
1 : // 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.
4 :
5 : #ifndef V8_BOOTSTRAPPER_H_
6 : #define V8_BOOTSTRAPPER_H_
7 :
8 : #include "src/factory.h"
9 : #include "src/snapshot/natives.h"
10 : #include "src/visitors.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : // A SourceCodeCache uses a FixedArray to store pairs of
16 : // (OneByteString*, JSFunction*), mapping names of native code files
17 : // (array.js, etc.) to precompiled functions. Instead of mapping
18 : // names to functions it might make sense to let the JS2C tool
19 : // generate an index for each native JS file.
20 : class SourceCodeCache final BASE_EMBEDDED {
21 : public:
22 60782 : explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
23 :
24 : void Initialize(Isolate* isolate, bool create_heap_objects) {
25 120067 : cache_ = create_heap_objects ? isolate->heap()->empty_fixed_array() : NULL;
26 : }
27 :
28 : void Iterate(RootVisitor* v) {
29 : v->VisitRootPointer(Root::kExtensions,
30 241585 : bit_cast<Object**, FixedArray**>(&cache_));
31 : }
32 :
33 6436 : bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) {
34 26942 : for (int i = 0; i < cache_->length(); i+=2) {
35 : SeqOneByteString* str = SeqOneByteString::cast(cache_->get(i));
36 8730 : if (str->IsUtf8EqualTo(name)) {
37 : *handle = Handle<SharedFunctionInfo>(
38 3392 : SharedFunctionInfo::cast(cache_->get(i + 1)));
39 1696 : return true;
40 : }
41 : }
42 : return false;
43 : }
44 :
45 9403 : void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) {
46 : Isolate* isolate = shared->GetIsolate();
47 : Factory* factory = isolate->factory();
48 : HandleScope scope(isolate);
49 4697 : int length = cache_->length();
50 4697 : Handle<FixedArray> new_array = factory->NewFixedArray(length + 2, TENURED);
51 9412 : cache_->CopyTo(0, *new_array, 0, cache_->length());
52 4706 : cache_ = *new_array;
53 : Handle<String> str =
54 : factory
55 : ->NewStringFromOneByte(Vector<const uint8_t>::cast(name), TENURED)
56 9408 : .ToHandleChecked();
57 : DCHECK(!str.is_null());
58 4702 : cache_->set(length, *str);
59 4700 : cache_->set(length + 1, *shared);
60 4703 : Script::cast(shared->script())->set_type(type_);
61 4689 : }
62 :
63 : private:
64 : Script::Type type_;
65 : FixedArray* cache_;
66 : DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
67 : };
68 :
69 : enum GlobalContextType { FULL_CONTEXT, DEBUG_CONTEXT };
70 :
71 : // The Boostrapper is the public interface for creating a JavaScript global
72 : // context.
73 : class Bootstrapper final {
74 : public:
75 : static void InitializeOncePerProcess();
76 : static void TearDownExtensions();
77 :
78 : // Requires: Heap::SetUp has been called.
79 : void Initialize(bool create_heap_objects);
80 : void TearDown();
81 :
82 : // Creates a JavaScript Global Context with initial object graph.
83 : // The returned value is a global handle casted to V8Environment*.
84 : Handle<Context> CreateEnvironment(
85 : MaybeHandle<JSGlobalProxy> maybe_global_proxy,
86 : v8::Local<v8::ObjectTemplate> global_object_template,
87 : v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
88 : v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer,
89 : GlobalContextType context_type = FULL_CONTEXT);
90 :
91 : Handle<JSGlobalProxy> NewRemoteContext(
92 : MaybeHandle<JSGlobalProxy> maybe_global_proxy,
93 : v8::Local<v8::ObjectTemplate> global_object_template);
94 :
95 : // Detach the environment from its outer global object.
96 : void DetachGlobal(Handle<Context> env);
97 :
98 : // Traverses the pointers for memory management.
99 : void Iterate(RootVisitor* v);
100 :
101 : // Accessor for the native scripts source code.
102 : Handle<String> GetNativeSource(NativeType type, int index);
103 :
104 : // Tells whether bootstrapping is active.
105 26773 : bool IsActive() const { return nesting_ != 0; }
106 :
107 : // Support for thread preemption.
108 : static int ArchiveSpacePerThread();
109 : char* ArchiveState(char* to);
110 : char* RestoreState(char* from);
111 : void FreeThreadResources();
112 :
113 : // Used for new context creation.
114 : bool InstallExtensions(Handle<Context> native_context,
115 : v8::ExtensionConfiguration* extensions);
116 :
117 : SourceCodeCache* extensions_cache() { return &extensions_cache_; }
118 :
119 : static bool CompileNative(Isolate* isolate, Vector<const char> name,
120 : Handle<String> source, int argc,
121 : Handle<Object> argv[], NativesFlag natives_flag);
122 : static bool CompileBuiltin(Isolate* isolate, int index);
123 : static bool CompileExtraBuiltin(Isolate* isolate, int index);
124 : static bool CompileExperimentalExtraBuiltin(Isolate* isolate, int index);
125 :
126 : static void ExportFromRuntime(Isolate* isolate, Handle<JSObject> container);
127 :
128 : private:
129 : Isolate* isolate_;
130 : typedef int NestingCounterType;
131 : NestingCounterType nesting_;
132 : SourceCodeCache extensions_cache_;
133 :
134 : friend class BootstrapperActive;
135 : friend class Isolate;
136 : friend class NativesExternalStringResource;
137 :
138 : explicit Bootstrapper(Isolate* isolate);
139 :
140 : static v8::Extension* free_buffer_extension_;
141 : static v8::Extension* gc_extension_;
142 : static v8::Extension* externalize_string_extension_;
143 : static v8::Extension* statistics_extension_;
144 : static v8::Extension* trigger_failure_extension_;
145 : static v8::Extension* ignition_statistics_extension_;
146 :
147 : DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
148 : };
149 :
150 :
151 : class BootstrapperActive final BASE_EMBEDDED {
152 : public:
153 : explicit BootstrapperActive(Bootstrapper* bootstrapper)
154 107272 : : bootstrapper_(bootstrapper) {
155 212890 : ++bootstrapper_->nesting_;
156 : }
157 :
158 : ~BootstrapperActive() {
159 212889 : --bootstrapper_->nesting_;
160 : }
161 :
162 : private:
163 : Bootstrapper* bootstrapper_;
164 :
165 : DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
166 : };
167 :
168 : } // namespace internal
169 : } // namespace v8
170 :
171 : #endif // V8_BOOTSTRAPPER_H_
|