blob: 2e05452a8c9e8b6e4710a4b0dcac0ed720dcba06 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29#ifndef V8_BOOTSTRAPPER_H_
30#define V8_BOOTSTRAPPER_H_
31
Ben Murdoch257744e2011-11-30 15:57:28 +000032#include "allocation.h"
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
Andrei Popescu31002712010-02-23 13:46:05 +000037
Steve Block44f0eee2011-05-26 01:26:41 +010038// A SourceCodeCache uses a FixedArray to store pairs of
39// (AsciiString*, JSFunction*), mapping names of native code files
40// (runtime.js, etc.) to precompiled functions. Instead of mapping
41// names to functions it might make sense to let the JS2C tool
42// generate an index for each native JS file.
43class SourceCodeCache BASE_EMBEDDED {
Andrei Popescu31002712010-02-23 13:46:05 +000044 public:
Steve Block44f0eee2011-05-26 01:26:41 +010045 explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
Andrei Popescu31002712010-02-23 13:46:05 +000046
Steve Block44f0eee2011-05-26 01:26:41 +010047 void Initialize(bool create_heap_objects) {
48 cache_ = create_heap_objects ? HEAP->empty_fixed_array() : NULL;
49 }
50
51 void Iterate(ObjectVisitor* v) {
52 v->VisitPointer(BitCast<Object**, FixedArray**>(&cache_));
53 }
54
55 bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) {
56 for (int i = 0; i < cache_->length(); i+=2) {
57 SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i));
58 if (str->IsEqualTo(name)) {
59 *handle = Handle<SharedFunctionInfo>(
60 SharedFunctionInfo::cast(cache_->get(i + 1)));
61 return true;
62 }
63 }
64 return false;
65 }
66
67 void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) {
68 HandleScope scope;
69 int length = cache_->length();
70 Handle<FixedArray> new_array =
71 FACTORY->NewFixedArray(length + 2, TENURED);
72 cache_->CopyTo(0, *new_array, 0, cache_->length());
73 cache_ = *new_array;
74 Handle<String> str = FACTORY->NewStringFromAscii(name, TENURED);
75 cache_->set(length, *str);
76 cache_->set(length + 1, *shared);
77 Script::cast(shared->script())->set_type(Smi::FromInt(type_));
78 }
Andrei Popescu31002712010-02-23 13:46:05 +000079
80 private:
Steve Block44f0eee2011-05-26 01:26:41 +010081 Script::Type type_;
82 FixedArray* cache_;
83 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
Andrei Popescu31002712010-02-23 13:46:05 +000084};
85
86
Steve Blocka7e24c12009-10-30 11:49:00 +000087// The Boostrapper is the public interface for creating a JavaScript global
88// context.
Steve Block44f0eee2011-05-26 01:26:41 +010089class Bootstrapper {
Steve Blocka7e24c12009-10-30 11:49:00 +000090 public:
91 // Requires: Heap::Setup has been called.
Steve Block44f0eee2011-05-26 01:26:41 +010092 void Initialize(bool create_heap_objects);
93 void TearDown();
Steve Blocka7e24c12009-10-30 11:49:00 +000094
95 // Creates a JavaScript Global Context with initial object graph.
96 // The returned value is a global handle casted to V8Environment*.
Steve Block44f0eee2011-05-26 01:26:41 +010097 Handle<Context> CreateEnvironment(
Ben Murdoch257744e2011-11-30 15:57:28 +000098 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +000099 Handle<Object> global_object,
100 v8::Handle<v8::ObjectTemplate> global_template,
101 v8::ExtensionConfiguration* extensions);
102
103 // Detach the environment from its outer global object.
Steve Block44f0eee2011-05-26 01:26:41 +0100104 void DetachGlobal(Handle<Context> env);
Steve Blocka7e24c12009-10-30 11:49:00 +0000105
Andrei Popescu74b3c142010-03-29 12:03:09 +0100106 // Reattach an outer global object to an environment.
Steve Block44f0eee2011-05-26 01:26:41 +0100107 void ReattachGlobal(Handle<Context> env, Handle<Object> global_object);
Andrei Popescu74b3c142010-03-29 12:03:09 +0100108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109 // Traverses the pointers for memory management.
Steve Block44f0eee2011-05-26 01:26:41 +0100110 void Iterate(ObjectVisitor* v);
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
Steve Block6ded16b2010-05-10 14:33:55 +0100112 // Accessor for the native scripts source code.
Steve Block44f0eee2011-05-26 01:26:41 +0100113 Handle<String> NativesSourceLookup(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000114
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 // Tells whether bootstrapping is active.
Steve Block44f0eee2011-05-26 01:26:41 +0100116 bool IsActive() const { return nesting_ != 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 // Support for thread preemption.
Ben Murdoch257744e2011-11-30 15:57:28 +0000119 static int ArchiveSpacePerThread();
Steve Block44f0eee2011-05-26 01:26:41 +0100120 char* ArchiveState(char* to);
121 char* RestoreState(char* from);
122 void FreeThreadResources();
Leon Clarkee46be812010-01-19 14:06:41 +0000123
124 // This will allocate a char array that is deleted when V8 is shut down.
125 // It should only be used for strictly finite allocations.
Steve Block44f0eee2011-05-26 01:26:41 +0100126 char* AllocateAutoDeletedArray(int bytes);
Andrei Popescu31002712010-02-23 13:46:05 +0000127
128 // Used for new context creation.
Steve Block44f0eee2011-05-26 01:26:41 +0100129 bool InstallExtensions(Handle<Context> global_context,
130 v8::ExtensionConfiguration* extensions);
131
132 SourceCodeCache* extensions_cache() { return &extensions_cache_; }
133
134 private:
135 typedef int NestingCounterType;
136 NestingCounterType nesting_;
137 SourceCodeCache extensions_cache_;
138 // This is for delete, not delete[].
139 List<char*>* delete_these_non_arrays_on_tear_down_;
140 // This is for delete[]
141 List<char*>* delete_these_arrays_on_tear_down_;
142
143 friend class BootstrapperActive;
144 friend class Isolate;
145 friend class NativesExternalStringResource;
146
147 Bootstrapper();
148
149 DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
150};
151
152
153class BootstrapperActive BASE_EMBEDDED {
154 public:
155 BootstrapperActive() {
156 ++Isolate::Current()->bootstrapper()->nesting_;
157 }
158
159 ~BootstrapperActive() {
160 --Isolate::Current()->bootstrapper()->nesting_;
161 }
162
163 private:
164 DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165};
166
Steve Blockd0582a62009-12-15 09:54:21 +0000167
168class NativesExternalStringResource
169 : public v8::String::ExternalAsciiStringResource {
170 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100171 explicit NativesExternalStringResource(Bootstrapper* bootstrapper,
172 const char* source);
Steve Blockd0582a62009-12-15 09:54:21 +0000173
174 const char* data() const {
175 return data_;
176 }
177
178 size_t length() const {
179 return length_;
180 }
181 private:
182 const char* data_;
183 size_t length_;
184};
185
Steve Blocka7e24c12009-10-30 11:49:00 +0000186}} // namespace v8::internal
187
188#endif // V8_BOOTSTRAPPER_H_