blob: 8cd29b218aa9473f7468038c9ac7cbdbf48cf385 [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#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "bootstrapper.h"
33#include "compiler.h"
34#include "debug.h"
35#include "execution.h"
36#include "global-handles.h"
37#include "macro-assembler.h"
38#include "natives.h"
Iain Merrick75681382010-08-19 15:07:18 +010039#include "objects-visiting.h"
Steve Blockd0582a62009-12-15 09:54:21 +000040#include "snapshot.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080041#include "extensions/externalize-string-extension.h"
42#include "extensions/gc-extension.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
47// A SourceCodeCache uses a FixedArray to store pairs of
48// (AsciiString*, JSFunction*), mapping names of native code files
49// (runtime.js, etc.) to precompiled functions. Instead of mapping
50// names to functions it might make sense to let the JS2C tool
51// generate an index for each native JS file.
52class SourceCodeCache BASE_EMBEDDED {
53 public:
54 explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
55
56 void Initialize(bool create_heap_objects) {
57 cache_ = create_heap_objects ? Heap::empty_fixed_array() : NULL;
58 }
59
60 void Iterate(ObjectVisitor* v) {
Iain Merrick75681382010-08-19 15:07:18 +010061 v->VisitPointer(BitCast<Object**>(&cache_));
Steve Blocka7e24c12009-10-30 11:49:00 +000062 }
63
64
Steve Block6ded16b2010-05-10 14:33:55 +010065 bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) {
Steve Blocka7e24c12009-10-30 11:49:00 +000066 for (int i = 0; i < cache_->length(); i+=2) {
67 SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i));
68 if (str->IsEqualTo(name)) {
Steve Block6ded16b2010-05-10 14:33:55 +010069 *handle = Handle<SharedFunctionInfo>(
70 SharedFunctionInfo::cast(cache_->get(i + 1)));
Steve Blocka7e24c12009-10-30 11:49:00 +000071 return true;
72 }
73 }
74 return false;
75 }
76
77
Steve Block6ded16b2010-05-10 14:33:55 +010078 void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) {
Steve Blocka7e24c12009-10-30 11:49:00 +000079 HandleScope scope;
80 int length = cache_->length();
81 Handle<FixedArray> new_array =
82 Factory::NewFixedArray(length + 2, TENURED);
83 cache_->CopyTo(0, *new_array, 0, cache_->length());
84 cache_ = *new_array;
85 Handle<String> str = Factory::NewStringFromAscii(name, TENURED);
86 cache_->set(length, *str);
Steve Block6ded16b2010-05-10 14:33:55 +010087 cache_->set(length + 1, *shared);
88 Script::cast(shared->script())->set_type(Smi::FromInt(type_));
Steve Blocka7e24c12009-10-30 11:49:00 +000089 }
90
91 private:
92 Script::Type type_;
93 FixedArray* cache_;
94 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
95};
96
Steve Blocka7e24c12009-10-30 11:49:00 +000097static SourceCodeCache extensions_cache(Script::TYPE_EXTENSION);
Steve Blockd0582a62009-12-15 09:54:21 +000098// This is for delete, not delete[].
99static List<char*>* delete_these_non_arrays_on_tear_down = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000100// This is for delete[]
101static List<char*>* delete_these_arrays_on_tear_down = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000102
103
104NativesExternalStringResource::NativesExternalStringResource(const char* source)
105 : data_(source), length_(StrLength(source)) {
106 if (delete_these_non_arrays_on_tear_down == NULL) {
107 delete_these_non_arrays_on_tear_down = new List<char*>(2);
108 }
109 // The resources are small objects and we only make a fixed number of
110 // them, but let's clean them up on exit for neatness.
111 delete_these_non_arrays_on_tear_down->
112 Add(reinterpret_cast<char*>(this));
113}
Steve Blocka7e24c12009-10-30 11:49:00 +0000114
115
116Handle<String> Bootstrapper::NativesSourceLookup(int index) {
117 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
118 if (Heap::natives_source_cache()->get(index)->IsUndefined()) {
Steve Blockd0582a62009-12-15 09:54:21 +0000119 if (!Snapshot::IsEnabled() || FLAG_new_snapshot) {
120 // We can use external strings for the natives.
121 NativesExternalStringResource* resource =
122 new NativesExternalStringResource(
123 Natives::GetScriptSource(index).start());
124 Handle<String> source_code =
125 Factory::NewExternalStringFromAscii(resource);
126 Heap::natives_source_cache()->set(index, *source_code);
127 } else {
128 // Old snapshot code can't cope with external strings at all.
129 Handle<String> source_code =
130 Factory::NewStringFromAscii(Natives::GetScriptSource(index));
131 Heap::natives_source_cache()->set(index, *source_code);
132 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 }
134 Handle<Object> cached_source(Heap::natives_source_cache()->get(index));
135 return Handle<String>::cast(cached_source);
136}
137
138
Steve Blocka7e24c12009-10-30 11:49:00 +0000139void Bootstrapper::Initialize(bool create_heap_objects) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 extensions_cache.Initialize(create_heap_objects);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800141 GCExtension::Register();
142 ExternalizeStringExtension::Register();
Steve Blocka7e24c12009-10-30 11:49:00 +0000143}
144
145
Leon Clarkee46be812010-01-19 14:06:41 +0000146char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
147 char* memory = new char[bytes];
148 if (memory != NULL) {
149 if (delete_these_arrays_on_tear_down == NULL) {
150 delete_these_arrays_on_tear_down = new List<char*>(2);
151 }
152 delete_these_arrays_on_tear_down->Add(memory);
153 }
154 return memory;
155}
156
157
Steve Blocka7e24c12009-10-30 11:49:00 +0000158void Bootstrapper::TearDown() {
Steve Blockd0582a62009-12-15 09:54:21 +0000159 if (delete_these_non_arrays_on_tear_down != NULL) {
160 int len = delete_these_non_arrays_on_tear_down->length();
161 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
162 for (int i = 0; i < len; i++) {
163 delete delete_these_non_arrays_on_tear_down->at(i);
Leon Clarkee46be812010-01-19 14:06:41 +0000164 delete_these_non_arrays_on_tear_down->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000165 }
166 delete delete_these_non_arrays_on_tear_down;
167 delete_these_non_arrays_on_tear_down = NULL;
168 }
169
Leon Clarkee46be812010-01-19 14:06:41 +0000170 if (delete_these_arrays_on_tear_down != NULL) {
171 int len = delete_these_arrays_on_tear_down->length();
172 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
173 for (int i = 0; i < len; i++) {
174 delete[] delete_these_arrays_on_tear_down->at(i);
175 delete_these_arrays_on_tear_down->at(i) = NULL;
176 }
177 delete delete_these_arrays_on_tear_down;
178 delete_these_arrays_on_tear_down = NULL;
179 }
180
Andrei Popescu31002712010-02-23 13:46:05 +0000181 extensions_cache.Initialize(false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000182}
183
184
Steve Blocka7e24c12009-10-30 11:49:00 +0000185class Genesis BASE_EMBEDDED {
186 public:
187 Genesis(Handle<Object> global_object,
188 v8::Handle<v8::ObjectTemplate> global_template,
189 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000190 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000191
192 Handle<Context> result() { return result_; }
193
194 Genesis* previous() { return previous_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000195
196 private:
197 Handle<Context> global_context_;
198
199 // There may be more than one active genesis object: When GC is
200 // triggered during environment creation there may be weak handle
201 // processing callbacks which may create new environments.
202 Genesis* previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000203
204 Handle<Context> global_context() { return global_context_; }
205
Andrei Popescu31002712010-02-23 13:46:05 +0000206 // Creates some basic objects. Used for creating a context from scratch.
207 void CreateRoots();
208 // Creates the empty function. Used for creating a context from scratch.
209 Handle<JSFunction> CreateEmptyFunction();
210 // Creates the global objects using the global and the template passed in
211 // through the API. We call this regardless of whether we are building a
212 // context from scratch or using a deserialized one from the partial snapshot
213 // but in the latter case we don't use the objects it produces directly, as
214 // we have to used the deserialized ones that are linked together with the
215 // rest of the context snapshot.
216 Handle<JSGlobalProxy> CreateNewGlobals(
217 v8::Handle<v8::ObjectTemplate> global_template,
218 Handle<Object> global_object,
219 Handle<GlobalObject>* global_proxy_out);
220 // Hooks the given global proxy into the context. If the context was created
221 // by deserialization then this will unhook the global proxy that was
222 // deserialized, leaving the GC to pick it up.
223 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
224 Handle<JSGlobalProxy> global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +0000225 // Similarly, we want to use the inner global that has been created by the
226 // templates passed through the API. The inner global from the snapshot is
227 // detached from the other objects in the snapshot.
228 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
Andrei Popescu31002712010-02-23 13:46:05 +0000229 // New context initialization. Used for creating a context from scratch.
230 void InitializeGlobal(Handle<GlobalObject> inner_global,
231 Handle<JSFunction> empty_function);
232 // Installs the contents of the native .js files on the global objects.
233 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 void InstallNativeFunctions();
235 bool InstallNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100236 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100237 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100238 void InitializeNormalizedMapCaches();
Andrei Popescu31002712010-02-23 13:46:05 +0000239 // Used both for deserialized and from-scratch contexts to add the extensions
240 // provided.
241 static bool InstallExtensions(Handle<Context> global_context,
242 v8::ExtensionConfiguration* extensions);
243 static bool InstallExtension(const char* name);
244 static bool InstallExtension(v8::RegisteredExtension* current);
245 static void InstallSpecialObjects(Handle<Context> global_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000246 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000247 bool ConfigureApiObject(Handle<JSObject> object,
248 Handle<ObjectTemplateInfo> object_template);
249 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
250
251 // Migrates all properties from the 'from' object to the 'to'
252 // object and overrides the prototype in 'to' with the one from
253 // 'from'.
254 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
255 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
256 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
257
Steve Block6ded16b2010-05-10 14:33:55 +0100258 enum PrototypePropertyMode {
259 DONT_ADD_PROTOTYPE,
260 ADD_READONLY_PROTOTYPE,
261 ADD_WRITEABLE_PROTOTYPE
262 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100264 PrototypePropertyMode prototypeMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 void MakeFunctionInstancePrototypeWritable();
266
Steve Blocka7e24c12009-10-30 11:49:00 +0000267 static bool CompileBuiltin(int index);
268 static bool CompileNative(Vector<const char> name, Handle<String> source);
269 static bool CompileScriptCached(Vector<const char> name,
270 Handle<String> source,
271 SourceCodeCache* cache,
272 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000273 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 bool use_runtime_context);
275
276 Handle<Context> result_;
Andrei Popescu31002712010-02-23 13:46:05 +0000277 Handle<JSFunction> empty_function_;
278 BootstrapperActive active_;
279 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000280};
281
Steve Blocka7e24c12009-10-30 11:49:00 +0000282
283void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000284 extensions_cache.Iterate(v);
Steve Blockd0582a62009-12-15 09:54:21 +0000285 v->Synchronize("Extensions");
Steve Blocka7e24c12009-10-30 11:49:00 +0000286}
287
288
Steve Blocka7e24c12009-10-30 11:49:00 +0000289Handle<Context> Bootstrapper::CreateEnvironment(
290 Handle<Object> global_object,
291 v8::Handle<v8::ObjectTemplate> global_template,
292 v8::ExtensionConfiguration* extensions) {
Andrei Popescu31002712010-02-23 13:46:05 +0000293 HandleScope scope;
294 Handle<Context> env;
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 Genesis genesis(global_object, global_template, extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000296 env = genesis.result();
297 if (!env.is_null()) {
298 if (InstallExtensions(env, extensions)) {
299 return env;
300 }
301 }
302 return Handle<Context>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000303}
304
305
306static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
307 // object.__proto__ = proto;
308 Handle<Map> old_to_map = Handle<Map>(object->map());
309 Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map);
310 new_to_map->set_prototype(*proto);
311 object->set_map(*new_to_map);
312}
313
314
315void Bootstrapper::DetachGlobal(Handle<Context> env) {
316 JSGlobalProxy::cast(env->global_proxy())->set_context(*Factory::null_value());
317 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
318 Factory::null_value());
319 env->set_global_proxy(env->global());
320 env->global()->set_global_receiver(env->global());
321}
322
323
Andrei Popescu74b3c142010-03-29 12:03:09 +0100324void Bootstrapper::ReattachGlobal(Handle<Context> env,
325 Handle<Object> global_object) {
326 ASSERT(global_object->IsJSGlobalProxy());
327 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
328 env->global()->set_global_receiver(*global);
329 env->set_global_proxy(*global);
330 SetObjectPrototype(global, Handle<JSObject>(env->global()));
331 global->set_context(*env);
332}
333
334
Steve Blocka7e24c12009-10-30 11:49:00 +0000335static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
336 const char* name,
337 InstanceType type,
338 int instance_size,
339 Handle<JSObject> prototype,
340 Builtins::Name call,
341 bool is_ecma_native) {
342 Handle<String> symbol = Factory::LookupAsciiSymbol(name);
343 Handle<Code> call_code = Handle<Code>(Builtins::builtin(call));
Steve Block6ded16b2010-05-10 14:33:55 +0100344 Handle<JSFunction> function = prototype.is_null() ?
345 Factory::NewFunctionWithoutPrototype(symbol, call_code) :
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 Factory::NewFunctionWithPrototype(symbol,
347 type,
348 instance_size,
349 prototype,
350 call_code,
351 is_ecma_native);
Steve Block1e0659c2011-05-24 12:43:12 +0100352 SetLocalPropertyNoThrow(target, symbol, function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 if (is_ecma_native) {
354 function->shared()->set_instance_class_name(*symbol);
355 }
356 return function;
357}
358
359
360Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100361 PrototypePropertyMode prototypeMode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 Handle<DescriptorArray> result = Factory::empty_descriptor_array();
363
Steve Block6ded16b2010-05-10 14:33:55 +0100364 if (prototypeMode != DONT_ADD_PROTOTYPE) {
365 PropertyAttributes attributes = static_cast<PropertyAttributes>(
366 DONT_ENUM |
367 DONT_DELETE |
368 (prototypeMode == ADD_READONLY_PROTOTYPE ? READ_ONLY : 0));
369 result =
370 Factory::CopyAppendProxyDescriptor(
371 result,
372 Factory::prototype_symbol(),
373 Factory::NewProxy(&Accessors::FunctionPrototype),
374 attributes);
375 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000376
Steve Block6ded16b2010-05-10 14:33:55 +0100377 PropertyAttributes attributes =
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
379 // Add length.
380 result =
381 Factory::CopyAppendProxyDescriptor(
382 result,
383 Factory::length_symbol(),
384 Factory::NewProxy(&Accessors::FunctionLength),
385 attributes);
386
387 // Add name.
388 result =
389 Factory::CopyAppendProxyDescriptor(
390 result,
391 Factory::name_symbol(),
392 Factory::NewProxy(&Accessors::FunctionName),
393 attributes);
394
395 // Add arguments.
396 result =
397 Factory::CopyAppendProxyDescriptor(
398 result,
399 Factory::arguments_symbol(),
400 Factory::NewProxy(&Accessors::FunctionArguments),
401 attributes);
402
403 // Add caller.
404 result =
405 Factory::CopyAppendProxyDescriptor(
406 result,
407 Factory::caller_symbol(),
408 Factory::NewProxy(&Accessors::FunctionCaller),
409 attributes);
410
411 return result;
412}
413
414
Andrei Popescu31002712010-02-23 13:46:05 +0000415Handle<JSFunction> Genesis::CreateEmptyFunction() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 // Allocate the map for function instances.
417 Handle<Map> fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
418 global_context()->set_function_instance_map(*fm);
419 // Please note that the prototype property for function instances must be
420 // writable.
421 Handle<DescriptorArray> function_map_descriptors =
Steve Block6ded16b2010-05-10 14:33:55 +0100422 ComputeFunctionInstanceDescriptor(ADD_WRITEABLE_PROTOTYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000423 fm->set_instance_descriptors(*function_map_descriptors);
Steve Block6ded16b2010-05-10 14:33:55 +0100424 fm->set_function_with_prototype(true);
425
426 // Functions with this map will not have a 'prototype' property, and
427 // can not be used as constructors.
428 Handle<Map> function_without_prototype_map =
429 Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
430 global_context()->set_function_without_prototype_map(
431 *function_without_prototype_map);
432 Handle<DescriptorArray> function_without_prototype_map_descriptors =
433 ComputeFunctionInstanceDescriptor(DONT_ADD_PROTOTYPE);
434 function_without_prototype_map->set_instance_descriptors(
435 *function_without_prototype_map_descriptors);
436 function_without_prototype_map->set_function_with_prototype(false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000437
438 // Allocate the function map first and then patch the prototype later
439 fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
440 global_context()->set_function_map(*fm);
Steve Block6ded16b2010-05-10 14:33:55 +0100441 function_map_descriptors =
442 ComputeFunctionInstanceDescriptor(ADD_READONLY_PROTOTYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 fm->set_instance_descriptors(*function_map_descriptors);
Steve Block6ded16b2010-05-10 14:33:55 +0100444 fm->set_function_with_prototype(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000445
446 Handle<String> object_name = Handle<String>(Heap::Object_symbol());
447
448 { // --- O b j e c t ---
449 Handle<JSFunction> object_fun =
450 Factory::NewFunction(object_name, Factory::null_value());
451 Handle<Map> object_function_map =
452 Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
453 object_fun->set_initial_map(*object_function_map);
454 object_function_map->set_constructor(*object_fun);
455
456 global_context()->set_object_function(*object_fun);
457
458 // Allocate a new prototype for the object function.
459 Handle<JSObject> prototype = Factory::NewJSObject(Top::object_function(),
460 TENURED);
461
462 global_context()->set_initial_object_prototype(*prototype);
463 SetPrototype(object_fun, prototype);
464 object_function_map->
465 set_instance_descriptors(Heap::empty_descriptor_array());
466 }
467
468 // Allocate the empty function as the prototype for function ECMAScript
469 // 262 15.3.4.
470 Handle<String> symbol = Factory::LookupAsciiSymbol("Empty");
471 Handle<JSFunction> empty_function =
Steve Block6ded16b2010-05-10 14:33:55 +0100472 Factory::NewFunctionWithoutPrototype(symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +0000473
Andrei Popescu31002712010-02-23 13:46:05 +0000474 // --- E m p t y ---
475 Handle<Code> code =
476 Handle<Code>(Builtins::builtin(Builtins::EmptyFunction));
477 empty_function->set_code(*code);
Iain Merrick75681382010-08-19 15:07:18 +0100478 empty_function->shared()->set_code(*code);
Andrei Popescu31002712010-02-23 13:46:05 +0000479 Handle<String> source = Factory::NewStringFromAscii(CStrVector("() {}"));
480 Handle<Script> script = Factory::NewScript(source);
481 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
482 empty_function->shared()->set_script(*script);
483 empty_function->shared()->set_start_position(0);
484 empty_function->shared()->set_end_position(source->length());
485 empty_function->shared()->DontAdaptArguments();
486 global_context()->function_map()->set_prototype(*empty_function);
487 global_context()->function_instance_map()->set_prototype(*empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +0100488 global_context()->function_without_prototype_map()->
489 set_prototype(*empty_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000490
Andrei Popescu31002712010-02-23 13:46:05 +0000491 // Allocate the function map first and then patch the prototype later
Steve Block6ded16b2010-05-10 14:33:55 +0100492 Handle<Map> empty_fm = Factory::CopyMapDropDescriptors(
493 function_without_prototype_map);
494 empty_fm->set_instance_descriptors(
495 *function_without_prototype_map_descriptors);
Andrei Popescu31002712010-02-23 13:46:05 +0000496 empty_fm->set_prototype(global_context()->object_function()->prototype());
497 empty_function->set_map(*empty_fm);
498 return empty_function;
499}
500
501
Ben Murdochb0fe1622011-05-05 13:52:32 +0100502static void AddToWeakGlobalContextList(Context* context) {
503 ASSERT(context->IsGlobalContext());
504#ifdef DEBUG
505 { // NOLINT
506 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
507 // Check that context is not in the list yet.
508 for (Object* current = Heap::global_contexts_list();
509 !current->IsUndefined();
510 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
511 ASSERT(current != context);
512 }
513 }
514#endif
515 context->set(Context::NEXT_CONTEXT_LINK, Heap::global_contexts_list());
516 Heap::set_global_contexts_list(context);
517}
518
519
Andrei Popescu31002712010-02-23 13:46:05 +0000520void Genesis::CreateRoots() {
521 // Allocate the global context FixedArray first and then patch the
522 // closure and extension object later (we need the empty function
523 // and the global object, but in order to create those, we need the
524 // global context).
525 global_context_ =
526 Handle<Context>::cast(
527 GlobalHandles::Create(*Factory::NewGlobalContext()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100528 AddToWeakGlobalContextList(*global_context_);
Andrei Popescu31002712010-02-23 13:46:05 +0000529 Top::set_context(*global_context());
530
531 // Allocate the message listeners object.
532 {
533 v8::NeanderArray listeners;
534 global_context()->set_message_listeners(*listeners.value());
535 }
536}
537
538
539Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
540 v8::Handle<v8::ObjectTemplate> global_template,
541 Handle<Object> global_object,
542 Handle<GlobalObject>* inner_global_out) {
543 // The argument global_template aka data is an ObjectTemplateInfo.
544 // It has a constructor pointer that points at global_constructor which is a
545 // FunctionTemplateInfo.
546 // The global_constructor is used to create or reinitialize the global_proxy.
547 // The global_constructor also has a prototype_template pointer that points at
548 // js_global_template which is an ObjectTemplateInfo.
549 // That in turn has a constructor pointer that points at
550 // js_global_constructor which is a FunctionTemplateInfo.
551 // js_global_constructor is used to make js_global_function
552 // js_global_function is used to make the new inner_global.
553 //
554 // --- G l o b a l ---
555 // Step 1: Create a fresh inner JSGlobalObject.
556 Handle<JSFunction> js_global_function;
557 Handle<ObjectTemplateInfo> js_global_template;
558 if (!global_template.IsEmpty()) {
559 // Get prototype template of the global_template.
560 Handle<ObjectTemplateInfo> data =
561 v8::Utils::OpenHandle(*global_template);
562 Handle<FunctionTemplateInfo> global_constructor =
563 Handle<FunctionTemplateInfo>(
564 FunctionTemplateInfo::cast(data->constructor()));
565 Handle<Object> proto_template(global_constructor->prototype_template());
566 if (!proto_template->IsUndefined()) {
567 js_global_template =
568 Handle<ObjectTemplateInfo>::cast(proto_template);
569 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 }
571
Andrei Popescu31002712010-02-23 13:46:05 +0000572 if (js_global_template.is_null()) {
573 Handle<String> name = Handle<String>(Heap::empty_symbol());
574 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
575 js_global_function =
576 Factory::NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
Andrei Popescu402d9372010-02-26 13:31:12 +0000577 JSGlobalObject::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000578 // Change the constructor property of the prototype of the
579 // hidden global function to refer to the Object function.
580 Handle<JSObject> prototype =
581 Handle<JSObject>(
582 JSObject::cast(js_global_function->instance_prototype()));
Steve Block1e0659c2011-05-24 12:43:12 +0100583 SetLocalPropertyNoThrow(
584 prototype, Factory::constructor_symbol(), Top::object_function(), NONE);
Andrei Popescu31002712010-02-23 13:46:05 +0000585 } else {
586 Handle<FunctionTemplateInfo> js_global_constructor(
587 FunctionTemplateInfo::cast(js_global_template->constructor()));
588 js_global_function =
589 Factory::CreateApiFunction(js_global_constructor,
590 Factory::InnerGlobalObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000591 }
592
Andrei Popescu31002712010-02-23 13:46:05 +0000593 js_global_function->initial_map()->set_is_hidden_prototype();
594 Handle<GlobalObject> inner_global =
595 Factory::NewGlobalObject(js_global_function);
596 if (inner_global_out != NULL) {
597 *inner_global_out = inner_global;
598 }
599
600 // Step 2: create or re-initialize the global proxy object.
601 Handle<JSFunction> global_proxy_function;
602 if (global_template.IsEmpty()) {
603 Handle<String> name = Handle<String>(Heap::empty_symbol());
604 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
605 global_proxy_function =
606 Factory::NewFunction(name, JS_GLOBAL_PROXY_TYPE,
607 JSGlobalProxy::kSize, code, true);
608 } else {
609 Handle<ObjectTemplateInfo> data =
610 v8::Utils::OpenHandle(*global_template);
611 Handle<FunctionTemplateInfo> global_constructor(
612 FunctionTemplateInfo::cast(data->constructor()));
613 global_proxy_function =
614 Factory::CreateApiFunction(global_constructor,
615 Factory::OuterGlobalObject);
616 }
617
618 Handle<String> global_name = Factory::LookupAsciiSymbol("global");
619 global_proxy_function->shared()->set_instance_class_name(*global_name);
620 global_proxy_function->initial_map()->set_is_access_check_needed(true);
621
622 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
623 // Return the global proxy.
624
625 if (global_object.location() != NULL) {
626 ASSERT(global_object->IsJSGlobalProxy());
627 return ReinitializeJSGlobalProxy(
628 global_proxy_function,
629 Handle<JSGlobalProxy>::cast(global_object));
630 } else {
631 return Handle<JSGlobalProxy>::cast(
632 Factory::NewJSObject(global_proxy_function, TENURED));
633 }
634}
635
636
637void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
638 Handle<JSGlobalProxy> global_proxy) {
639 // Set the global context for the global object.
640 inner_global->set_global_context(*global_context());
641 inner_global->set_global_receiver(*global_proxy);
642 global_proxy->set_context(*global_context());
643 global_context()->set_global_proxy(*global_proxy);
644}
645
646
Andrei Popescu402d9372010-02-26 13:31:12 +0000647void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
648 Handle<GlobalObject> inner_global_from_snapshot(
649 GlobalObject::cast(global_context_->extension()));
650 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
651 global_context_->set_extension(*inner_global);
652 global_context_->set_global(*inner_global);
653 global_context_->set_security_token(*inner_global);
654 static const PropertyAttributes attributes =
655 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
656 ForceSetProperty(builtins_global,
657 Factory::LookupAsciiSymbol("global"),
658 inner_global,
659 attributes);
660 // Setup the reference from the global object to the builtins object.
661 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
662 TransferNamedProperties(inner_global_from_snapshot, inner_global);
663 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
664}
665
666
667// This is only called if we are not using snapshots. The equivalent
668// work in the snapshot case is done in HookUpInnerGlobal.
Andrei Popescu31002712010-02-23 13:46:05 +0000669void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
670 Handle<JSFunction> empty_function) {
671 // --- G l o b a l C o n t e x t ---
672 // Use the empty function as closure (no scope info).
673 global_context()->set_closure(*empty_function);
674 global_context()->set_fcontext(*global_context());
675 global_context()->set_previous(NULL);
676 // Set extension and global object.
677 global_context()->set_extension(*inner_global);
678 global_context()->set_global(*inner_global);
679 // Security setup: Set the security token of the global object to
680 // its the inner global. This makes the security check between two
681 // different contexts fail by default even in case of global
682 // object reinitialization.
683 global_context()->set_security_token(*inner_global);
684
685 Handle<String> object_name = Handle<String>(Heap::Object_symbol());
Steve Block1e0659c2011-05-24 12:43:12 +0100686 SetLocalPropertyNoThrow(inner_global, object_name,
687 Top::object_function(), DONT_ENUM);
Andrei Popescu31002712010-02-23 13:46:05 +0000688
Steve Blocka7e24c12009-10-30 11:49:00 +0000689 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
690
691 // Install global Function object
692 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
693 empty_function, Builtins::Illegal, true); // ECMA native.
694
695 { // --- A r r a y ---
696 Handle<JSFunction> array_function =
697 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
698 Top::initial_object_prototype(), Builtins::ArrayCode,
699 true);
700 array_function->shared()->set_construct_stub(
701 Builtins::builtin(Builtins::ArrayConstructCode));
702 array_function->shared()->DontAdaptArguments();
703
704 // This seems a bit hackish, but we need to make sure Array.length
705 // is 1.
706 array_function->shared()->set_length(1);
707 Handle<DescriptorArray> array_descriptors =
708 Factory::CopyAppendProxyDescriptor(
709 Factory::empty_descriptor_array(),
710 Factory::length_symbol(),
711 Factory::NewProxy(&Accessors::ArrayLength),
712 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
713
714 // Cache the fast JavaScript array map
715 global_context()->set_js_array_map(array_function->initial_map());
716 global_context()->js_array_map()->set_instance_descriptors(
717 *array_descriptors);
718 // array_function is used internally. JS code creating array object should
719 // search for the 'Array' property on the global object and use that one
720 // as the constructor. 'Array' property on a global object can be
721 // overwritten by JS code.
722 global_context()->set_array_function(*array_function);
723 }
724
725 { // --- N u m b e r ---
726 Handle<JSFunction> number_fun =
727 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
728 Top::initial_object_prototype(), Builtins::Illegal,
729 true);
730 global_context()->set_number_function(*number_fun);
731 }
732
733 { // --- B o o l e a n ---
734 Handle<JSFunction> boolean_fun =
735 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
736 Top::initial_object_prototype(), Builtins::Illegal,
737 true);
738 global_context()->set_boolean_function(*boolean_fun);
739 }
740
741 { // --- S t r i n g ---
742 Handle<JSFunction> string_fun =
743 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
744 Top::initial_object_prototype(), Builtins::Illegal,
745 true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100746 string_fun->shared()->set_construct_stub(
747 Builtins::builtin(Builtins::StringConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000748 global_context()->set_string_function(*string_fun);
749 // Add 'length' property to strings.
750 Handle<DescriptorArray> string_descriptors =
751 Factory::CopyAppendProxyDescriptor(
752 Factory::empty_descriptor_array(),
753 Factory::length_symbol(),
754 Factory::NewProxy(&Accessors::StringLength),
755 static_cast<PropertyAttributes>(DONT_ENUM |
756 DONT_DELETE |
757 READ_ONLY));
758
759 Handle<Map> string_map =
760 Handle<Map>(global_context()->string_function()->initial_map());
761 string_map->set_instance_descriptors(*string_descriptors);
762 }
763
764 { // --- D a t e ---
765 // Builtin functions for Date.prototype.
766 Handle<JSFunction> date_fun =
767 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
768 Top::initial_object_prototype(), Builtins::Illegal,
769 true);
770
771 global_context()->set_date_function(*date_fun);
772 }
773
774
775 { // -- R e g E x p
776 // Builtin functions for RegExp.prototype.
777 Handle<JSFunction> regexp_fun =
778 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
779 Top::initial_object_prototype(), Builtins::Illegal,
780 true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000781 global_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +0100782
783 ASSERT(regexp_fun->has_initial_map());
784 Handle<Map> initial_map(regexp_fun->initial_map());
785
786 ASSERT_EQ(0, initial_map->inobject_properties());
787
788 Handle<DescriptorArray> descriptors = Factory::NewDescriptorArray(5);
789 PropertyAttributes final =
790 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
791 int enum_index = 0;
792 {
793 // ECMA-262, section 15.10.7.1.
794 FieldDescriptor field(Heap::source_symbol(),
795 JSRegExp::kSourceFieldIndex,
796 final,
797 enum_index++);
798 descriptors->Set(0, &field);
799 }
800 {
801 // ECMA-262, section 15.10.7.2.
802 FieldDescriptor field(Heap::global_symbol(),
803 JSRegExp::kGlobalFieldIndex,
804 final,
805 enum_index++);
806 descriptors->Set(1, &field);
807 }
808 {
809 // ECMA-262, section 15.10.7.3.
810 FieldDescriptor field(Heap::ignore_case_symbol(),
811 JSRegExp::kIgnoreCaseFieldIndex,
812 final,
813 enum_index++);
814 descriptors->Set(2, &field);
815 }
816 {
817 // ECMA-262, section 15.10.7.4.
818 FieldDescriptor field(Heap::multiline_symbol(),
819 JSRegExp::kMultilineFieldIndex,
820 final,
821 enum_index++);
822 descriptors->Set(3, &field);
823 }
824 {
825 // ECMA-262, section 15.10.7.5.
826 PropertyAttributes writable =
827 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
828 FieldDescriptor field(Heap::last_index_symbol(),
829 JSRegExp::kLastIndexFieldIndex,
830 writable,
831 enum_index++);
832 descriptors->Set(4, &field);
833 }
834 descriptors->SetNextEnumerationIndex(enum_index);
835 descriptors->Sort();
836
837 initial_map->set_inobject_properties(5);
838 initial_map->set_pre_allocated_property_fields(5);
839 initial_map->set_unused_property_fields(0);
840 initial_map->set_instance_size(
841 initial_map->instance_size() + 5 * kPointerSize);
842 initial_map->set_instance_descriptors(*descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100843 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 }
845
846 { // -- J S O N
847 Handle<String> name = Factory::NewStringFromAscii(CStrVector("JSON"));
848 Handle<JSFunction> cons = Factory::NewFunction(
849 name,
850 Factory::the_hole_value());
851 cons->SetInstancePrototype(global_context()->initial_object_prototype());
852 cons->SetInstanceClassName(*name);
853 Handle<JSObject> json_object = Factory::NewJSObject(cons, TENURED);
854 ASSERT(json_object->IsJSObject());
Steve Block1e0659c2011-05-24 12:43:12 +0100855 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000856 global_context()->set_json_object(*json_object);
857 }
858
859 { // --- arguments_boilerplate_
860 // Make sure we can recognize argument objects at runtime.
861 // This is done by introducing an anonymous function with
862 // class_name equals 'Arguments'.
863 Handle<String> symbol = Factory::LookupAsciiSymbol("Arguments");
864 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
865 Handle<JSObject> prototype =
866 Handle<JSObject>(
867 JSObject::cast(global_context()->object_function()->prototype()));
868
869 Handle<JSFunction> function =
870 Factory::NewFunctionWithPrototype(symbol,
871 JS_OBJECT_TYPE,
872 JSObject::kHeaderSize,
873 prototype,
874 code,
875 false);
876 ASSERT(!function->has_initial_map());
877 function->shared()->set_instance_class_name(*symbol);
878 function->shared()->set_expected_nof_properties(2);
879 Handle<JSObject> result = Factory::NewJSObject(function);
880
881 global_context()->set_arguments_boilerplate(*result);
882 // Note: callee must be added as the first property and
883 // length must be added as the second property.
Steve Block1e0659c2011-05-24 12:43:12 +0100884 SetLocalPropertyNoThrow(result, Factory::callee_symbol(),
885 Factory::undefined_value(),
886 DONT_ENUM);
887 SetLocalPropertyNoThrow(result, Factory::length_symbol(),
888 Factory::undefined_value(),
889 DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000890
891#ifdef DEBUG
892 LookupResult lookup;
893 result->LocalLookup(Heap::callee_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +0000894 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Blocka7e24c12009-10-30 11:49:00 +0000895 ASSERT(lookup.GetFieldIndex() == Heap::arguments_callee_index);
896
897 result->LocalLookup(Heap::length_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +0000898 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Blocka7e24c12009-10-30 11:49:00 +0000899 ASSERT(lookup.GetFieldIndex() == Heap::arguments_length_index);
900
901 ASSERT(result->map()->inobject_properties() > Heap::arguments_callee_index);
902 ASSERT(result->map()->inobject_properties() > Heap::arguments_length_index);
903
904 // Check the state of the object.
905 ASSERT(result->HasFastProperties());
906 ASSERT(result->HasFastElements());
907#endif
908 }
909
910 { // --- context extension
911 // Create a function for the context extension objects.
912 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
913 Handle<JSFunction> context_extension_fun =
914 Factory::NewFunction(Factory::empty_symbol(),
915 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
916 JSObject::kHeaderSize,
917 code,
918 true);
919
920 Handle<String> name = Factory::LookupAsciiSymbol("context_extension");
921 context_extension_fun->shared()->set_instance_class_name(*name);
922 global_context()->set_context_extension_function(*context_extension_fun);
923 }
924
925
926 {
927 // Setup the call-as-function delegate.
928 Handle<Code> code =
929 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsFunction));
930 Handle<JSFunction> delegate =
931 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE,
932 JSObject::kHeaderSize, code, true);
933 global_context()->set_call_as_function_delegate(*delegate);
934 delegate->shared()->DontAdaptArguments();
935 }
936
937 {
938 // Setup the call-as-constructor delegate.
939 Handle<Code> code =
940 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsConstructor));
941 Handle<JSFunction> delegate =
942 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE,
943 JSObject::kHeaderSize, code, true);
944 global_context()->set_call_as_constructor_delegate(*delegate);
945 delegate->shared()->DontAdaptArguments();
946 }
947
Steve Blocka7e24c12009-10-30 11:49:00 +0000948 // Initialize the out of memory slot.
949 global_context()->set_out_of_memory(Heap::false_value());
950
951 // Initialize the data slot.
952 global_context()->set_data(Heap::undefined_value());
953}
954
955
956bool Genesis::CompileBuiltin(int index) {
957 Vector<const char> name = Natives::GetScriptName(index);
958 Handle<String> source_code = Bootstrapper::NativesSourceLookup(index);
959 return CompileNative(name, source_code);
960}
961
962
963bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
964 HandleScope scope;
965#ifdef ENABLE_DEBUGGER_SUPPORT
966 Debugger::set_compiling_natives(true);
967#endif
Andrei Popescu31002712010-02-23 13:46:05 +0000968 bool result = CompileScriptCached(name,
969 source,
970 NULL,
971 NULL,
972 Handle<Context>(Top::context()),
973 true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000974 ASSERT(Top::has_pending_exception() != result);
975 if (!result) Top::clear_pending_exception();
976#ifdef ENABLE_DEBUGGER_SUPPORT
977 Debugger::set_compiling_natives(false);
978#endif
979 return result;
980}
981
982
983bool Genesis::CompileScriptCached(Vector<const char> name,
984 Handle<String> source,
985 SourceCodeCache* cache,
986 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000987 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000988 bool use_runtime_context) {
989 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100990 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +0000991
992 // If we can't find the function in the cache, we compile a new
993 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +0100994 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 ASSERT(source->IsAsciiRepresentation());
996 Handle<String> script_name = Factory::NewStringFromUtf8(name);
Steve Block6ded16b2010-05-10 14:33:55 +0100997 function_info = Compiler::Compile(
Andrei Popescu31002712010-02-23 13:46:05 +0000998 source,
999 script_name,
1000 0,
1001 0,
1002 extension,
1003 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001004 Handle<String>::null(),
Andrei Popescu31002712010-02-23 13:46:05 +00001005 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001006 if (function_info.is_null()) return false;
1007 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 }
1009
1010 // Setup the function context. Conceptually, we should clone the
1011 // function before overwriting the context but since we're in a
1012 // single-threaded environment it is not strictly necessary.
Andrei Popescu31002712010-02-23 13:46:05 +00001013 ASSERT(top_context->IsGlobalContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001014 Handle<Context> context =
1015 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001016 ? Handle<Context>(top_context->runtime_context())
1017 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001018 Handle<JSFunction> fun =
Steve Block6ded16b2010-05-10 14:33:55 +01001019 Factory::NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001020
Leon Clarke4515c472010-02-03 11:58:03 +00001021 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001022 // object as the receiver. Provide no parameters.
1023 Handle<Object> receiver =
1024 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001025 ? top_context->builtins()
1026 : top_context->global());
Steve Blocka7e24c12009-10-30 11:49:00 +00001027 bool has_pending_exception;
1028 Handle<Object> result =
1029 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
1030 if (has_pending_exception) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001031 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001032}
1033
1034
John Reck59135872010-11-02 12:39:01 -07001035#define INSTALL_NATIVE(Type, name, var) \
1036 Handle<String> var##_name = Factory::LookupAsciiSymbol(name); \
1037 global_context()->set_##var(Type::cast( \
1038 global_context()->builtins()->GetPropertyNoExceptionThrown(*var##_name)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001039
1040void Genesis::InstallNativeFunctions() {
1041 HandleScope scope;
1042 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1043 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1044 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1045 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1046 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1047 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1048 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1049 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Leon Clarkee46be812010-01-19 14:06:41 +00001050 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001051 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1052 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1053 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001054 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1055 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1056}
1057
1058#undef INSTALL_NATIVE
1059
1060
1061bool Genesis::InstallNatives() {
1062 HandleScope scope;
1063
1064 // Create a function for the builtins object. Allocate space for the
1065 // JavaScript builtins, a reference to the builtins object
1066 // (itself) and a reference to the global_context directly in the object.
1067 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
1068 Handle<JSFunction> builtins_fun =
1069 Factory::NewFunction(Factory::empty_symbol(), JS_BUILTINS_OBJECT_TYPE,
1070 JSBuiltinsObject::kSize, code, true);
1071
1072 Handle<String> name = Factory::LookupAsciiSymbol("builtins");
1073 builtins_fun->shared()->set_instance_class_name(*name);
1074
1075 // Allocate the builtins object.
1076 Handle<JSBuiltinsObject> builtins =
1077 Handle<JSBuiltinsObject>::cast(Factory::NewGlobalObject(builtins_fun));
1078 builtins->set_builtins(*builtins);
1079 builtins->set_global_context(*global_context());
1080 builtins->set_global_receiver(*builtins);
1081
1082 // Setup the 'global' properties of the builtins object. The
1083 // 'global' property that refers to the global object is the only
1084 // way to get from code running in the builtins context to the
1085 // global object.
1086 static const PropertyAttributes attributes =
1087 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001088 Handle<String> global_symbol = Factory::LookupAsciiSymbol("global");
Steve Block1e0659c2011-05-24 12:43:12 +01001089 Handle<Object> global_obj(global_context()->global());
1090 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001091
1092 // Setup the reference from the global object to the builtins object.
1093 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1094
1095 // Create a bridge function that has context in the global context.
1096 Handle<JSFunction> bridge =
1097 Factory::NewFunction(Factory::empty_symbol(), Factory::undefined_value());
1098 ASSERT(bridge->context() == *Top::global_context());
1099
1100 // Allocate the builtins context.
1101 Handle<Context> context =
1102 Factory::NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
1103 context->set_global(*builtins); // override builtins global object
1104
1105 global_context()->set_runtime_context(*context);
1106
1107 { // -- S c r i p t
1108 // Builtin functions for Script.
1109 Handle<JSFunction> script_fun =
1110 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
1111 Top::initial_object_prototype(), Builtins::Illegal,
1112 false);
1113 Handle<JSObject> prototype =
1114 Factory::NewJSObject(Top::object_function(), TENURED);
1115 SetPrototype(script_fun, prototype);
1116 global_context()->set_script_function(*script_fun);
1117
1118 // Add 'source' and 'data' property to scripts.
1119 PropertyAttributes common_attributes =
1120 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1121 Handle<Proxy> proxy_source = Factory::NewProxy(&Accessors::ScriptSource);
1122 Handle<DescriptorArray> script_descriptors =
1123 Factory::CopyAppendProxyDescriptor(
1124 Factory::empty_descriptor_array(),
1125 Factory::LookupAsciiSymbol("source"),
1126 proxy_source,
1127 common_attributes);
1128 Handle<Proxy> proxy_name = Factory::NewProxy(&Accessors::ScriptName);
1129 script_descriptors =
1130 Factory::CopyAppendProxyDescriptor(
1131 script_descriptors,
1132 Factory::LookupAsciiSymbol("name"),
1133 proxy_name,
1134 common_attributes);
1135 Handle<Proxy> proxy_id = Factory::NewProxy(&Accessors::ScriptId);
1136 script_descriptors =
1137 Factory::CopyAppendProxyDescriptor(
1138 script_descriptors,
1139 Factory::LookupAsciiSymbol("id"),
1140 proxy_id,
1141 common_attributes);
1142 Handle<Proxy> proxy_line_offset =
1143 Factory::NewProxy(&Accessors::ScriptLineOffset);
1144 script_descriptors =
1145 Factory::CopyAppendProxyDescriptor(
1146 script_descriptors,
1147 Factory::LookupAsciiSymbol("line_offset"),
1148 proxy_line_offset,
1149 common_attributes);
1150 Handle<Proxy> proxy_column_offset =
1151 Factory::NewProxy(&Accessors::ScriptColumnOffset);
1152 script_descriptors =
1153 Factory::CopyAppendProxyDescriptor(
1154 script_descriptors,
1155 Factory::LookupAsciiSymbol("column_offset"),
1156 proxy_column_offset,
1157 common_attributes);
1158 Handle<Proxy> proxy_data = Factory::NewProxy(&Accessors::ScriptData);
1159 script_descriptors =
1160 Factory::CopyAppendProxyDescriptor(
1161 script_descriptors,
1162 Factory::LookupAsciiSymbol("data"),
1163 proxy_data,
1164 common_attributes);
1165 Handle<Proxy> proxy_type = Factory::NewProxy(&Accessors::ScriptType);
1166 script_descriptors =
1167 Factory::CopyAppendProxyDescriptor(
1168 script_descriptors,
1169 Factory::LookupAsciiSymbol("type"),
1170 proxy_type,
1171 common_attributes);
1172 Handle<Proxy> proxy_compilation_type =
1173 Factory::NewProxy(&Accessors::ScriptCompilationType);
1174 script_descriptors =
1175 Factory::CopyAppendProxyDescriptor(
1176 script_descriptors,
1177 Factory::LookupAsciiSymbol("compilation_type"),
1178 proxy_compilation_type,
1179 common_attributes);
1180 Handle<Proxy> proxy_line_ends =
1181 Factory::NewProxy(&Accessors::ScriptLineEnds);
1182 script_descriptors =
1183 Factory::CopyAppendProxyDescriptor(
1184 script_descriptors,
1185 Factory::LookupAsciiSymbol("line_ends"),
1186 proxy_line_ends,
1187 common_attributes);
1188 Handle<Proxy> proxy_context_data =
1189 Factory::NewProxy(&Accessors::ScriptContextData);
1190 script_descriptors =
1191 Factory::CopyAppendProxyDescriptor(
1192 script_descriptors,
1193 Factory::LookupAsciiSymbol("context_data"),
1194 proxy_context_data,
1195 common_attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001196 Handle<Proxy> proxy_eval_from_script =
1197 Factory::NewProxy(&Accessors::ScriptEvalFromScript);
Steve Blocka7e24c12009-10-30 11:49:00 +00001198 script_descriptors =
1199 Factory::CopyAppendProxyDescriptor(
1200 script_descriptors,
Steve Blockd0582a62009-12-15 09:54:21 +00001201 Factory::LookupAsciiSymbol("eval_from_script"),
1202 proxy_eval_from_script,
Steve Blocka7e24c12009-10-30 11:49:00 +00001203 common_attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001204 Handle<Proxy> proxy_eval_from_script_position =
1205 Factory::NewProxy(&Accessors::ScriptEvalFromScriptPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +00001206 script_descriptors =
1207 Factory::CopyAppendProxyDescriptor(
1208 script_descriptors,
Steve Blockd0582a62009-12-15 09:54:21 +00001209 Factory::LookupAsciiSymbol("eval_from_script_position"),
1210 proxy_eval_from_script_position,
1211 common_attributes);
1212 Handle<Proxy> proxy_eval_from_function_name =
1213 Factory::NewProxy(&Accessors::ScriptEvalFromFunctionName);
1214 script_descriptors =
1215 Factory::CopyAppendProxyDescriptor(
1216 script_descriptors,
1217 Factory::LookupAsciiSymbol("eval_from_function_name"),
1218 proxy_eval_from_function_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001219 common_attributes);
1220
1221 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1222 script_map->set_instance_descriptors(*script_descriptors);
1223
1224 // Allocate the empty script.
1225 Handle<Script> script = Factory::NewScript(Factory::empty_string());
1226 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Andrei Popescu31002712010-02-23 13:46:05 +00001227 Heap::public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001228 }
Steve Block6ded16b2010-05-10 14:33:55 +01001229 {
1230 // Builtin function for OpaqueReference -- a JSValue-based object,
1231 // that keeps its field isolated from JavaScript code. It may store
1232 // objects, that JavaScript code may not access.
1233 Handle<JSFunction> opaque_reference_fun =
1234 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
1235 JSValue::kSize, Top::initial_object_prototype(),
1236 Builtins::Illegal, false);
1237 Handle<JSObject> prototype =
1238 Factory::NewJSObject(Top::object_function(), TENURED);
1239 SetPrototype(opaque_reference_fun, prototype);
1240 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1241 }
1242
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001243 { // --- I n t e r n a l A r r a y ---
1244 // An array constructor on the builtins object that works like
1245 // the public Array constructor, except that its prototype
1246 // doesn't inherit from Object.prototype.
1247 // To be used only for internal work by builtins. Instances
1248 // must not be leaked to user code.
1249 // Only works correctly when called as a constructor. The normal
1250 // Array code uses Array.prototype as prototype when called as
1251 // a function.
1252 Handle<JSFunction> array_function =
1253 InstallFunction(builtins,
1254 "InternalArray",
1255 JS_ARRAY_TYPE,
1256 JSArray::kSize,
1257 Top::initial_object_prototype(),
1258 Builtins::ArrayCode,
1259 true);
1260 Handle<JSObject> prototype =
1261 Factory::NewJSObject(Top::object_function(), TENURED);
1262 SetPrototype(array_function, prototype);
1263
1264 array_function->shared()->set_construct_stub(
1265 Builtins::builtin(Builtins::ArrayConstructCode));
1266 array_function->shared()->DontAdaptArguments();
1267
1268 // Make "length" magic on instances.
1269 Handle<DescriptorArray> array_descriptors =
1270 Factory::CopyAppendProxyDescriptor(
1271 Factory::empty_descriptor_array(),
1272 Factory::length_symbol(),
1273 Factory::NewProxy(&Accessors::ArrayLength),
1274 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1275
1276 array_function->initial_map()->set_instance_descriptors(
1277 *array_descriptors);
1278 }
1279
Steve Block6ded16b2010-05-10 14:33:55 +01001280 if (FLAG_disable_native_files) {
1281 PrintF("Warning: Running without installed natives!\n");
1282 return true;
1283 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001284
Andrei Popescu31002712010-02-23 13:46:05 +00001285 // Install natives.
1286 for (int i = Natives::GetDebuggerCount();
1287 i < Natives::GetBuiltinsCount();
1288 i++) {
1289 Vector<const char> name = Natives::GetScriptName(i);
1290 if (!CompileBuiltin(i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001291 // TODO(ager): We really only need to install the JS builtin
1292 // functions on the builtins object after compiling and running
1293 // runtime.js.
1294 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001295 }
1296
1297 InstallNativeFunctions();
1298
Iain Merrick75681382010-08-19 15:07:18 +01001299 // Store the map for the string prototype after the natives has been compiled
1300 // and the String function has been setup.
1301 Handle<JSFunction> string_function(global_context()->string_function());
1302 ASSERT(JSObject::cast(
1303 string_function->initial_map()->prototype())->HasFastProperties());
1304 global_context()->set_string_function_prototype_map(
1305 HeapObject::cast(string_function->initial_map()->prototype())->map());
1306
Ben Murdochb0fe1622011-05-05 13:52:32 +01001307 InstallBuiltinFunctionIds();
Kristian Monsen25f61362010-05-21 11:50:48 +01001308
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 // Install Function.prototype.call and apply.
1310 { Handle<String> key = Factory::function_class_symbol();
1311 Handle<JSFunction> function =
1312 Handle<JSFunction>::cast(GetProperty(Top::global(), key));
1313 Handle<JSObject> proto =
1314 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1315
1316 // Install the call and the apply functions.
1317 Handle<JSFunction> call =
1318 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001319 Handle<JSObject>::null(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001320 Builtins::FunctionCall,
1321 false);
1322 Handle<JSFunction> apply =
1323 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001324 Handle<JSObject>::null(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001325 Builtins::FunctionApply,
1326 false);
1327
1328 // Make sure that Function.prototype.call appears to be compiled.
1329 // The code will never be called, but inline caching for call will
1330 // only work if it appears to be compiled.
1331 call->shared()->DontAdaptArguments();
1332 ASSERT(call->is_compiled());
1333
1334 // Set the expected parameters for apply to 2; required by builtin.
1335 apply->shared()->set_formal_parameter_count(2);
1336
1337 // Set the lengths for the functions to satisfy ECMA-262.
1338 call->shared()->set_length(1);
1339 apply->shared()->set_length(2);
1340 }
1341
Steve Block6ded16b2010-05-10 14:33:55 +01001342 // Create a constructor for RegExp results (a variant of Array that
1343 // predefines the two properties index and match).
1344 {
1345 // RegExpResult initial map.
1346
1347 // Find global.Array.prototype to inherit from.
1348 Handle<JSFunction> array_constructor(global_context()->array_function());
1349 Handle<JSObject> array_prototype(
1350 JSObject::cast(array_constructor->instance_prototype()));
1351
1352 // Add initial map.
1353 Handle<Map> initial_map =
1354 Factory::NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
1355 initial_map->set_constructor(*array_constructor);
1356
1357 // Set prototype on map.
1358 initial_map->set_non_instance_prototype(false);
1359 initial_map->set_prototype(*array_prototype);
1360
1361 // Update map with length accessor from Array and add "index" and "input".
1362 Handle<Map> array_map(global_context()->js_array_map());
1363 Handle<DescriptorArray> array_descriptors(
1364 array_map->instance_descriptors());
1365 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1366
1367 Handle<DescriptorArray> reresult_descriptors =
1368 Factory::NewDescriptorArray(3);
1369
1370 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1371
1372 int enum_index = 0;
1373 {
1374 FieldDescriptor index_field(Heap::index_symbol(),
1375 JSRegExpResult::kIndexIndex,
1376 NONE,
1377 enum_index++);
1378 reresult_descriptors->Set(1, &index_field);
1379 }
1380
1381 {
1382 FieldDescriptor input_field(Heap::input_symbol(),
1383 JSRegExpResult::kInputIndex,
1384 NONE,
1385 enum_index++);
1386 reresult_descriptors->Set(2, &input_field);
1387 }
1388 reresult_descriptors->Sort();
1389
1390 initial_map->set_inobject_properties(2);
1391 initial_map->set_pre_allocated_property_fields(2);
1392 initial_map->set_unused_property_fields(0);
1393 initial_map->set_instance_descriptors(*reresult_descriptors);
1394
1395 global_context()->set_regexp_result_map(*initial_map);
1396 }
1397
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001398
Steve Blocka7e24c12009-10-30 11:49:00 +00001399#ifdef DEBUG
1400 builtins->Verify();
1401#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001402
Steve Blocka7e24c12009-10-30 11:49:00 +00001403 return true;
1404}
1405
1406
Ben Murdochb0fe1622011-05-05 13:52:32 +01001407static Handle<JSObject> ResolveBuiltinIdHolder(
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001408 Handle<Context> global_context,
1409 const char* holder_expr) {
1410 Handle<GlobalObject> global(global_context->global());
1411 const char* period_pos = strchr(holder_expr, '.');
1412 if (period_pos == NULL) {
1413 return Handle<JSObject>::cast(
1414 GetProperty(global, Factory::LookupAsciiSymbol(holder_expr)));
Steve Block59151502010-09-22 15:07:15 +01001415 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001416 ASSERT_EQ(".prototype", period_pos);
1417 Vector<const char> property(holder_expr,
1418 static_cast<int>(period_pos - holder_expr));
1419 Handle<JSFunction> function = Handle<JSFunction>::cast(
1420 GetProperty(global, Factory::LookupSymbol(property)));
1421 return Handle<JSObject>(JSObject::cast(function->prototype()));
1422}
1423
1424
Ben Murdochb0fe1622011-05-05 13:52:32 +01001425static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1426 const char* function_name,
1427 BuiltinFunctionId id) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001428 Handle<String> name = Factory::LookupAsciiSymbol(function_name);
John Reck59135872010-11-02 12:39:01 -07001429 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1430 Handle<JSFunction> function(JSFunction::cast(function_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001431 function->shared()->set_function_data(Smi::FromInt(id));
1432}
1433
1434
Ben Murdochb0fe1622011-05-05 13:52:32 +01001435void Genesis::InstallBuiltinFunctionIds() {
Kristian Monsen25f61362010-05-21 11:50:48 +01001436 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001437#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1438 { \
1439 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1440 global_context(), #holder_expr); \
1441 BuiltinFunctionId id = k##name; \
1442 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01001443 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001444 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1445#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01001446}
1447
1448
Steve Block6ded16b2010-05-10 14:33:55 +01001449// Do not forget to update macros.py with named constant
1450// of cache id.
1451#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1452 F(16, global_context()->regexp_function())
1453
1454
1455static FixedArray* CreateCache(int size, JSFunction* factory) {
1456 // Caches are supposed to live for a long time, allocate in old space.
1457 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
1458 // Cannot use cast as object is not fully initialized yet.
1459 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
1460 *Factory::NewFixedArrayWithHoles(array_size, TENURED));
1461 cache->set(JSFunctionResultCache::kFactoryIndex, factory);
1462 cache->MakeZeroSize();
1463 return cache;
1464}
1465
1466
1467void Genesis::InstallJSFunctionResultCaches() {
1468 const int kNumberOfCaches = 0 +
1469#define F(size, func) + 1
1470 JSFUNCTION_RESULT_CACHE_LIST(F)
1471#undef F
1472 ;
1473
1474 Handle<FixedArray> caches = Factory::NewFixedArray(kNumberOfCaches, TENURED);
1475
1476 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001477
1478#define F(size, func) do { \
1479 FixedArray* cache = CreateCache((size), (func)); \
1480 caches->set(index++, cache); \
1481 } while (false)
1482
1483 JSFUNCTION_RESULT_CACHE_LIST(F);
1484
Steve Block6ded16b2010-05-10 14:33:55 +01001485#undef F
1486
1487 global_context()->set_jsfunction_result_caches(*caches);
1488}
1489
1490
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001491void Genesis::InitializeNormalizedMapCaches() {
1492 Handle<FixedArray> array(
1493 Factory::NewFixedArray(NormalizedMapCache::kEntries, TENURED));
1494 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1495}
1496
1497
Andrei Popescu31002712010-02-23 13:46:05 +00001498int BootstrapperActive::nesting_ = 0;
1499
1500
1501bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1502 v8::ExtensionConfiguration* extensions) {
1503 BootstrapperActive active;
1504 SaveContext saved_context;
1505 Top::set_context(*global_context);
1506 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1507 Genesis::InstallSpecialObjects(global_context);
1508 return true;
1509}
1510
1511
1512void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001513 HandleScope scope;
1514 Handle<JSGlobalObject> js_global(
Andrei Popescu31002712010-02-23 13:46:05 +00001515 JSGlobalObject::cast(global_context->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001516 // Expose the natives in global if a name for it is specified.
1517 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1518 Handle<String> natives_string =
1519 Factory::LookupAsciiSymbol(FLAG_expose_natives_as);
Steve Block1e0659c2011-05-24 12:43:12 +01001520 SetLocalPropertyNoThrow(js_global, natives_string,
1521 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001522 }
1523
1524 Handle<Object> Error = GetProperty(js_global, "Error");
1525 if (Error->IsJSObject()) {
1526 Handle<String> name = Factory::LookupAsciiSymbol("stackTraceLimit");
Steve Block1e0659c2011-05-24 12:43:12 +01001527 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1528 name,
1529 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1530 NONE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 }
1532
1533#ifdef ENABLE_DEBUGGER_SUPPORT
1534 // Expose the debug global object in global if a name for it is specified.
1535 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
1536 // If loading fails we just bail out without installing the
1537 // debugger but without tanking the whole context.
Andrei Popescu31002712010-02-23 13:46:05 +00001538 if (!Debug::Load()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001539 // Set the security token for the debugger context to the same as
1540 // the shell global context to allow calling between these (otherwise
1541 // exposing debug global object doesn't make much sense).
1542 Debug::debug_context()->set_security_token(
Andrei Popescu31002712010-02-23 13:46:05 +00001543 global_context->security_token());
Steve Blocka7e24c12009-10-30 11:49:00 +00001544
1545 Handle<String> debug_string =
1546 Factory::LookupAsciiSymbol(FLAG_expose_debug_as);
Steve Block1e0659c2011-05-24 12:43:12 +01001547 Handle<Object> global_proxy(Debug::debug_context()->global_proxy());
1548 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001549 }
1550#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001551}
1552
1553
Andrei Popescu31002712010-02-23 13:46:05 +00001554bool Genesis::InstallExtensions(Handle<Context> global_context,
1555 v8::ExtensionConfiguration* extensions) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001556 // Clear coloring of extension list
1557 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1558 while (current != NULL) {
1559 current->set_state(v8::UNVISITED);
1560 current = current->next();
1561 }
Andrei Popescu31002712010-02-23 13:46:05 +00001562 // Install auto extensions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001563 current = v8::RegisteredExtension::first_extension();
1564 while (current != NULL) {
1565 if (current->extension()->auto_enable())
1566 InstallExtension(current);
1567 current = current->next();
1568 }
1569
1570 if (FLAG_expose_gc) InstallExtension("v8/gc");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001571 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
Steve Blocka7e24c12009-10-30 11:49:00 +00001572
1573 if (extensions == NULL) return true;
1574 // Install required extensions
1575 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1576 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1577 for (int i = 0; i < count; i++) {
1578 if (!InstallExtension(names[i]))
1579 return false;
1580 }
1581
1582 return true;
1583}
1584
1585
1586// Installs a named extension. This methods is unoptimized and does
1587// not scale well if we want to support a large number of extensions.
1588bool Genesis::InstallExtension(const char* name) {
1589 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1590 // Loop until we find the relevant extension
1591 while (current != NULL) {
1592 if (strcmp(name, current->extension()->name()) == 0) break;
1593 current = current->next();
1594 }
1595 // Didn't find the extension; fail.
1596 if (current == NULL) {
1597 v8::Utils::ReportApiFailure(
1598 "v8::Context::New()", "Cannot find required extension");
1599 return false;
1600 }
1601 return InstallExtension(current);
1602}
1603
1604
1605bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1606 HandleScope scope;
1607
1608 if (current->state() == v8::INSTALLED) return true;
1609 // The current node has already been visited so there must be a
1610 // cycle in the dependency graph; fail.
1611 if (current->state() == v8::VISITED) {
1612 v8::Utils::ReportApiFailure(
1613 "v8::Context::New()", "Circular extension dependency");
1614 return false;
1615 }
1616 ASSERT(current->state() == v8::UNVISITED);
1617 current->set_state(v8::VISITED);
1618 v8::Extension* extension = current->extension();
1619 // Install the extension's dependencies
1620 for (int i = 0; i < extension->dependency_count(); i++) {
1621 if (!InstallExtension(extension->dependencies()[i])) return false;
1622 }
1623 Vector<const char> source = CStrVector(extension->source());
1624 Handle<String> source_code = Factory::NewStringFromAscii(source);
1625 bool result = CompileScriptCached(CStrVector(extension->name()),
1626 source_code,
Andrei Popescu31002712010-02-23 13:46:05 +00001627 &extensions_cache,
1628 extension,
1629 Handle<Context>(Top::context()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001630 false);
1631 ASSERT(Top::has_pending_exception() != result);
1632 if (!result) {
1633 Top::clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001634 }
1635 current->set_state(v8::INSTALLED);
1636 return result;
1637}
1638
1639
Andrei Popescu402d9372010-02-26 13:31:12 +00001640bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1641 HandleScope scope;
1642 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1643 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
1644 Handle<String> name = Factory::LookupAsciiSymbol(Builtins::GetName(id));
John Reck59135872010-11-02 12:39:01 -07001645 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001646 Handle<JSFunction> function
John Reck59135872010-11-02 12:39:01 -07001647 = Handle<JSFunction>(JSFunction::cast(function_object));
Andrei Popescu402d9372010-02-26 13:31:12 +00001648 builtins->set_javascript_builtin(id, *function);
1649 Handle<SharedFunctionInfo> shared
1650 = Handle<SharedFunctionInfo>(function->shared());
1651 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
Iain Merrick75681382010-08-19 15:07:18 +01001652 // Set the code object on the function object.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001653 function->ReplaceCode(function->shared()->code());
Steve Block6ded16b2010-05-10 14:33:55 +01001654 builtins->set_javascript_builtin_code(id, shared->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00001655 }
1656 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00001657}
1658
1659
Steve Blocka7e24c12009-10-30 11:49:00 +00001660bool Genesis::ConfigureGlobalObjects(
1661 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1662 Handle<JSObject> global_proxy(
1663 JSObject::cast(global_context()->global_proxy()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001664 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001665
1666 if (!global_proxy_template.IsEmpty()) {
1667 // Configure the global proxy object.
1668 Handle<ObjectTemplateInfo> proxy_data =
1669 v8::Utils::OpenHandle(*global_proxy_template);
1670 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1671
1672 // Configure the inner global object.
1673 Handle<FunctionTemplateInfo> proxy_constructor(
1674 FunctionTemplateInfo::cast(proxy_data->constructor()));
1675 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1676 Handle<ObjectTemplateInfo> inner_data(
1677 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001678 if (!ConfigureApiObject(inner_global, inner_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001679 }
1680 }
1681
Andrei Popescu402d9372010-02-26 13:31:12 +00001682 SetObjectPrototype(global_proxy, inner_global);
Steve Blocka7e24c12009-10-30 11:49:00 +00001683 return true;
1684}
1685
1686
1687bool Genesis::ConfigureApiObject(Handle<JSObject> object,
1688 Handle<ObjectTemplateInfo> object_template) {
1689 ASSERT(!object_template.is_null());
1690 ASSERT(object->IsInstanceOf(
1691 FunctionTemplateInfo::cast(object_template->constructor())));
1692
1693 bool pending_exception = false;
1694 Handle<JSObject> obj =
1695 Execution::InstantiateObject(object_template, &pending_exception);
1696 if (pending_exception) {
1697 ASSERT(Top::has_pending_exception());
1698 Top::clear_pending_exception();
1699 return false;
1700 }
1701 TransferObject(obj, object);
1702 return true;
1703}
1704
1705
1706void Genesis::TransferNamedProperties(Handle<JSObject> from,
1707 Handle<JSObject> to) {
1708 if (from->HasFastProperties()) {
1709 Handle<DescriptorArray> descs =
1710 Handle<DescriptorArray>(from->map()->instance_descriptors());
1711 for (int i = 0; i < descs->number_of_descriptors(); i++) {
1712 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
1713 switch (details.type()) {
1714 case FIELD: {
1715 HandleScope inner;
1716 Handle<String> key = Handle<String>(descs->GetKey(i));
1717 int index = descs->GetFieldIndex(i);
1718 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
Steve Block1e0659c2011-05-24 12:43:12 +01001719 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001720 break;
1721 }
1722 case CONSTANT_FUNCTION: {
1723 HandleScope inner;
1724 Handle<String> key = Handle<String>(descs->GetKey(i));
1725 Handle<JSFunction> fun =
1726 Handle<JSFunction>(descs->GetConstantFunction(i));
Steve Block1e0659c2011-05-24 12:43:12 +01001727 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001728 break;
1729 }
1730 case CALLBACKS: {
1731 LookupResult result;
1732 to->LocalLookup(descs->GetKey(i), &result);
1733 // If the property is already there we skip it
Andrei Popescu402d9372010-02-26 13:31:12 +00001734 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00001735 HandleScope inner;
Andrei Popescu31002712010-02-23 13:46:05 +00001736 ASSERT(!to->HasFastProperties());
1737 // Add to dictionary.
Steve Blocka7e24c12009-10-30 11:49:00 +00001738 Handle<String> key = Handle<String>(descs->GetKey(i));
Andrei Popescu31002712010-02-23 13:46:05 +00001739 Handle<Object> callbacks(descs->GetCallbacksObject(i));
1740 PropertyDetails d =
1741 PropertyDetails(details.attributes(), CALLBACKS, details.index());
1742 SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00001743 break;
1744 }
1745 case MAP_TRANSITION:
1746 case CONSTANT_TRANSITION:
1747 case NULL_DESCRIPTOR:
1748 // Ignore non-properties.
1749 break;
1750 case NORMAL:
1751 // Do not occur since the from object has fast properties.
1752 case INTERCEPTOR:
1753 // No element in instance descriptors have interceptor type.
1754 UNREACHABLE();
1755 break;
1756 }
1757 }
1758 } else {
1759 Handle<StringDictionary> properties =
1760 Handle<StringDictionary>(from->property_dictionary());
1761 int capacity = properties->Capacity();
1762 for (int i = 0; i < capacity; i++) {
1763 Object* raw_key(properties->KeyAt(i));
1764 if (properties->IsKey(raw_key)) {
1765 ASSERT(raw_key->IsString());
1766 // If the property is already there we skip it.
1767 LookupResult result;
1768 to->LocalLookup(String::cast(raw_key), &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00001769 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00001770 // Set the property.
1771 Handle<String> key = Handle<String>(String::cast(raw_key));
1772 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
1773 if (value->IsJSGlobalPropertyCell()) {
1774 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
1775 }
1776 PropertyDetails details = properties->DetailsAt(i);
Steve Block1e0659c2011-05-24 12:43:12 +01001777 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001778 }
1779 }
1780 }
1781}
1782
1783
1784void Genesis::TransferIndexedProperties(Handle<JSObject> from,
1785 Handle<JSObject> to) {
1786 // Cloning the elements array is sufficient.
1787 Handle<FixedArray> from_elements =
1788 Handle<FixedArray>(FixedArray::cast(from->elements()));
1789 Handle<FixedArray> to_elements = Factory::CopyFixedArray(from_elements);
1790 to->set_elements(*to_elements);
1791}
1792
1793
1794void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
1795 HandleScope outer;
1796
1797 ASSERT(!from->IsJSArray());
1798 ASSERT(!to->IsJSArray());
1799
1800 TransferNamedProperties(from, to);
1801 TransferIndexedProperties(from, to);
1802
1803 // Transfer the prototype (new map is needed).
1804 Handle<Map> old_to_map = Handle<Map>(to->map());
1805 Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map);
1806 new_to_map->set_prototype(from->map()->prototype());
1807 to->set_map(*new_to_map);
1808}
1809
1810
1811void Genesis::MakeFunctionInstancePrototypeWritable() {
1812 // Make a new function map so all future functions
1813 // will have settable and enumerable prototype properties.
1814 HandleScope scope;
1815
1816 Handle<DescriptorArray> function_map_descriptors =
Steve Block6ded16b2010-05-10 14:33:55 +01001817 ComputeFunctionInstanceDescriptor(ADD_WRITEABLE_PROTOTYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001818 Handle<Map> fm = Factory::CopyMapDropDescriptors(Top::function_map());
1819 fm->set_instance_descriptors(*function_map_descriptors);
Steve Block6ded16b2010-05-10 14:33:55 +01001820 fm->set_function_with_prototype(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001821 Top::context()->global_context()->set_function_map(*fm);
1822}
1823
1824
Steve Blocka7e24c12009-10-30 11:49:00 +00001825Genesis::Genesis(Handle<Object> global_object,
1826 v8::Handle<v8::ObjectTemplate> global_template,
1827 v8::ExtensionConfiguration* extensions) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001828 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00001829 // If V8 isn't running and cannot be initialized, just return.
1830 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
1831
1832 // Before creating the roots we must save the context and restore it
1833 // on all function exits.
1834 HandleScope scope;
Andrei Popescu31002712010-02-23 13:46:05 +00001835 SaveContext saved_context;
Steve Blocka7e24c12009-10-30 11:49:00 +00001836
Andrei Popescu31002712010-02-23 13:46:05 +00001837 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
1838 if (!new_context.is_null()) {
1839 global_context_ =
1840 Handle<Context>::cast(GlobalHandles::Create(*new_context));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001841 AddToWeakGlobalContextList(*global_context_);
Andrei Popescu31002712010-02-23 13:46:05 +00001842 Top::set_context(*global_context_);
1843 i::Counters::contexts_created_by_snapshot.Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00001844 JSFunction* empty_function =
Steve Block1e0659c2011-05-24 12:43:12 +01001845 JSFunction::cast(global_context_->function_map()->prototype());
Andrei Popescu31002712010-02-23 13:46:05 +00001846 empty_function_ = Handle<JSFunction>(empty_function);
Andrei Popescu402d9372010-02-26 13:31:12 +00001847 Handle<GlobalObject> inner_global;
Andrei Popescu31002712010-02-23 13:46:05 +00001848 Handle<JSGlobalProxy> global_proxy =
1849 CreateNewGlobals(global_template,
1850 global_object,
Andrei Popescu402d9372010-02-26 13:31:12 +00001851 &inner_global);
1852
Andrei Popescu31002712010-02-23 13:46:05 +00001853 HookUpGlobalProxy(inner_global, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00001854 HookUpInnerGlobal(inner_global);
1855
Andrei Popescu31002712010-02-23 13:46:05 +00001856 if (!ConfigureGlobalObjects(global_template)) return;
1857 } else {
1858 // We get here if there was no context snapshot.
1859 CreateRoots();
1860 Handle<JSFunction> empty_function = CreateEmptyFunction();
1861 Handle<GlobalObject> inner_global;
1862 Handle<JSGlobalProxy> global_proxy =
1863 CreateNewGlobals(global_template, global_object, &inner_global);
1864 HookUpGlobalProxy(inner_global, global_proxy);
1865 InitializeGlobal(inner_global, empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +01001866 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001867 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01001868 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001869
Andrei Popescu31002712010-02-23 13:46:05 +00001870 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00001871
Andrei Popescu31002712010-02-23 13:46:05 +00001872 if (!ConfigureGlobalObjects(global_template)) return;
1873 i::Counters::contexts_created_from_scratch.Increment();
1874 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001875
1876 result_ = global_context_;
1877}
1878
1879
1880// Support for thread preemption.
1881
1882// Reserve space for statics needing saving and restoring.
1883int Bootstrapper::ArchiveSpacePerThread() {
Andrei Popescu31002712010-02-23 13:46:05 +00001884 return BootstrapperActive::ArchiveSpacePerThread();
Steve Blocka7e24c12009-10-30 11:49:00 +00001885}
1886
1887
1888// Archive statics that are thread local.
1889char* Bootstrapper::ArchiveState(char* to) {
Andrei Popescu31002712010-02-23 13:46:05 +00001890 return BootstrapperActive::ArchiveState(to);
Steve Blocka7e24c12009-10-30 11:49:00 +00001891}
1892
1893
1894// Restore statics that are thread local.
1895char* Bootstrapper::RestoreState(char* from) {
Andrei Popescu31002712010-02-23 13:46:05 +00001896 return BootstrapperActive::RestoreState(from);
Steve Blocka7e24c12009-10-30 11:49:00 +00001897}
1898
1899
1900// Called when the top-level V8 mutex is destroyed.
1901void Bootstrapper::FreeThreadResources() {
Andrei Popescu31002712010-02-23 13:46:05 +00001902 ASSERT(!BootstrapperActive::IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00001903}
1904
1905
1906// Reserve space for statics needing saving and restoring.
Andrei Popescu31002712010-02-23 13:46:05 +00001907int BootstrapperActive::ArchiveSpacePerThread() {
1908 return sizeof(nesting_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001909}
1910
1911
1912// Archive statics that are thread local.
Andrei Popescu31002712010-02-23 13:46:05 +00001913char* BootstrapperActive::ArchiveState(char* to) {
1914 *reinterpret_cast<int*>(to) = nesting_;
1915 nesting_ = 0;
1916 return to + sizeof(nesting_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001917}
1918
1919
1920// Restore statics that are thread local.
Andrei Popescu31002712010-02-23 13:46:05 +00001921char* BootstrapperActive::RestoreState(char* from) {
1922 nesting_ = *reinterpret_cast<int*>(from);
1923 return from + sizeof(nesting_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001924}
1925
1926} } // namespace v8::internal