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/snapshot/natives.h"
12#include "src/visitors.h"
13
14namespace v8 {
15namespace internal {
16
17// A SourceCodeCache uses a FixedArray to store pairs of
18// (OneByteString, SharedFunctionInfo), mapping names of native code files
19// (array.js, etc.) to precompiled functions. Instead of mapping
20// names to functions it might make sense to let the JS2C tool
21// generate an index for each native JS file.
22class SourceCodeCache final {
23 public:
24 explicit SourceCodeCache(Script::Type type) : type_(type) {}
25
26 void Initialize(Isolate* isolate, bool create_heap_objects);
27
28 void Iterate(RootVisitor* v);
29
30 bool Lookup(Isolate* isolate, Vector<const char> name,
31 Handle<SharedFunctionInfo>* handle);
32
33 void Add(Isolate* isolate, Vector<const char> name,
34 Handle<SharedFunctionInfo> shared);
35
36 private:
37 Script::Type type_;
38 FixedArray cache_;
39 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
40};
41
42
43// The Boostrapper is the public interface for creating a JavaScript global
44// context.
45class Bootstrapper final {
46 public:
47 static void InitializeOncePerProcess();
48
49 // Requires: Heap::SetUp has been called.
50 void Initialize(bool create_heap_objects);
51 void TearDown();
52
53 // Creates a JavaScript Global Context with initial object graph.
54 // The returned value is a global handle casted to V8Environment*.
55 Handle<Context> CreateEnvironment(
56 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
57 v8::Local<v8::ObjectTemplate> global_object_template,
58 v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
59 v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer,
60 v8::MicrotaskQueue* microtask_queue);
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 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
114class BootstrapperActive final {
115 public:
116 explicit BootstrapperActive(Bootstrapper* bootstrapper)
117 : bootstrapper_(bootstrapper) {
118 ++bootstrapper_->nesting_;
119 }
120
121 ~BootstrapperActive() {
122 --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_
135