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