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