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_INIT_BOOTSTRAPPER_H_ 6 #define V8_INIT_BOOTSTRAPPER_H_ 7 8 #include "include/v8-context.h" 9 #include "include/v8-local-handle.h" 10 #include "include/v8-snapshot.h" 11 #include "src/heap/factory.h" 12 #include "src/objects/fixed-array.h" 13 #include "src/objects/shared-function-info.h" 14 #include "src/objects/visitors.h" 15 16 namespace v8 { 17 namespace internal { 18 19 // A SourceCodeCache uses a FixedArray to store pairs of (OneByteString, 20 // SharedFunctionInfo), mapping names of native extensions code files to 21 // precompiled functions. 22 class SourceCodeCache final { 23 public: SourceCodeCache(Script::Type type)24 explicit SourceCodeCache(Script::Type type) : type_(type) {} 25 SourceCodeCache(const SourceCodeCache&) = delete; 26 SourceCodeCache& operator=(const SourceCodeCache&) = delete; 27 28 void Initialize(Isolate* isolate, bool create_heap_objects); 29 30 void Iterate(RootVisitor* v); 31 32 bool Lookup(Isolate* isolate, base::Vector<const char> name, 33 Handle<SharedFunctionInfo>* handle); 34 35 void Add(Isolate* isolate, base::Vector<const char> name, 36 Handle<SharedFunctionInfo> shared); 37 38 private: 39 Script::Type type_; 40 FixedArray cache_; 41 }; 42 43 // The Boostrapper is the public interface for creating a JavaScript global 44 // context. 45 class Bootstrapper final { 46 public: 47 Bootstrapper(const Bootstrapper&) = delete; 48 Bootstrapper& operator=(const Bootstrapper&) = delete; 49 50 static void InitializeOncePerProcess(); 51 52 // Requires: Heap::SetUp has been called. 53 void Initialize(bool create_heap_objects); 54 void TearDown(); 55 56 // Creates a JavaScript Global Context with initial object graph. 57 // The returned value is a global handle casted to V8Environment*. 58 Handle<Context> CreateEnvironment( 59 MaybeHandle<JSGlobalProxy> maybe_global_proxy, 60 v8::Local<v8::ObjectTemplate> global_object_template, 61 v8::ExtensionConfiguration* extensions, size_t context_snapshot_index, 62 v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer, 63 v8::MicrotaskQueue* microtask_queue); 64 65 // Used for testing context deserialization. No code runs in the generated 66 // context. It only needs to pass heap verification. CreateEnvironmentForTesting()67 Handle<Context> CreateEnvironmentForTesting() { 68 MaybeHandle<JSGlobalProxy> no_global_proxy; 69 v8::Local<v8::ObjectTemplate> no_global_object_template; 70 ExtensionConfiguration no_extensions; 71 static constexpr int kDefaultContextIndex = 0; 72 v8::DeserializeEmbedderFieldsCallback no_callback; 73 v8::MicrotaskQueue* no_microtask_queue = nullptr; 74 return CreateEnvironment(no_global_proxy, no_global_object_template, 75 &no_extensions, kDefaultContextIndex, no_callback, 76 no_microtask_queue); 77 } 78 79 Handle<JSGlobalProxy> NewRemoteContext( 80 MaybeHandle<JSGlobalProxy> maybe_global_proxy, 81 v8::Local<v8::ObjectTemplate> global_object_template); 82 83 // Traverses the pointers for memory management. 84 void Iterate(RootVisitor* v); 85 86 // Tells whether bootstrapping is active. IsActive()87 bool IsActive() const { return nesting_ != 0; } 88 89 // Support for thread preemption. 90 static int ArchiveSpacePerThread(); 91 char* ArchiveState(char* to); 92 char* RestoreState(char* from); 93 void FreeThreadResources(); 94 95 // Used for new context creation. 96 bool InstallExtensions(Handle<Context> native_context, 97 v8::ExtensionConfiguration* extensions); 98 extensions_cache()99 SourceCodeCache* extensions_cache() { return &extensions_cache_; } 100 101 private: 102 // Log newly created Map objects if no snapshot was used. 103 void LogAllMaps(); 104 105 Isolate* isolate_; 106 using NestingCounterType = int; 107 NestingCounterType nesting_; 108 SourceCodeCache extensions_cache_; 109 110 friend class BootstrapperActive; 111 friend class Isolate; 112 friend class NativesExternalStringResource; 113 114 explicit Bootstrapper(Isolate* isolate); 115 }; 116 117 class BootstrapperActive final { 118 public: BootstrapperActive(Bootstrapper * bootstrapper)119 explicit BootstrapperActive(Bootstrapper* bootstrapper) 120 : bootstrapper_(bootstrapper) { 121 ++bootstrapper_->nesting_; 122 } 123 BootstrapperActive(const BootstrapperActive&) = delete; 124 BootstrapperActive& operator=(const BootstrapperActive&) = delete; 125 ~BootstrapperActive()126 ~BootstrapperActive() { --bootstrapper_->nesting_; } 127 128 private: 129 Bootstrapper* bootstrapper_; 130 }; 131 132 } // namespace internal 133 } // namespace v8 134 135 #endif // V8_INIT_BOOTSTRAPPER_H_ 136