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/heap/factory.h"
9 : #include "src/objects/fixed-array.h"
10 : #include "src/objects/shared-function-info.h"
11 : #include "src/objects/slots.h"
12 : #include "src/snapshot/natives.h"
13 : #include "src/visitors.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 : // A SourceCodeCache uses a FixedArray to store pairs of
19 : // (OneByteString, SharedFunctionInfo), mapping names of native code files
20 : // (array.js, etc.) to precompiled functions. Instead of mapping
21 : // names to functions it might make sense to let the JS2C tool
22 : // generate an index for each native JS file.
23 : class SourceCodeCache final {
24 : public:
25 62883 : explicit SourceCodeCache(Script::Type type) : type_(type) {}
26 :
27 : void Initialize(Isolate* isolate, bool create_heap_objects);
28 :
29 : void Iterate(RootVisitor* v);
30 :
31 : bool Lookup(Isolate* isolate, Vector<const char> name,
32 : Handle<SharedFunctionInfo>* handle);
33 :
34 : void Add(Isolate* isolate, Vector<const char> name,
35 : Handle<SharedFunctionInfo> shared);
36 :
37 : private:
38 : Script::Type type_;
39 : FixedArray cache_;
40 : DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
41 : };
42 :
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 :
63 : Handle<JSGlobalProxy> NewRemoteContext(
64 : MaybeHandle<JSGlobalProxy> maybe_global_proxy,
65 : v8::Local<v8::ObjectTemplate> global_object_template);
66 :
67 : // Detach the environment from its outer global object.
68 : void DetachGlobal(Handle<Context> env);
69 :
70 : // Traverses the pointers for memory management.
71 : void Iterate(RootVisitor* v);
72 :
73 : // Accessor for the native scripts source code.
74 : Handle<String> GetNativeSource(NativeType type, int index);
75 :
76 : // Tells whether bootstrapping is active.
77 0 : bool IsActive() const { return nesting_ != 0; }
78 :
79 : // Support for thread preemption.
80 : static int ArchiveSpacePerThread();
81 : char* ArchiveState(char* to);
82 : char* RestoreState(char* from);
83 : void FreeThreadResources();
84 :
85 : // Used for new context creation.
86 : bool InstallExtensions(Handle<Context> native_context,
87 : v8::ExtensionConfiguration* extensions);
88 :
89 : SourceCodeCache* extensions_cache() { return &extensions_cache_; }
90 :
91 : static bool CompileNative(Isolate* isolate, Vector<const char> name,
92 : Handle<String> source, int argc,
93 : Handle<Object> argv[], NativesFlag natives_flag);
94 : static bool CompileExtraBuiltin(Isolate* isolate, int index);
95 : static bool CompileExperimentalExtraBuiltin(Isolate* isolate, int index);
96 :
97 : private:
98 : // Log newly created Map objects if no snapshot was used.
99 : void LogAllMaps();
100 :
101 : Isolate* isolate_;
102 : typedef int NestingCounterType;
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 : static v8::Extension* free_buffer_extension_;
113 : static v8::Extension* gc_extension_;
114 : static v8::Extension* externalize_string_extension_;
115 : static v8::Extension* statistics_extension_;
116 : static v8::Extension* trigger_failure_extension_;
117 : static v8::Extension* ignition_statistics_extension_;
118 :
119 : DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
120 : };
121 :
122 : class BootstrapperActive final {
123 : public:
124 : explicit BootstrapperActive(Bootstrapper* bootstrapper)
125 91909 : : bootstrapper_(bootstrapper) {
126 183503 : ++bootstrapper_->nesting_;
127 : }
128 :
129 : ~BootstrapperActive() {
130 91635 : --bootstrapper_->nesting_;
131 : }
132 :
133 : private:
134 : Bootstrapper* bootstrapper_;
135 :
136 : DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
137 : };
138 :
139 : } // namespace internal
140 : } // namespace v8
141 :
142 : #endif // V8_BOOTSTRAPPER_H_
|