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.
5 #ifndef V8_BOOTSTRAPPER_H_
6 #define V8_BOOTSTRAPPER_H_
8 #include "src/factory.h"
13 // A SourceCodeCache uses a FixedArray to store pairs of
14 // (OneByteString*, JSFunction*), mapping names of native code files
15 // (runtime.js, etc.) to precompiled functions. Instead of mapping
16 // names to functions it might make sense to let the JS2C tool
17 // generate an index for each native JS file.
18 class SourceCodeCache FINAL BASE_EMBEDDED {
20 explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
22 void Initialize(Isolate* isolate, bool create_heap_objects) {
23 cache_ = create_heap_objects ? isolate->heap()->empty_fixed_array() : NULL;
26 void Iterate(ObjectVisitor* v) {
27 v->VisitPointer(bit_cast<Object**, FixedArray**>(&cache_));
30 bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) {
31 for (int i = 0; i < cache_->length(); i+=2) {
32 SeqOneByteString* str = SeqOneByteString::cast(cache_->get(i));
33 if (str->IsUtf8EqualTo(name)) {
34 *handle = Handle<SharedFunctionInfo>(
35 SharedFunctionInfo::cast(cache_->get(i + 1)));
42 void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) {
43 Isolate* isolate = shared->GetIsolate();
44 Factory* factory = isolate->factory();
45 HandleScope scope(isolate);
46 int length = cache_->length();
47 Handle<FixedArray> new_array = factory->NewFixedArray(length + 2, TENURED);
48 cache_->CopyTo(0, *new_array, 0, cache_->length());
51 factory->NewStringFromAscii(name, TENURED).ToHandleChecked();
52 DCHECK(!str.is_null());
53 cache_->set(length, *str);
54 cache_->set(length + 1, *shared);
55 Script::cast(shared->script())->set_type(Smi::FromInt(type_));
61 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
65 // The Boostrapper is the public interface for creating a JavaScript global
67 class Bootstrapper FINAL {
69 static void InitializeOncePerProcess();
70 static void TearDownExtensions();
72 // Requires: Heap::SetUp has been called.
73 void Initialize(bool create_heap_objects);
76 // Creates a JavaScript Global Context with initial object graph.
77 // The returned value is a global handle casted to V8Environment*.
78 Handle<Context> CreateEnvironment(
79 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
80 v8::Handle<v8::ObjectTemplate> global_object_template,
81 v8::ExtensionConfiguration* extensions);
83 // Detach the environment from its outer global object.
84 void DetachGlobal(Handle<Context> env);
86 // Traverses the pointers for memory management.
87 void Iterate(ObjectVisitor* v);
89 // Accessor for the native scripts source code.
90 Handle<String> NativesSourceLookup(int index);
92 // Tells whether bootstrapping is active.
93 bool IsActive() const { return nesting_ != 0; }
95 // Support for thread preemption.
96 static int ArchiveSpacePerThread();
97 char* ArchiveState(char* to);
98 char* RestoreState(char* from);
99 void FreeThreadResources();
101 // This will allocate a char array that is deleted when V8 is shut down.
102 // It should only be used for strictly finite allocations.
103 char* AllocateAutoDeletedArray(int bytes);
105 // Used for new context creation.
106 bool InstallExtensions(Handle<Context> native_context,
107 v8::ExtensionConfiguration* extensions);
109 SourceCodeCache* extensions_cache() { return &extensions_cache_; }
113 typedef int NestingCounterType;
114 NestingCounterType nesting_;
115 SourceCodeCache extensions_cache_;
116 // This is for delete, not delete[].
117 List<char*>* delete_these_non_arrays_on_tear_down_;
118 // This is for delete[]
119 List<char*>* delete_these_arrays_on_tear_down_;
121 friend class BootstrapperActive;
122 friend class Isolate;
123 friend class NativesExternalStringResource;
125 explicit Bootstrapper(Isolate* isolate);
127 static v8::Extension* free_buffer_extension_;
128 static v8::Extension* gc_extension_;
129 static v8::Extension* externalize_string_extension_;
130 static v8::Extension* statistics_extension_;
131 static v8::Extension* trigger_failure_extension_;
133 DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
137 class BootstrapperActive FINAL BASE_EMBEDDED {
139 explicit BootstrapperActive(Bootstrapper* bootstrapper)
140 : bootstrapper_(bootstrapper) {
141 ++bootstrapper_->nesting_;
144 ~BootstrapperActive() {
145 --bootstrapper_->nesting_;
149 Bootstrapper* bootstrapper_;
151 DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
155 class NativesExternalStringResource FINAL
156 : public v8::String::ExternalOneByteStringResource {
158 NativesExternalStringResource(Bootstrapper* bootstrapper,
161 virtual const char* data() const OVERRIDE { return data_; }
162 virtual size_t length() const OVERRIDE { return length_; }
169 }} // namespace v8::internal
171 #endif // V8_BOOTSTRAPPER_H_