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 61049 : 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 :
50 : // Requires: Heap::SetUp has been called.
51 : void Initialize(bool create_heap_objects);
52 : void TearDown();
53 :
54 : // Creates a JavaScript Global Context with initial object graph.
55 : // The returned value is a global handle casted to V8Environment*.
56 : Handle<Context> CreateEnvironment(
57 : MaybeHandle<JSGlobalProxy> maybe_global_proxy,
58 : v8::Local<v8::ObjectTemplate> global_object_template,
59 : v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
60 : v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer);
61 :
62 : Handle<JSGlobalProxy> NewRemoteContext(
63 : MaybeHandle<JSGlobalProxy> maybe_global_proxy,
64 : v8::Local<v8::ObjectTemplate> global_object_template);
65 :
66 : // Detach the environment from its outer global object.
67 : void DetachGlobal(Handle<Context> env);
68 :
69 : // Traverses the pointers for memory management.
70 : void Iterate(RootVisitor* v);
71 :
72 : // Accessor for the native scripts source code.
73 : Handle<String> GetNativeSource(NativeType type, int index);
74 :
75 : // Tells whether bootstrapping is active.
76 0 : bool IsActive() const { return nesting_ != 0; }
77 :
78 : // Support for thread preemption.
79 : static int ArchiveSpacePerThread();
80 : char* ArchiveState(char* to);
81 : char* RestoreState(char* from);
82 : void FreeThreadResources();
83 :
84 : // Used for new context creation.
85 : bool InstallExtensions(Handle<Context> native_context,
86 : v8::ExtensionConfiguration* extensions);
87 :
88 : SourceCodeCache* extensions_cache() { return &extensions_cache_; }
89 :
90 : static bool CompileNative(Isolate* isolate, Vector<const char> name,
91 : Handle<String> source, int argc,
92 : Handle<Object> argv[], NativesFlag natives_flag);
93 : static bool CompileExtraBuiltin(Isolate* isolate, int index);
94 : static bool CompileExperimentalExtraBuiltin(Isolate* isolate, int index);
95 :
96 : private:
97 : // Log newly created Map objects if no snapshot was used.
98 : void LogAllMaps();
99 :
100 : Isolate* isolate_;
101 : typedef int NestingCounterType;
102 : NestingCounterType nesting_;
103 : SourceCodeCache extensions_cache_;
104 :
105 : friend class BootstrapperActive;
106 : friend class Isolate;
107 : friend class NativesExternalStringResource;
108 :
109 : explicit Bootstrapper(Isolate* isolate);
110 :
111 : DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
112 : };
113 :
114 : class BootstrapperActive final {
115 : public:
116 : explicit BootstrapperActive(Bootstrapper* bootstrapper)
117 89815 : : bootstrapper_(bootstrapper) {
118 179311 : ++bootstrapper_->nesting_;
119 : }
120 :
121 : ~BootstrapperActive() {
122 89536 : --bootstrapper_->nesting_;
123 : }
124 :
125 : private:
126 : Bootstrapper* bootstrapper_;
127 :
128 : DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
129 : };
130 :
131 : } // namespace internal
132 : } // namespace v8
133 :
134 : #endif // V8_BOOTSTRAPPER_H_
|