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