blob: 6e565e08fd1738ed77bf20d66900fe09be5a56a9 [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"
Steve Blockd0582a62009-12-15 09:54:21 +000039#include "snapshot.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040
41namespace v8 {
42namespace internal {
43
44// A SourceCodeCache uses a FixedArray to store pairs of
45// (AsciiString*, JSFunction*), mapping names of native code files
46// (runtime.js, etc.) to precompiled functions. Instead of mapping
47// names to functions it might make sense to let the JS2C tool
48// generate an index for each native JS file.
49class SourceCodeCache BASE_EMBEDDED {
50 public:
51 explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
52
53 void Initialize(bool create_heap_objects) {
54 cache_ = create_heap_objects ? Heap::empty_fixed_array() : NULL;
55 }
56
57 void Iterate(ObjectVisitor* v) {
58 v->VisitPointer(bit_cast<Object**, FixedArray**>(&cache_));
59 }
60
61
62 bool Lookup(Vector<const char> name, Handle<JSFunction>* handle) {
63 for (int i = 0; i < cache_->length(); i+=2) {
64 SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i));
65 if (str->IsEqualTo(name)) {
66 *handle = Handle<JSFunction>(JSFunction::cast(cache_->get(i + 1)));
67 return true;
68 }
69 }
70 return false;
71 }
72
73
74 void Add(Vector<const char> name, Handle<JSFunction> fun) {
75 ASSERT(fun->IsBoilerplate());
76 HandleScope scope;
77 int length = cache_->length();
78 Handle<FixedArray> new_array =
79 Factory::NewFixedArray(length + 2, TENURED);
80 cache_->CopyTo(0, *new_array, 0, cache_->length());
81 cache_ = *new_array;
82 Handle<String> str = Factory::NewStringFromAscii(name, TENURED);
83 cache_->set(length, *str);
84 cache_->set(length + 1, *fun);
85 Script::cast(fun->shared()->script())->set_type(Smi::FromInt(type_));
86 }
87
88 private:
89 Script::Type type_;
90 FixedArray* cache_;
91 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
92};
93
Steve Blocka7e24c12009-10-30 11:49:00 +000094static SourceCodeCache extensions_cache(Script::TYPE_EXTENSION);
Steve Blockd0582a62009-12-15 09:54:21 +000095// This is for delete, not delete[].
96static List<char*>* delete_these_non_arrays_on_tear_down = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +000097// This is for delete[]
98static List<char*>* delete_these_arrays_on_tear_down = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +000099
100
101NativesExternalStringResource::NativesExternalStringResource(const char* source)
102 : data_(source), length_(StrLength(source)) {
103 if (delete_these_non_arrays_on_tear_down == NULL) {
104 delete_these_non_arrays_on_tear_down = new List<char*>(2);
105 }
106 // The resources are small objects and we only make a fixed number of
107 // them, but let's clean them up on exit for neatness.
108 delete_these_non_arrays_on_tear_down->
109 Add(reinterpret_cast<char*>(this));
110}
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
112
113Handle<String> Bootstrapper::NativesSourceLookup(int index) {
114 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
115 if (Heap::natives_source_cache()->get(index)->IsUndefined()) {
Steve Blockd0582a62009-12-15 09:54:21 +0000116 if (!Snapshot::IsEnabled() || FLAG_new_snapshot) {
117 // We can use external strings for the natives.
118 NativesExternalStringResource* resource =
119 new NativesExternalStringResource(
120 Natives::GetScriptSource(index).start());
121 Handle<String> source_code =
122 Factory::NewExternalStringFromAscii(resource);
123 Heap::natives_source_cache()->set(index, *source_code);
124 } else {
125 // Old snapshot code can't cope with external strings at all.
126 Handle<String> source_code =
127 Factory::NewStringFromAscii(Natives::GetScriptSource(index));
128 Heap::natives_source_cache()->set(index, *source_code);
129 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 }
131 Handle<Object> cached_source(Heap::natives_source_cache()->get(index));
132 return Handle<String>::cast(cached_source);
133}
134
135
Steve Blocka7e24c12009-10-30 11:49:00 +0000136void Bootstrapper::Initialize(bool create_heap_objects) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 extensions_cache.Initialize(create_heap_objects);
138}
139
140
Leon Clarkee46be812010-01-19 14:06:41 +0000141char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
142 char* memory = new char[bytes];
143 if (memory != NULL) {
144 if (delete_these_arrays_on_tear_down == NULL) {
145 delete_these_arrays_on_tear_down = new List<char*>(2);
146 }
147 delete_these_arrays_on_tear_down->Add(memory);
148 }
149 return memory;
150}
151
152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153void Bootstrapper::TearDown() {
Steve Blockd0582a62009-12-15 09:54:21 +0000154 if (delete_these_non_arrays_on_tear_down != NULL) {
155 int len = delete_these_non_arrays_on_tear_down->length();
156 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
157 for (int i = 0; i < len; i++) {
158 delete delete_these_non_arrays_on_tear_down->at(i);
Leon Clarkee46be812010-01-19 14:06:41 +0000159 delete_these_non_arrays_on_tear_down->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000160 }
161 delete delete_these_non_arrays_on_tear_down;
162 delete_these_non_arrays_on_tear_down = NULL;
163 }
164
Leon Clarkee46be812010-01-19 14:06:41 +0000165 if (delete_these_arrays_on_tear_down != NULL) {
166 int len = delete_these_arrays_on_tear_down->length();
167 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
168 for (int i = 0; i < len; i++) {
169 delete[] delete_these_arrays_on_tear_down->at(i);
170 delete_these_arrays_on_tear_down->at(i) = NULL;
171 }
172 delete delete_these_arrays_on_tear_down;
173 delete_these_arrays_on_tear_down = NULL;
174 }
175
Andrei Popescu31002712010-02-23 13:46:05 +0000176 extensions_cache.Initialize(false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000177}
178
179
Steve Blocka7e24c12009-10-30 11:49:00 +0000180class Genesis BASE_EMBEDDED {
181 public:
182 Genesis(Handle<Object> global_object,
183 v8::Handle<v8::ObjectTemplate> global_template,
184 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000185 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000186
187 Handle<Context> result() { return result_; }
188
189 Genesis* previous() { return previous_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000190
191 private:
192 Handle<Context> global_context_;
193
194 // There may be more than one active genesis object: When GC is
195 // triggered during environment creation there may be weak handle
196 // processing callbacks which may create new environments.
197 Genesis* previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
199 Handle<Context> global_context() { return global_context_; }
200
Andrei Popescu31002712010-02-23 13:46:05 +0000201 // Creates some basic objects. Used for creating a context from scratch.
202 void CreateRoots();
203 // Creates the empty function. Used for creating a context from scratch.
204 Handle<JSFunction> CreateEmptyFunction();
205 // Creates the global objects using the global and the template passed in
206 // through the API. We call this regardless of whether we are building a
207 // context from scratch or using a deserialized one from the partial snapshot
208 // but in the latter case we don't use the objects it produces directly, as
209 // we have to used the deserialized ones that are linked together with the
210 // rest of the context snapshot.
211 Handle<JSGlobalProxy> CreateNewGlobals(
212 v8::Handle<v8::ObjectTemplate> global_template,
213 Handle<Object> global_object,
214 Handle<GlobalObject>* global_proxy_out);
215 // Hooks the given global proxy into the context. If the context was created
216 // by deserialization then this will unhook the global proxy that was
217 // deserialized, leaving the GC to pick it up.
218 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
219 Handle<JSGlobalProxy> global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +0000220 // Similarly, we want to use the inner global that has been created by the
221 // templates passed through the API. The inner global from the snapshot is
222 // detached from the other objects in the snapshot.
223 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
Andrei Popescu31002712010-02-23 13:46:05 +0000224 // New context initialization. Used for creating a context from scratch.
225 void InitializeGlobal(Handle<GlobalObject> inner_global,
226 Handle<JSFunction> empty_function);
227 // Installs the contents of the native .js files on the global objects.
228 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 void InstallNativeFunctions();
230 bool InstallNatives();
Andrei Popescu31002712010-02-23 13:46:05 +0000231 // Used both for deserialized and from-scratch contexts to add the extensions
232 // provided.
233 static bool InstallExtensions(Handle<Context> global_context,
234 v8::ExtensionConfiguration* extensions);
235 static bool InstallExtension(const char* name);
236 static bool InstallExtension(v8::RegisteredExtension* current);
237 static void InstallSpecialObjects(Handle<Context> global_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000238 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000239 bool ConfigureApiObject(Handle<JSObject> object,
240 Handle<ObjectTemplateInfo> object_template);
241 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
242
243 // Migrates all properties from the 'from' object to the 'to'
244 // object and overrides the prototype in 'to' with the one from
245 // 'from'.
246 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
247 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
248 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
249
250 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
251 bool make_prototype_read_only,
252 bool make_prototype_enumerable = false);
253 void MakeFunctionInstancePrototypeWritable();
254
255 void AddSpecialFunction(Handle<JSObject> prototype,
256 const char* name,
257 Handle<Code> code);
258
259 void BuildSpecialFunctionTable();
260
261 static bool CompileBuiltin(int index);
262 static bool CompileNative(Vector<const char> name, Handle<String> source);
263 static bool CompileScriptCached(Vector<const char> name,
264 Handle<String> source,
265 SourceCodeCache* cache,
266 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000267 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000268 bool use_runtime_context);
269
270 Handle<Context> result_;
Andrei Popescu31002712010-02-23 13:46:05 +0000271 Handle<JSFunction> empty_function_;
272 BootstrapperActive active_;
273 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000274};
275
Steve Blocka7e24c12009-10-30 11:49:00 +0000276
277void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 extensions_cache.Iterate(v);
Steve Blockd0582a62009-12-15 09:54:21 +0000279 v->Synchronize("Extensions");
Steve Blocka7e24c12009-10-30 11:49:00 +0000280}
281
282
Steve Blocka7e24c12009-10-30 11:49:00 +0000283Handle<Context> Bootstrapper::CreateEnvironment(
284 Handle<Object> global_object,
285 v8::Handle<v8::ObjectTemplate> global_template,
286 v8::ExtensionConfiguration* extensions) {
Andrei Popescu31002712010-02-23 13:46:05 +0000287 HandleScope scope;
288 Handle<Context> env;
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 Genesis genesis(global_object, global_template, extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000290 env = genesis.result();
291 if (!env.is_null()) {
292 if (InstallExtensions(env, extensions)) {
293 return env;
294 }
295 }
296 return Handle<Context>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000297}
298
299
300static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
301 // object.__proto__ = proto;
302 Handle<Map> old_to_map = Handle<Map>(object->map());
303 Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map);
304 new_to_map->set_prototype(*proto);
305 object->set_map(*new_to_map);
306}
307
308
309void Bootstrapper::DetachGlobal(Handle<Context> env) {
310 JSGlobalProxy::cast(env->global_proxy())->set_context(*Factory::null_value());
311 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
312 Factory::null_value());
313 env->set_global_proxy(env->global());
314 env->global()->set_global_receiver(env->global());
315}
316
317
Steve Blocka7e24c12009-10-30 11:49:00 +0000318static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
319 const char* name,
320 InstanceType type,
321 int instance_size,
322 Handle<JSObject> prototype,
323 Builtins::Name call,
324 bool is_ecma_native) {
325 Handle<String> symbol = Factory::LookupAsciiSymbol(name);
326 Handle<Code> call_code = Handle<Code>(Builtins::builtin(call));
327 Handle<JSFunction> function =
328 Factory::NewFunctionWithPrototype(symbol,
329 type,
330 instance_size,
331 prototype,
332 call_code,
333 is_ecma_native);
334 SetProperty(target, symbol, function, DONT_ENUM);
335 if (is_ecma_native) {
336 function->shared()->set_instance_class_name(*symbol);
337 }
338 return function;
339}
340
341
342Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
343 bool make_prototype_read_only,
344 bool make_prototype_enumerable) {
345 Handle<DescriptorArray> result = Factory::empty_descriptor_array();
346
347 // Add prototype.
348 PropertyAttributes attributes = static_cast<PropertyAttributes>(
349 (make_prototype_enumerable ? 0 : DONT_ENUM)
350 | DONT_DELETE
351 | (make_prototype_read_only ? READ_ONLY : 0));
352 result =
353 Factory::CopyAppendProxyDescriptor(
354 result,
355 Factory::prototype_symbol(),
356 Factory::NewProxy(&Accessors::FunctionPrototype),
357 attributes);
358
359 attributes =
360 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
361 // Add length.
362 result =
363 Factory::CopyAppendProxyDescriptor(
364 result,
365 Factory::length_symbol(),
366 Factory::NewProxy(&Accessors::FunctionLength),
367 attributes);
368
369 // Add name.
370 result =
371 Factory::CopyAppendProxyDescriptor(
372 result,
373 Factory::name_symbol(),
374 Factory::NewProxy(&Accessors::FunctionName),
375 attributes);
376
377 // Add arguments.
378 result =
379 Factory::CopyAppendProxyDescriptor(
380 result,
381 Factory::arguments_symbol(),
382 Factory::NewProxy(&Accessors::FunctionArguments),
383 attributes);
384
385 // Add caller.
386 result =
387 Factory::CopyAppendProxyDescriptor(
388 result,
389 Factory::caller_symbol(),
390 Factory::NewProxy(&Accessors::FunctionCaller),
391 attributes);
392
393 return result;
394}
395
396
Andrei Popescu31002712010-02-23 13:46:05 +0000397Handle<JSFunction> Genesis::CreateEmptyFunction() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000398 // Allocate the map for function instances.
399 Handle<Map> fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
400 global_context()->set_function_instance_map(*fm);
401 // Please note that the prototype property for function instances must be
402 // writable.
403 Handle<DescriptorArray> function_map_descriptors =
404 ComputeFunctionInstanceDescriptor(false, false);
405 fm->set_instance_descriptors(*function_map_descriptors);
406
407 // Allocate the function map first and then patch the prototype later
408 fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
409 global_context()->set_function_map(*fm);
410 function_map_descriptors = ComputeFunctionInstanceDescriptor(true);
411 fm->set_instance_descriptors(*function_map_descriptors);
412
413 Handle<String> object_name = Handle<String>(Heap::Object_symbol());
414
415 { // --- O b j e c t ---
416 Handle<JSFunction> object_fun =
417 Factory::NewFunction(object_name, Factory::null_value());
418 Handle<Map> object_function_map =
419 Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
420 object_fun->set_initial_map(*object_function_map);
421 object_function_map->set_constructor(*object_fun);
422
423 global_context()->set_object_function(*object_fun);
424
425 // Allocate a new prototype for the object function.
426 Handle<JSObject> prototype = Factory::NewJSObject(Top::object_function(),
427 TENURED);
428
429 global_context()->set_initial_object_prototype(*prototype);
430 SetPrototype(object_fun, prototype);
431 object_function_map->
432 set_instance_descriptors(Heap::empty_descriptor_array());
433 }
434
435 // Allocate the empty function as the prototype for function ECMAScript
436 // 262 15.3.4.
437 Handle<String> symbol = Factory::LookupAsciiSymbol("Empty");
438 Handle<JSFunction> empty_function =
439 Factory::NewFunction(symbol, Factory::null_value());
440
Andrei Popescu31002712010-02-23 13:46:05 +0000441 // --- E m p t y ---
442 Handle<Code> code =
443 Handle<Code>(Builtins::builtin(Builtins::EmptyFunction));
444 empty_function->set_code(*code);
445 Handle<String> source = Factory::NewStringFromAscii(CStrVector("() {}"));
446 Handle<Script> script = Factory::NewScript(source);
447 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
448 empty_function->shared()->set_script(*script);
449 empty_function->shared()->set_start_position(0);
450 empty_function->shared()->set_end_position(source->length());
451 empty_function->shared()->DontAdaptArguments();
452 global_context()->function_map()->set_prototype(*empty_function);
453 global_context()->function_instance_map()->set_prototype(*empty_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000454
Andrei Popescu31002712010-02-23 13:46:05 +0000455 // Allocate the function map first and then patch the prototype later
456 Handle<Map> empty_fm = Factory::CopyMapDropDescriptors(fm);
457 empty_fm->set_instance_descriptors(*function_map_descriptors);
458 empty_fm->set_prototype(global_context()->object_function()->prototype());
459 empty_function->set_map(*empty_fm);
460 return empty_function;
461}
462
463
464void Genesis::CreateRoots() {
465 // Allocate the global context FixedArray first and then patch the
466 // closure and extension object later (we need the empty function
467 // and the global object, but in order to create those, we need the
468 // global context).
469 global_context_ =
470 Handle<Context>::cast(
471 GlobalHandles::Create(*Factory::NewGlobalContext()));
472 Top::set_context(*global_context());
473
474 // Allocate the message listeners object.
475 {
476 v8::NeanderArray listeners;
477 global_context()->set_message_listeners(*listeners.value());
478 }
479}
480
481
482Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
483 v8::Handle<v8::ObjectTemplate> global_template,
484 Handle<Object> global_object,
485 Handle<GlobalObject>* inner_global_out) {
486 // The argument global_template aka data is an ObjectTemplateInfo.
487 // It has a constructor pointer that points at global_constructor which is a
488 // FunctionTemplateInfo.
489 // The global_constructor is used to create or reinitialize the global_proxy.
490 // The global_constructor also has a prototype_template pointer that points at
491 // js_global_template which is an ObjectTemplateInfo.
492 // That in turn has a constructor pointer that points at
493 // js_global_constructor which is a FunctionTemplateInfo.
494 // js_global_constructor is used to make js_global_function
495 // js_global_function is used to make the new inner_global.
496 //
497 // --- G l o b a l ---
498 // Step 1: Create a fresh inner JSGlobalObject.
499 Handle<JSFunction> js_global_function;
500 Handle<ObjectTemplateInfo> js_global_template;
501 if (!global_template.IsEmpty()) {
502 // Get prototype template of the global_template.
503 Handle<ObjectTemplateInfo> data =
504 v8::Utils::OpenHandle(*global_template);
505 Handle<FunctionTemplateInfo> global_constructor =
506 Handle<FunctionTemplateInfo>(
507 FunctionTemplateInfo::cast(data->constructor()));
508 Handle<Object> proto_template(global_constructor->prototype_template());
509 if (!proto_template->IsUndefined()) {
510 js_global_template =
511 Handle<ObjectTemplateInfo>::cast(proto_template);
512 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 }
514
Andrei Popescu31002712010-02-23 13:46:05 +0000515 if (js_global_template.is_null()) {
516 Handle<String> name = Handle<String>(Heap::empty_symbol());
517 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
518 js_global_function =
519 Factory::NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
Andrei Popescu402d9372010-02-26 13:31:12 +0000520 JSGlobalObject::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000521 // Change the constructor property of the prototype of the
522 // hidden global function to refer to the Object function.
523 Handle<JSObject> prototype =
524 Handle<JSObject>(
525 JSObject::cast(js_global_function->instance_prototype()));
526 SetProperty(prototype, Factory::constructor_symbol(),
527 Top::object_function(), NONE);
528 } else {
529 Handle<FunctionTemplateInfo> js_global_constructor(
530 FunctionTemplateInfo::cast(js_global_template->constructor()));
531 js_global_function =
532 Factory::CreateApiFunction(js_global_constructor,
533 Factory::InnerGlobalObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 }
535
Andrei Popescu31002712010-02-23 13:46:05 +0000536 js_global_function->initial_map()->set_is_hidden_prototype();
537 Handle<GlobalObject> inner_global =
538 Factory::NewGlobalObject(js_global_function);
539 if (inner_global_out != NULL) {
540 *inner_global_out = inner_global;
541 }
542
543 // Step 2: create or re-initialize the global proxy object.
544 Handle<JSFunction> global_proxy_function;
545 if (global_template.IsEmpty()) {
546 Handle<String> name = Handle<String>(Heap::empty_symbol());
547 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
548 global_proxy_function =
549 Factory::NewFunction(name, JS_GLOBAL_PROXY_TYPE,
550 JSGlobalProxy::kSize, code, true);
551 } else {
552 Handle<ObjectTemplateInfo> data =
553 v8::Utils::OpenHandle(*global_template);
554 Handle<FunctionTemplateInfo> global_constructor(
555 FunctionTemplateInfo::cast(data->constructor()));
556 global_proxy_function =
557 Factory::CreateApiFunction(global_constructor,
558 Factory::OuterGlobalObject);
559 }
560
561 Handle<String> global_name = Factory::LookupAsciiSymbol("global");
562 global_proxy_function->shared()->set_instance_class_name(*global_name);
563 global_proxy_function->initial_map()->set_is_access_check_needed(true);
564
565 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
566 // Return the global proxy.
567
568 if (global_object.location() != NULL) {
569 ASSERT(global_object->IsJSGlobalProxy());
570 return ReinitializeJSGlobalProxy(
571 global_proxy_function,
572 Handle<JSGlobalProxy>::cast(global_object));
573 } else {
574 return Handle<JSGlobalProxy>::cast(
575 Factory::NewJSObject(global_proxy_function, TENURED));
576 }
577}
578
579
580void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
581 Handle<JSGlobalProxy> global_proxy) {
582 // Set the global context for the global object.
583 inner_global->set_global_context(*global_context());
584 inner_global->set_global_receiver(*global_proxy);
585 global_proxy->set_context(*global_context());
586 global_context()->set_global_proxy(*global_proxy);
587}
588
589
Andrei Popescu402d9372010-02-26 13:31:12 +0000590void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
591 Handle<GlobalObject> inner_global_from_snapshot(
592 GlobalObject::cast(global_context_->extension()));
593 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
594 global_context_->set_extension(*inner_global);
595 global_context_->set_global(*inner_global);
596 global_context_->set_security_token(*inner_global);
597 static const PropertyAttributes attributes =
598 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
599 ForceSetProperty(builtins_global,
600 Factory::LookupAsciiSymbol("global"),
601 inner_global,
602 attributes);
603 // Setup the reference from the global object to the builtins object.
604 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
605 TransferNamedProperties(inner_global_from_snapshot, inner_global);
606 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
607}
608
609
610// This is only called if we are not using snapshots. The equivalent
611// work in the snapshot case is done in HookUpInnerGlobal.
Andrei Popescu31002712010-02-23 13:46:05 +0000612void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
613 Handle<JSFunction> empty_function) {
614 // --- G l o b a l C o n t e x t ---
615 // Use the empty function as closure (no scope info).
616 global_context()->set_closure(*empty_function);
617 global_context()->set_fcontext(*global_context());
618 global_context()->set_previous(NULL);
619 // Set extension and global object.
620 global_context()->set_extension(*inner_global);
621 global_context()->set_global(*inner_global);
622 // Security setup: Set the security token of the global object to
623 // its the inner global. This makes the security check between two
624 // different contexts fail by default even in case of global
625 // object reinitialization.
626 global_context()->set_security_token(*inner_global);
627
628 Handle<String> object_name = Handle<String>(Heap::Object_symbol());
629 SetProperty(inner_global, object_name, Top::object_function(), DONT_ENUM);
630
Steve Blocka7e24c12009-10-30 11:49:00 +0000631 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
632
633 // Install global Function object
634 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
635 empty_function, Builtins::Illegal, true); // ECMA native.
636
637 { // --- A r r a y ---
638 Handle<JSFunction> array_function =
639 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
640 Top::initial_object_prototype(), Builtins::ArrayCode,
641 true);
642 array_function->shared()->set_construct_stub(
643 Builtins::builtin(Builtins::ArrayConstructCode));
644 array_function->shared()->DontAdaptArguments();
645
646 // This seems a bit hackish, but we need to make sure Array.length
647 // is 1.
648 array_function->shared()->set_length(1);
649 Handle<DescriptorArray> array_descriptors =
650 Factory::CopyAppendProxyDescriptor(
651 Factory::empty_descriptor_array(),
652 Factory::length_symbol(),
653 Factory::NewProxy(&Accessors::ArrayLength),
654 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
655
656 // Cache the fast JavaScript array map
657 global_context()->set_js_array_map(array_function->initial_map());
658 global_context()->js_array_map()->set_instance_descriptors(
659 *array_descriptors);
660 // array_function is used internally. JS code creating array object should
661 // search for the 'Array' property on the global object and use that one
662 // as the constructor. 'Array' property on a global object can be
663 // overwritten by JS code.
664 global_context()->set_array_function(*array_function);
665 }
666
667 { // --- N u m b e r ---
668 Handle<JSFunction> number_fun =
669 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
670 Top::initial_object_prototype(), Builtins::Illegal,
671 true);
672 global_context()->set_number_function(*number_fun);
673 }
674
675 { // --- B o o l e a n ---
676 Handle<JSFunction> boolean_fun =
677 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
678 Top::initial_object_prototype(), Builtins::Illegal,
679 true);
680 global_context()->set_boolean_function(*boolean_fun);
681 }
682
683 { // --- S t r i n g ---
684 Handle<JSFunction> string_fun =
685 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
686 Top::initial_object_prototype(), Builtins::Illegal,
687 true);
688 global_context()->set_string_function(*string_fun);
689 // Add 'length' property to strings.
690 Handle<DescriptorArray> string_descriptors =
691 Factory::CopyAppendProxyDescriptor(
692 Factory::empty_descriptor_array(),
693 Factory::length_symbol(),
694 Factory::NewProxy(&Accessors::StringLength),
695 static_cast<PropertyAttributes>(DONT_ENUM |
696 DONT_DELETE |
697 READ_ONLY));
698
699 Handle<Map> string_map =
700 Handle<Map>(global_context()->string_function()->initial_map());
701 string_map->set_instance_descriptors(*string_descriptors);
702 }
703
704 { // --- D a t e ---
705 // Builtin functions for Date.prototype.
706 Handle<JSFunction> date_fun =
707 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
708 Top::initial_object_prototype(), Builtins::Illegal,
709 true);
710
711 global_context()->set_date_function(*date_fun);
712 }
713
714
715 { // -- R e g E x p
716 // Builtin functions for RegExp.prototype.
717 Handle<JSFunction> regexp_fun =
718 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
719 Top::initial_object_prototype(), Builtins::Illegal,
720 true);
721
722 global_context()->set_regexp_function(*regexp_fun);
723 }
724
725 { // -- J S O N
726 Handle<String> name = Factory::NewStringFromAscii(CStrVector("JSON"));
727 Handle<JSFunction> cons = Factory::NewFunction(
728 name,
729 Factory::the_hole_value());
730 cons->SetInstancePrototype(global_context()->initial_object_prototype());
731 cons->SetInstanceClassName(*name);
732 Handle<JSObject> json_object = Factory::NewJSObject(cons, TENURED);
733 ASSERT(json_object->IsJSObject());
734 SetProperty(global, name, json_object, DONT_ENUM);
735 global_context()->set_json_object(*json_object);
736 }
737
738 { // --- arguments_boilerplate_
739 // Make sure we can recognize argument objects at runtime.
740 // This is done by introducing an anonymous function with
741 // class_name equals 'Arguments'.
742 Handle<String> symbol = Factory::LookupAsciiSymbol("Arguments");
743 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
744 Handle<JSObject> prototype =
745 Handle<JSObject>(
746 JSObject::cast(global_context()->object_function()->prototype()));
747
748 Handle<JSFunction> function =
749 Factory::NewFunctionWithPrototype(symbol,
750 JS_OBJECT_TYPE,
751 JSObject::kHeaderSize,
752 prototype,
753 code,
754 false);
755 ASSERT(!function->has_initial_map());
756 function->shared()->set_instance_class_name(*symbol);
757 function->shared()->set_expected_nof_properties(2);
758 Handle<JSObject> result = Factory::NewJSObject(function);
759
760 global_context()->set_arguments_boilerplate(*result);
761 // Note: callee must be added as the first property and
762 // length must be added as the second property.
763 SetProperty(result, Factory::callee_symbol(),
764 Factory::undefined_value(),
765 DONT_ENUM);
766 SetProperty(result, Factory::length_symbol(),
767 Factory::undefined_value(),
768 DONT_ENUM);
769
770#ifdef DEBUG
771 LookupResult lookup;
772 result->LocalLookup(Heap::callee_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +0000773 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Blocka7e24c12009-10-30 11:49:00 +0000774 ASSERT(lookup.GetFieldIndex() == Heap::arguments_callee_index);
775
776 result->LocalLookup(Heap::length_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +0000777 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Blocka7e24c12009-10-30 11:49:00 +0000778 ASSERT(lookup.GetFieldIndex() == Heap::arguments_length_index);
779
780 ASSERT(result->map()->inobject_properties() > Heap::arguments_callee_index);
781 ASSERT(result->map()->inobject_properties() > Heap::arguments_length_index);
782
783 // Check the state of the object.
784 ASSERT(result->HasFastProperties());
785 ASSERT(result->HasFastElements());
786#endif
787 }
788
789 { // --- context extension
790 // Create a function for the context extension objects.
791 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
792 Handle<JSFunction> context_extension_fun =
793 Factory::NewFunction(Factory::empty_symbol(),
794 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
795 JSObject::kHeaderSize,
796 code,
797 true);
798
799 Handle<String> name = Factory::LookupAsciiSymbol("context_extension");
800 context_extension_fun->shared()->set_instance_class_name(*name);
801 global_context()->set_context_extension_function(*context_extension_fun);
802 }
803
804
805 {
806 // Setup the call-as-function delegate.
807 Handle<Code> code =
808 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsFunction));
809 Handle<JSFunction> delegate =
810 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE,
811 JSObject::kHeaderSize, code, true);
812 global_context()->set_call_as_function_delegate(*delegate);
813 delegate->shared()->DontAdaptArguments();
814 }
815
816 {
817 // Setup the call-as-constructor delegate.
818 Handle<Code> code =
819 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsConstructor));
820 Handle<JSFunction> delegate =
821 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE,
822 JSObject::kHeaderSize, code, true);
823 global_context()->set_call_as_constructor_delegate(*delegate);
824 delegate->shared()->DontAdaptArguments();
825 }
826
827 global_context()->set_special_function_table(Heap::empty_fixed_array());
828
829 // Initialize the out of memory slot.
830 global_context()->set_out_of_memory(Heap::false_value());
831
832 // Initialize the data slot.
833 global_context()->set_data(Heap::undefined_value());
834}
835
836
837bool Genesis::CompileBuiltin(int index) {
838 Vector<const char> name = Natives::GetScriptName(index);
839 Handle<String> source_code = Bootstrapper::NativesSourceLookup(index);
840 return CompileNative(name, source_code);
841}
842
843
844bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
845 HandleScope scope;
846#ifdef ENABLE_DEBUGGER_SUPPORT
847 Debugger::set_compiling_natives(true);
848#endif
Andrei Popescu31002712010-02-23 13:46:05 +0000849 bool result = CompileScriptCached(name,
850 source,
851 NULL,
852 NULL,
853 Handle<Context>(Top::context()),
854 true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000855 ASSERT(Top::has_pending_exception() != result);
856 if (!result) Top::clear_pending_exception();
857#ifdef ENABLE_DEBUGGER_SUPPORT
858 Debugger::set_compiling_natives(false);
859#endif
860 return result;
861}
862
863
864bool Genesis::CompileScriptCached(Vector<const char> name,
865 Handle<String> source,
866 SourceCodeCache* cache,
867 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000868 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 bool use_runtime_context) {
870 HandleScope scope;
871 Handle<JSFunction> boilerplate;
872
873 // If we can't find the function in the cache, we compile a new
874 // function and insert it into the cache.
Andrei Popescu31002712010-02-23 13:46:05 +0000875 if (cache == NULL || !cache->Lookup(name, &boilerplate)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000876 ASSERT(source->IsAsciiRepresentation());
877 Handle<String> script_name = Factory::NewStringFromUtf8(name);
Andrei Popescu31002712010-02-23 13:46:05 +0000878 boilerplate = Compiler::Compile(
879 source,
880 script_name,
881 0,
882 0,
883 extension,
884 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +0000885 Handle<String>::null(),
Andrei Popescu31002712010-02-23 13:46:05 +0000886 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000887 if (boilerplate.is_null()) return false;
Andrei Popescu31002712010-02-23 13:46:05 +0000888 if (cache != NULL) cache->Add(name, boilerplate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000889 }
890
891 // Setup the function context. Conceptually, we should clone the
892 // function before overwriting the context but since we're in a
893 // single-threaded environment it is not strictly necessary.
Andrei Popescu31002712010-02-23 13:46:05 +0000894 ASSERT(top_context->IsGlobalContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000895 Handle<Context> context =
896 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +0000897 ? Handle<Context>(top_context->runtime_context())
898 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000899 Handle<JSFunction> fun =
900 Factory::NewFunctionFromBoilerplate(boilerplate, context);
901
Leon Clarke4515c472010-02-03 11:58:03 +0000902 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +0000903 // object as the receiver. Provide no parameters.
904 Handle<Object> receiver =
905 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +0000906 ? top_context->builtins()
907 : top_context->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000908 bool has_pending_exception;
909 Handle<Object> result =
910 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
911 if (has_pending_exception) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +0000912 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000913}
914
915
916#define INSTALL_NATIVE(Type, name, var) \
917 Handle<String> var##_name = Factory::LookupAsciiSymbol(name); \
918 global_context()->set_##var(Type::cast(global_context()-> \
919 builtins()-> \
920 GetProperty(*var##_name)));
921
922void Genesis::InstallNativeFunctions() {
923 HandleScope scope;
924 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
925 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
926 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
927 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
928 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
929 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
930 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
931 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Leon Clarkee46be812010-01-19 14:06:41 +0000932 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
934 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
935 configure_instance_fun);
936 INSTALL_NATIVE(JSFunction, "MakeMessage", make_message_fun);
937 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
938 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
939}
940
941#undef INSTALL_NATIVE
942
943
944bool Genesis::InstallNatives() {
945 HandleScope scope;
946
947 // Create a function for the builtins object. Allocate space for the
948 // JavaScript builtins, a reference to the builtins object
949 // (itself) and a reference to the global_context directly in the object.
950 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
951 Handle<JSFunction> builtins_fun =
952 Factory::NewFunction(Factory::empty_symbol(), JS_BUILTINS_OBJECT_TYPE,
953 JSBuiltinsObject::kSize, code, true);
954
955 Handle<String> name = Factory::LookupAsciiSymbol("builtins");
956 builtins_fun->shared()->set_instance_class_name(*name);
957
958 // Allocate the builtins object.
959 Handle<JSBuiltinsObject> builtins =
960 Handle<JSBuiltinsObject>::cast(Factory::NewGlobalObject(builtins_fun));
961 builtins->set_builtins(*builtins);
962 builtins->set_global_context(*global_context());
963 builtins->set_global_receiver(*builtins);
964
965 // Setup the 'global' properties of the builtins object. The
966 // 'global' property that refers to the global object is the only
967 // way to get from code running in the builtins context to the
968 // global object.
969 static const PropertyAttributes attributes =
970 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
971 SetProperty(builtins, Factory::LookupAsciiSymbol("global"),
972 Handle<Object>(global_context()->global()), attributes);
973
974 // Setup the reference from the global object to the builtins object.
975 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
976
977 // Create a bridge function that has context in the global context.
978 Handle<JSFunction> bridge =
979 Factory::NewFunction(Factory::empty_symbol(), Factory::undefined_value());
980 ASSERT(bridge->context() == *Top::global_context());
981
982 // Allocate the builtins context.
983 Handle<Context> context =
984 Factory::NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
985 context->set_global(*builtins); // override builtins global object
986
987 global_context()->set_runtime_context(*context);
988
989 { // -- S c r i p t
990 // Builtin functions for Script.
991 Handle<JSFunction> script_fun =
992 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
993 Top::initial_object_prototype(), Builtins::Illegal,
994 false);
995 Handle<JSObject> prototype =
996 Factory::NewJSObject(Top::object_function(), TENURED);
997 SetPrototype(script_fun, prototype);
998 global_context()->set_script_function(*script_fun);
999
1000 // Add 'source' and 'data' property to scripts.
1001 PropertyAttributes common_attributes =
1002 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1003 Handle<Proxy> proxy_source = Factory::NewProxy(&Accessors::ScriptSource);
1004 Handle<DescriptorArray> script_descriptors =
1005 Factory::CopyAppendProxyDescriptor(
1006 Factory::empty_descriptor_array(),
1007 Factory::LookupAsciiSymbol("source"),
1008 proxy_source,
1009 common_attributes);
1010 Handle<Proxy> proxy_name = Factory::NewProxy(&Accessors::ScriptName);
1011 script_descriptors =
1012 Factory::CopyAppendProxyDescriptor(
1013 script_descriptors,
1014 Factory::LookupAsciiSymbol("name"),
1015 proxy_name,
1016 common_attributes);
1017 Handle<Proxy> proxy_id = Factory::NewProxy(&Accessors::ScriptId);
1018 script_descriptors =
1019 Factory::CopyAppendProxyDescriptor(
1020 script_descriptors,
1021 Factory::LookupAsciiSymbol("id"),
1022 proxy_id,
1023 common_attributes);
1024 Handle<Proxy> proxy_line_offset =
1025 Factory::NewProxy(&Accessors::ScriptLineOffset);
1026 script_descriptors =
1027 Factory::CopyAppendProxyDescriptor(
1028 script_descriptors,
1029 Factory::LookupAsciiSymbol("line_offset"),
1030 proxy_line_offset,
1031 common_attributes);
1032 Handle<Proxy> proxy_column_offset =
1033 Factory::NewProxy(&Accessors::ScriptColumnOffset);
1034 script_descriptors =
1035 Factory::CopyAppendProxyDescriptor(
1036 script_descriptors,
1037 Factory::LookupAsciiSymbol("column_offset"),
1038 proxy_column_offset,
1039 common_attributes);
1040 Handle<Proxy> proxy_data = Factory::NewProxy(&Accessors::ScriptData);
1041 script_descriptors =
1042 Factory::CopyAppendProxyDescriptor(
1043 script_descriptors,
1044 Factory::LookupAsciiSymbol("data"),
1045 proxy_data,
1046 common_attributes);
1047 Handle<Proxy> proxy_type = Factory::NewProxy(&Accessors::ScriptType);
1048 script_descriptors =
1049 Factory::CopyAppendProxyDescriptor(
1050 script_descriptors,
1051 Factory::LookupAsciiSymbol("type"),
1052 proxy_type,
1053 common_attributes);
1054 Handle<Proxy> proxy_compilation_type =
1055 Factory::NewProxy(&Accessors::ScriptCompilationType);
1056 script_descriptors =
1057 Factory::CopyAppendProxyDescriptor(
1058 script_descriptors,
1059 Factory::LookupAsciiSymbol("compilation_type"),
1060 proxy_compilation_type,
1061 common_attributes);
1062 Handle<Proxy> proxy_line_ends =
1063 Factory::NewProxy(&Accessors::ScriptLineEnds);
1064 script_descriptors =
1065 Factory::CopyAppendProxyDescriptor(
1066 script_descriptors,
1067 Factory::LookupAsciiSymbol("line_ends"),
1068 proxy_line_ends,
1069 common_attributes);
1070 Handle<Proxy> proxy_context_data =
1071 Factory::NewProxy(&Accessors::ScriptContextData);
1072 script_descriptors =
1073 Factory::CopyAppendProxyDescriptor(
1074 script_descriptors,
1075 Factory::LookupAsciiSymbol("context_data"),
1076 proxy_context_data,
1077 common_attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001078 Handle<Proxy> proxy_eval_from_script =
1079 Factory::NewProxy(&Accessors::ScriptEvalFromScript);
Steve Blocka7e24c12009-10-30 11:49:00 +00001080 script_descriptors =
1081 Factory::CopyAppendProxyDescriptor(
1082 script_descriptors,
Steve Blockd0582a62009-12-15 09:54:21 +00001083 Factory::LookupAsciiSymbol("eval_from_script"),
1084 proxy_eval_from_script,
Steve Blocka7e24c12009-10-30 11:49:00 +00001085 common_attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001086 Handle<Proxy> proxy_eval_from_script_position =
1087 Factory::NewProxy(&Accessors::ScriptEvalFromScriptPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +00001088 script_descriptors =
1089 Factory::CopyAppendProxyDescriptor(
1090 script_descriptors,
Steve Blockd0582a62009-12-15 09:54:21 +00001091 Factory::LookupAsciiSymbol("eval_from_script_position"),
1092 proxy_eval_from_script_position,
1093 common_attributes);
1094 Handle<Proxy> proxy_eval_from_function_name =
1095 Factory::NewProxy(&Accessors::ScriptEvalFromFunctionName);
1096 script_descriptors =
1097 Factory::CopyAppendProxyDescriptor(
1098 script_descriptors,
1099 Factory::LookupAsciiSymbol("eval_from_function_name"),
1100 proxy_eval_from_function_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001101 common_attributes);
1102
1103 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1104 script_map->set_instance_descriptors(*script_descriptors);
1105
1106 // Allocate the empty script.
1107 Handle<Script> script = Factory::NewScript(Factory::empty_string());
1108 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Andrei Popescu31002712010-02-23 13:46:05 +00001109 Heap::public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001110 }
1111
Andrei Popescu31002712010-02-23 13:46:05 +00001112 // Install natives.
1113 for (int i = Natives::GetDebuggerCount();
1114 i < Natives::GetBuiltinsCount();
1115 i++) {
1116 Vector<const char> name = Natives::GetScriptName(i);
1117 if (!CompileBuiltin(i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001118 // TODO(ager): We really only need to install the JS builtin
1119 // functions on the builtins object after compiling and running
1120 // runtime.js.
1121 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 }
1123
1124 InstallNativeFunctions();
1125
1126 // Install Function.prototype.call and apply.
1127 { Handle<String> key = Factory::function_class_symbol();
1128 Handle<JSFunction> function =
1129 Handle<JSFunction>::cast(GetProperty(Top::global(), key));
1130 Handle<JSObject> proto =
1131 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1132
1133 // Install the call and the apply functions.
1134 Handle<JSFunction> call =
1135 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1136 Factory::NewJSObject(Top::object_function(), TENURED),
1137 Builtins::FunctionCall,
1138 false);
1139 Handle<JSFunction> apply =
1140 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1141 Factory::NewJSObject(Top::object_function(), TENURED),
1142 Builtins::FunctionApply,
1143 false);
1144
1145 // Make sure that Function.prototype.call appears to be compiled.
1146 // The code will never be called, but inline caching for call will
1147 // only work if it appears to be compiled.
1148 call->shared()->DontAdaptArguments();
1149 ASSERT(call->is_compiled());
1150
1151 // Set the expected parameters for apply to 2; required by builtin.
1152 apply->shared()->set_formal_parameter_count(2);
1153
1154 // Set the lengths for the functions to satisfy ECMA-262.
1155 call->shared()->set_length(1);
1156 apply->shared()->set_length(2);
1157 }
1158
1159#ifdef DEBUG
1160 builtins->Verify();
1161#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001162
Steve Blocka7e24c12009-10-30 11:49:00 +00001163 return true;
1164}
1165
1166
Andrei Popescu31002712010-02-23 13:46:05 +00001167int BootstrapperActive::nesting_ = 0;
1168
1169
1170bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1171 v8::ExtensionConfiguration* extensions) {
1172 BootstrapperActive active;
1173 SaveContext saved_context;
1174 Top::set_context(*global_context);
1175 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1176 Genesis::InstallSpecialObjects(global_context);
1177 return true;
1178}
1179
1180
1181void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001182 HandleScope scope;
1183 Handle<JSGlobalObject> js_global(
Andrei Popescu31002712010-02-23 13:46:05 +00001184 JSGlobalObject::cast(global_context->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001185 // Expose the natives in global if a name for it is specified.
1186 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1187 Handle<String> natives_string =
1188 Factory::LookupAsciiSymbol(FLAG_expose_natives_as);
1189 SetProperty(js_global, natives_string,
1190 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
1191 }
1192
1193 Handle<Object> Error = GetProperty(js_global, "Error");
1194 if (Error->IsJSObject()) {
1195 Handle<String> name = Factory::LookupAsciiSymbol("stackTraceLimit");
1196 SetProperty(Handle<JSObject>::cast(Error),
1197 name,
1198 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1199 NONE);
1200 }
1201
1202#ifdef ENABLE_DEBUGGER_SUPPORT
1203 // Expose the debug global object in global if a name for it is specified.
1204 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
1205 // If loading fails we just bail out without installing the
1206 // debugger but without tanking the whole context.
Andrei Popescu31002712010-02-23 13:46:05 +00001207 if (!Debug::Load()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001208 // Set the security token for the debugger context to the same as
1209 // the shell global context to allow calling between these (otherwise
1210 // exposing debug global object doesn't make much sense).
1211 Debug::debug_context()->set_security_token(
Andrei Popescu31002712010-02-23 13:46:05 +00001212 global_context->security_token());
Steve Blocka7e24c12009-10-30 11:49:00 +00001213
1214 Handle<String> debug_string =
1215 Factory::LookupAsciiSymbol(FLAG_expose_debug_as);
1216 SetProperty(js_global, debug_string,
1217 Handle<Object>(Debug::debug_context()->global_proxy()), DONT_ENUM);
1218 }
1219#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001220}
1221
1222
Andrei Popescu31002712010-02-23 13:46:05 +00001223bool Genesis::InstallExtensions(Handle<Context> global_context,
1224 v8::ExtensionConfiguration* extensions) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001225 // Clear coloring of extension list
1226 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1227 while (current != NULL) {
1228 current->set_state(v8::UNVISITED);
1229 current = current->next();
1230 }
Andrei Popescu31002712010-02-23 13:46:05 +00001231 // Install auto extensions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001232 current = v8::RegisteredExtension::first_extension();
1233 while (current != NULL) {
1234 if (current->extension()->auto_enable())
1235 InstallExtension(current);
1236 current = current->next();
1237 }
1238
1239 if (FLAG_expose_gc) InstallExtension("v8/gc");
1240
1241 if (extensions == NULL) return true;
1242 // Install required extensions
1243 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1244 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1245 for (int i = 0; i < count; i++) {
1246 if (!InstallExtension(names[i]))
1247 return false;
1248 }
1249
1250 return true;
1251}
1252
1253
1254// Installs a named extension. This methods is unoptimized and does
1255// not scale well if we want to support a large number of extensions.
1256bool Genesis::InstallExtension(const char* name) {
1257 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1258 // Loop until we find the relevant extension
1259 while (current != NULL) {
1260 if (strcmp(name, current->extension()->name()) == 0) break;
1261 current = current->next();
1262 }
1263 // Didn't find the extension; fail.
1264 if (current == NULL) {
1265 v8::Utils::ReportApiFailure(
1266 "v8::Context::New()", "Cannot find required extension");
1267 return false;
1268 }
1269 return InstallExtension(current);
1270}
1271
1272
1273bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1274 HandleScope scope;
1275
1276 if (current->state() == v8::INSTALLED) return true;
1277 // The current node has already been visited so there must be a
1278 // cycle in the dependency graph; fail.
1279 if (current->state() == v8::VISITED) {
1280 v8::Utils::ReportApiFailure(
1281 "v8::Context::New()", "Circular extension dependency");
1282 return false;
1283 }
1284 ASSERT(current->state() == v8::UNVISITED);
1285 current->set_state(v8::VISITED);
1286 v8::Extension* extension = current->extension();
1287 // Install the extension's dependencies
1288 for (int i = 0; i < extension->dependency_count(); i++) {
1289 if (!InstallExtension(extension->dependencies()[i])) return false;
1290 }
1291 Vector<const char> source = CStrVector(extension->source());
1292 Handle<String> source_code = Factory::NewStringFromAscii(source);
1293 bool result = CompileScriptCached(CStrVector(extension->name()),
1294 source_code,
Andrei Popescu31002712010-02-23 13:46:05 +00001295 &extensions_cache,
1296 extension,
1297 Handle<Context>(Top::context()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001298 false);
1299 ASSERT(Top::has_pending_exception() != result);
1300 if (!result) {
1301 Top::clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 }
1303 current->set_state(v8::INSTALLED);
1304 return result;
1305}
1306
1307
Andrei Popescu402d9372010-02-26 13:31:12 +00001308bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1309 HandleScope scope;
1310 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1311 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
1312 Handle<String> name = Factory::LookupAsciiSymbol(Builtins::GetName(id));
1313 Handle<JSFunction> function
1314 = Handle<JSFunction>(JSFunction::cast(builtins->GetProperty(*name)));
1315 builtins->set_javascript_builtin(id, *function);
1316 Handle<SharedFunctionInfo> shared
1317 = Handle<SharedFunctionInfo>(function->shared());
1318 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
1319 }
1320 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00001321}
1322
1323
Steve Blocka7e24c12009-10-30 11:49:00 +00001324bool Genesis::ConfigureGlobalObjects(
1325 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1326 Handle<JSObject> global_proxy(
1327 JSObject::cast(global_context()->global_proxy()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001328 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001329
1330 if (!global_proxy_template.IsEmpty()) {
1331 // Configure the global proxy object.
1332 Handle<ObjectTemplateInfo> proxy_data =
1333 v8::Utils::OpenHandle(*global_proxy_template);
1334 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1335
1336 // Configure the inner global object.
1337 Handle<FunctionTemplateInfo> proxy_constructor(
1338 FunctionTemplateInfo::cast(proxy_data->constructor()));
1339 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1340 Handle<ObjectTemplateInfo> inner_data(
1341 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001342 if (!ConfigureApiObject(inner_global, inner_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001343 }
1344 }
1345
Andrei Popescu402d9372010-02-26 13:31:12 +00001346 SetObjectPrototype(global_proxy, inner_global);
Steve Blocka7e24c12009-10-30 11:49:00 +00001347 return true;
1348}
1349
1350
1351bool Genesis::ConfigureApiObject(Handle<JSObject> object,
1352 Handle<ObjectTemplateInfo> object_template) {
1353 ASSERT(!object_template.is_null());
1354 ASSERT(object->IsInstanceOf(
1355 FunctionTemplateInfo::cast(object_template->constructor())));
1356
1357 bool pending_exception = false;
1358 Handle<JSObject> obj =
1359 Execution::InstantiateObject(object_template, &pending_exception);
1360 if (pending_exception) {
1361 ASSERT(Top::has_pending_exception());
1362 Top::clear_pending_exception();
1363 return false;
1364 }
1365 TransferObject(obj, object);
1366 return true;
1367}
1368
1369
1370void Genesis::TransferNamedProperties(Handle<JSObject> from,
1371 Handle<JSObject> to) {
1372 if (from->HasFastProperties()) {
1373 Handle<DescriptorArray> descs =
1374 Handle<DescriptorArray>(from->map()->instance_descriptors());
1375 for (int i = 0; i < descs->number_of_descriptors(); i++) {
1376 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
1377 switch (details.type()) {
1378 case FIELD: {
1379 HandleScope inner;
1380 Handle<String> key = Handle<String>(descs->GetKey(i));
1381 int index = descs->GetFieldIndex(i);
1382 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
1383 SetProperty(to, key, value, details.attributes());
1384 break;
1385 }
1386 case CONSTANT_FUNCTION: {
1387 HandleScope inner;
1388 Handle<String> key = Handle<String>(descs->GetKey(i));
1389 Handle<JSFunction> fun =
1390 Handle<JSFunction>(descs->GetConstantFunction(i));
1391 SetProperty(to, key, fun, details.attributes());
1392 break;
1393 }
1394 case CALLBACKS: {
1395 LookupResult result;
1396 to->LocalLookup(descs->GetKey(i), &result);
1397 // If the property is already there we skip it
Andrei Popescu402d9372010-02-26 13:31:12 +00001398 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00001399 HandleScope inner;
Andrei Popescu31002712010-02-23 13:46:05 +00001400 ASSERT(!to->HasFastProperties());
1401 // Add to dictionary.
Steve Blocka7e24c12009-10-30 11:49:00 +00001402 Handle<String> key = Handle<String>(descs->GetKey(i));
Andrei Popescu31002712010-02-23 13:46:05 +00001403 Handle<Object> callbacks(descs->GetCallbacksObject(i));
1404 PropertyDetails d =
1405 PropertyDetails(details.attributes(), CALLBACKS, details.index());
1406 SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00001407 break;
1408 }
1409 case MAP_TRANSITION:
1410 case CONSTANT_TRANSITION:
1411 case NULL_DESCRIPTOR:
1412 // Ignore non-properties.
1413 break;
1414 case NORMAL:
1415 // Do not occur since the from object has fast properties.
1416 case INTERCEPTOR:
1417 // No element in instance descriptors have interceptor type.
1418 UNREACHABLE();
1419 break;
1420 }
1421 }
1422 } else {
1423 Handle<StringDictionary> properties =
1424 Handle<StringDictionary>(from->property_dictionary());
1425 int capacity = properties->Capacity();
1426 for (int i = 0; i < capacity; i++) {
1427 Object* raw_key(properties->KeyAt(i));
1428 if (properties->IsKey(raw_key)) {
1429 ASSERT(raw_key->IsString());
1430 // If the property is already there we skip it.
1431 LookupResult result;
1432 to->LocalLookup(String::cast(raw_key), &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00001433 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00001434 // Set the property.
1435 Handle<String> key = Handle<String>(String::cast(raw_key));
1436 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
1437 if (value->IsJSGlobalPropertyCell()) {
1438 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
1439 }
1440 PropertyDetails details = properties->DetailsAt(i);
1441 SetProperty(to, key, value, details.attributes());
1442 }
1443 }
1444 }
1445}
1446
1447
1448void Genesis::TransferIndexedProperties(Handle<JSObject> from,
1449 Handle<JSObject> to) {
1450 // Cloning the elements array is sufficient.
1451 Handle<FixedArray> from_elements =
1452 Handle<FixedArray>(FixedArray::cast(from->elements()));
1453 Handle<FixedArray> to_elements = Factory::CopyFixedArray(from_elements);
1454 to->set_elements(*to_elements);
1455}
1456
1457
1458void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
1459 HandleScope outer;
1460
1461 ASSERT(!from->IsJSArray());
1462 ASSERT(!to->IsJSArray());
1463
1464 TransferNamedProperties(from, to);
1465 TransferIndexedProperties(from, to);
1466
1467 // Transfer the prototype (new map is needed).
1468 Handle<Map> old_to_map = Handle<Map>(to->map());
1469 Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map);
1470 new_to_map->set_prototype(from->map()->prototype());
1471 to->set_map(*new_to_map);
1472}
1473
1474
1475void Genesis::MakeFunctionInstancePrototypeWritable() {
1476 // Make a new function map so all future functions
1477 // will have settable and enumerable prototype properties.
1478 HandleScope scope;
1479
1480 Handle<DescriptorArray> function_map_descriptors =
1481 ComputeFunctionInstanceDescriptor(false);
1482 Handle<Map> fm = Factory::CopyMapDropDescriptors(Top::function_map());
1483 fm->set_instance_descriptors(*function_map_descriptors);
1484 Top::context()->global_context()->set_function_map(*fm);
1485}
1486
1487
1488void Genesis::AddSpecialFunction(Handle<JSObject> prototype,
1489 const char* name,
1490 Handle<Code> code) {
1491 Handle<String> key = Factory::LookupAsciiSymbol(name);
1492 Handle<Object> value = Handle<Object>(prototype->GetProperty(*key));
1493 if (value->IsJSFunction()) {
1494 Handle<JSFunction> optimized = Factory::NewFunction(key,
1495 JS_OBJECT_TYPE,
1496 JSObject::kHeaderSize,
1497 code,
1498 false);
1499 optimized->shared()->DontAdaptArguments();
1500 int len = global_context()->special_function_table()->length();
1501 Handle<FixedArray> new_array = Factory::NewFixedArray(len + 3);
1502 for (int index = 0; index < len; index++) {
1503 new_array->set(index,
1504 global_context()->special_function_table()->get(index));
1505 }
1506 new_array->set(len+0, *prototype);
1507 new_array->set(len+1, *value);
1508 new_array->set(len+2, *optimized);
1509 global_context()->set_special_function_table(*new_array);
1510 }
1511}
1512
1513
1514void Genesis::BuildSpecialFunctionTable() {
1515 HandleScope scope;
1516 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
Andrei Popescu402d9372010-02-26 13:31:12 +00001517 // Add special versions for some Array.prototype functions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001518 Handle<JSFunction> function =
1519 Handle<JSFunction>(
1520 JSFunction::cast(global->GetProperty(Heap::Array_symbol())));
1521 Handle<JSObject> visible_prototype =
1522 Handle<JSObject>(JSObject::cast(function->prototype()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001523 // Remember to put those specializations on the hidden prototype if present.
1524 Handle<JSObject> special_prototype;
Steve Blocka7e24c12009-10-30 11:49:00 +00001525 Handle<Object> superproto(visible_prototype->GetPrototype());
1526 if (superproto->IsJSObject() &&
1527 JSObject::cast(*superproto)->map()->is_hidden_prototype()) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001528 special_prototype = Handle<JSObject>::cast(superproto);
Steve Blocka7e24c12009-10-30 11:49:00 +00001529 } else {
Andrei Popescu402d9372010-02-26 13:31:12 +00001530 special_prototype = visible_prototype;
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001532 AddSpecialFunction(special_prototype, "pop",
Steve Blocka7e24c12009-10-30 11:49:00 +00001533 Handle<Code>(Builtins::builtin(Builtins::ArrayPop)));
Andrei Popescu402d9372010-02-26 13:31:12 +00001534 AddSpecialFunction(special_prototype, "push",
Steve Blocka7e24c12009-10-30 11:49:00 +00001535 Handle<Code>(Builtins::builtin(Builtins::ArrayPush)));
Andrei Popescu402d9372010-02-26 13:31:12 +00001536 AddSpecialFunction(special_prototype, "shift",
1537 Handle<Code>(Builtins::builtin(Builtins::ArrayShift)));
1538 AddSpecialFunction(special_prototype, "unshift",
1539 Handle<Code>(Builtins::builtin(Builtins::ArrayUnshift)));
1540 AddSpecialFunction(special_prototype, "slice",
1541 Handle<Code>(Builtins::builtin(Builtins::ArraySlice)));
1542 AddSpecialFunction(special_prototype, "splice",
1543 Handle<Code>(Builtins::builtin(Builtins::ArraySplice)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001544}
1545
1546
1547Genesis::Genesis(Handle<Object> global_object,
1548 v8::Handle<v8::ObjectTemplate> global_template,
1549 v8::ExtensionConfiguration* extensions) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001550 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00001551 // If V8 isn't running and cannot be initialized, just return.
1552 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
1553
1554 // Before creating the roots we must save the context and restore it
1555 // on all function exits.
1556 HandleScope scope;
Andrei Popescu31002712010-02-23 13:46:05 +00001557 SaveContext saved_context;
Steve Blocka7e24c12009-10-30 11:49:00 +00001558
Andrei Popescu31002712010-02-23 13:46:05 +00001559 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
1560 if (!new_context.is_null()) {
1561 global_context_ =
1562 Handle<Context>::cast(GlobalHandles::Create(*new_context));
1563 Top::set_context(*global_context_);
1564 i::Counters::contexts_created_by_snapshot.Increment();
1565 result_ = global_context_;
1566 JSFunction* empty_function =
1567 JSFunction::cast(result_->function_map()->prototype());
1568 empty_function_ = Handle<JSFunction>(empty_function);
Andrei Popescu402d9372010-02-26 13:31:12 +00001569 Handle<GlobalObject> inner_global;
Andrei Popescu31002712010-02-23 13:46:05 +00001570 Handle<JSGlobalProxy> global_proxy =
1571 CreateNewGlobals(global_template,
1572 global_object,
Andrei Popescu402d9372010-02-26 13:31:12 +00001573 &inner_global);
1574
Andrei Popescu31002712010-02-23 13:46:05 +00001575 HookUpGlobalProxy(inner_global, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00001576 HookUpInnerGlobal(inner_global);
1577
Andrei Popescu31002712010-02-23 13:46:05 +00001578 if (!ConfigureGlobalObjects(global_template)) return;
1579 } else {
1580 // We get here if there was no context snapshot.
1581 CreateRoots();
1582 Handle<JSFunction> empty_function = CreateEmptyFunction();
1583 Handle<GlobalObject> inner_global;
1584 Handle<JSGlobalProxy> global_proxy =
1585 CreateNewGlobals(global_template, global_object, &inner_global);
1586 HookUpGlobalProxy(inner_global, global_proxy);
1587 InitializeGlobal(inner_global, empty_function);
1588 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001589
Andrei Popescu31002712010-02-23 13:46:05 +00001590 MakeFunctionInstancePrototypeWritable();
1591 BuildSpecialFunctionTable();
Steve Blocka7e24c12009-10-30 11:49:00 +00001592
Andrei Popescu31002712010-02-23 13:46:05 +00001593 if (!ConfigureGlobalObjects(global_template)) return;
1594 i::Counters::contexts_created_from_scratch.Increment();
1595 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001596
1597 result_ = global_context_;
1598}
1599
1600
1601// Support for thread preemption.
1602
1603// Reserve space for statics needing saving and restoring.
1604int Bootstrapper::ArchiveSpacePerThread() {
Andrei Popescu31002712010-02-23 13:46:05 +00001605 return BootstrapperActive::ArchiveSpacePerThread();
Steve Blocka7e24c12009-10-30 11:49:00 +00001606}
1607
1608
1609// Archive statics that are thread local.
1610char* Bootstrapper::ArchiveState(char* to) {
Andrei Popescu31002712010-02-23 13:46:05 +00001611 return BootstrapperActive::ArchiveState(to);
Steve Blocka7e24c12009-10-30 11:49:00 +00001612}
1613
1614
1615// Restore statics that are thread local.
1616char* Bootstrapper::RestoreState(char* from) {
Andrei Popescu31002712010-02-23 13:46:05 +00001617 return BootstrapperActive::RestoreState(from);
Steve Blocka7e24c12009-10-30 11:49:00 +00001618}
1619
1620
1621// Called when the top-level V8 mutex is destroyed.
1622void Bootstrapper::FreeThreadResources() {
Andrei Popescu31002712010-02-23 13:46:05 +00001623 ASSERT(!BootstrapperActive::IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00001624}
1625
1626
1627// Reserve space for statics needing saving and restoring.
Andrei Popescu31002712010-02-23 13:46:05 +00001628int BootstrapperActive::ArchiveSpacePerThread() {
1629 return sizeof(nesting_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001630}
1631
1632
1633// Archive statics that are thread local.
Andrei Popescu31002712010-02-23 13:46:05 +00001634char* BootstrapperActive::ArchiveState(char* to) {
1635 *reinterpret_cast<int*>(to) = nesting_;
1636 nesting_ = 0;
1637 return to + sizeof(nesting_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001638}
1639
1640
1641// Restore statics that are thread local.
Andrei Popescu31002712010-02-23 13:46:05 +00001642char* BootstrapperActive::RestoreState(char* from) {
1643 nesting_ = *reinterpret_cast<int*>(from);
1644 return from + sizeof(nesting_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001645}
1646
1647} } // namespace v8::internal