00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "v8.h"
00029
00030 #include "accessors.h"
00031 #include "api.h"
00032 #include "bootstrapper.h"
00033 #include "compiler.h"
00034 #include "debug.h"
00035 #include "execution.h"
00036 #include "global-handles.h"
00037 #include "macro-assembler.h"
00038 #include "natives.h"
00039
00040 namespace v8 { namespace internal {
00041
00042
00043
00044
00045
00046
00047 class SourceCodeCache BASE_EMBEDDED {
00048 public:
00049 explicit SourceCodeCache(ScriptType type): type_(type) { }
00050
00051 void Initialize(bool create_heap_objects) {
00052 if (create_heap_objects) {
00053 cache_ = Heap::empty_fixed_array();
00054 } else {
00055 cache_ = NULL;
00056 }
00057 }
00058
00059 void Iterate(ObjectVisitor* v) {
00060 v->VisitPointer(bit_cast<Object**, FixedArray**>(&cache_));
00061 }
00062
00063
00064 bool Lookup(Vector<const char> name, Handle<JSFunction>* handle) {
00065 for (int i = 0; i < cache_->length(); i+=2) {
00066 SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i));
00067 if (str->IsEqualTo(name)) {
00068 *handle = Handle<JSFunction>(JSFunction::cast(cache_->get(i + 1)));
00069 return true;
00070 }
00071 }
00072 return false;
00073 }
00074
00075
00076 void Add(Vector<const char> name, Handle<JSFunction> fun) {
00077 ASSERT(fun->IsBoilerplate());
00078 HandleScope scope;
00079 int length = cache_->length();
00080 Handle<FixedArray> new_array =
00081 Factory::NewFixedArray(length + 2, TENURED);
00082 cache_->CopyTo(0, *new_array, 0, cache_->length());
00083 cache_ = *new_array;
00084 Handle<String> str = Factory::NewStringFromAscii(name, TENURED);
00085 cache_->set(length, *str);
00086 cache_->set(length + 1, *fun);
00087 Script::cast(fun->shared()->script())->set_type(Smi::FromInt(type_));
00088 }
00089
00090 private:
00091 ScriptType type_;
00092 FixedArray* cache_;
00093 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
00094 };
00095
00096 static SourceCodeCache natives_cache(SCRIPT_TYPE_NATIVE);
00097 static SourceCodeCache extensions_cache(SCRIPT_TYPE_EXTENSION);
00098
00099
00100 Handle<String> Bootstrapper::NativesSourceLookup(int index) {
00101 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
00102 if (Heap::natives_source_cache()->get(index)->IsUndefined()) {
00103 Handle<String> source_code =
00104 Factory::NewStringFromAscii(Natives::GetScriptSource(index));
00105 Heap::natives_source_cache()->set(index, *source_code);
00106 }
00107 Handle<Object> cached_source(Heap::natives_source_cache()->get(index));
00108 return Handle<String>::cast(cached_source);
00109 }
00110
00111
00112 bool Bootstrapper::NativesCacheLookup(Vector<const char> name,
00113 Handle<JSFunction>* handle) {
00114 return natives_cache.Lookup(name, handle);
00115 }
00116
00117
00118 void Bootstrapper::NativesCacheAdd(Vector<const char> name,
00119 Handle<JSFunction> fun) {
00120 natives_cache.Add(name, fun);
00121 }
00122
00123
00124 void Bootstrapper::Initialize(bool create_heap_objects) {
00125 natives_cache.Initialize(create_heap_objects);
00126 extensions_cache.Initialize(create_heap_objects);
00127 }
00128
00129
00130 void Bootstrapper::TearDown() {
00131 natives_cache.Initialize(false);
00132 extensions_cache.Initialize(false);
00133 }
00134
00135
00136
00137
00138
00139
00140 class PendingFixups : public AllStatic {
00141 public:
00142 static void Add(Code* code, MacroAssembler* masm);
00143 static bool Process(Handle<JSBuiltinsObject> builtins);
00144
00145 static void Iterate(ObjectVisitor* v);
00146
00147 private:
00148 static List<Object*> code_;
00149 static List<const char*> name_;
00150 static List<int> pc_;
00151 static List<uint32_t> flags_;
00152
00153 static void Clear();
00154 };
00155
00156
00157 List<Object*> PendingFixups::code_(0);
00158 List<const char*> PendingFixups::name_(0);
00159 List<int> PendingFixups::pc_(0);
00160 List<uint32_t> PendingFixups::flags_(0);
00161
00162
00163 void PendingFixups::Add(Code* code, MacroAssembler* masm) {
00164
00165 List<MacroAssembler::Unresolved>* unresolved = masm->unresolved();
00166 int n = unresolved->length();
00167 for (int i = 0; i < n; i++) {
00168 const char* name = unresolved->at(i).name;
00169 code_.Add(code);
00170 name_.Add(name);
00171 pc_.Add(unresolved->at(i).pc);
00172 flags_.Add(unresolved->at(i).flags);
00173 LOG(StringEvent("unresolved", name));
00174 }
00175 }
00176
00177
00178 bool PendingFixups::Process(Handle<JSBuiltinsObject> builtins) {
00179 HandleScope scope;
00180
00181
00182
00183 for (int i = 0; i < code_.length(); i++) {
00184 const char* name = name_[i];
00185 uint32_t flags = flags_[i];
00186 Handle<String> symbol = Factory::LookupAsciiSymbol(name);
00187 Object* o = builtins->GetProperty(*symbol);
00188 #ifdef DEBUG
00189 if (!o->IsJSFunction()) {
00190 V8_Fatal(__FILE__, __LINE__, "Cannot resolve call to builtin %s", name);
00191 }
00192 #endif
00193 Handle<JSFunction> f = Handle<JSFunction>(JSFunction::cast(o));
00194
00195 int argc = Bootstrapper::FixupFlagsArgumentsCount::decode(flags);
00196 USE(argc);
00197 ASSERT(f->shared()->formal_parameter_count() == argc);
00198 if (!f->is_compiled()) {
00199
00200 if (!CompileLazy(f, CLEAR_EXCEPTION)) {
00201 Clear();
00202 return false;
00203 }
00204 }
00205 Code* code = Code::cast(code_[i]);
00206 Address pc = code->instruction_start() + pc_[i];
00207 bool is_pc_relative = Bootstrapper::FixupFlagsIsPCRelative::decode(flags);
00208 if (is_pc_relative) {
00209 Assembler::set_target_address_at(pc, f->code()->instruction_start());
00210 } else {
00211 *reinterpret_cast<Object**>(pc) = f->code();
00212 }
00213 LOG(StringEvent("resolved", name));
00214 }
00215 Clear();
00216
00217
00218
00219
00220
00221 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
00222 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
00223 Handle<String> name = Factory::LookupAsciiSymbol(Builtins::GetName(id));
00224 JSFunction* function = JSFunction::cast(builtins->GetProperty(*name));
00225 builtins->set_javascript_builtin(id, function);
00226 }
00227
00228 return true;
00229 }
00230
00231
00232 void PendingFixups::Clear() {
00233 code_.Clear();
00234 name_.Clear();
00235 pc_.Clear();
00236 flags_.Clear();
00237 }
00238
00239
00240 void PendingFixups::Iterate(ObjectVisitor* v) {
00241 if (!code_.is_empty()) {
00242 v->VisitPointers(&code_[0], &code_[0] + code_.length());
00243 }
00244 }
00245
00246
00247 class Genesis BASE_EMBEDDED {
00248 public:
00249 Genesis(Handle<Object> global_object,
00250 v8::Handle<v8::ObjectTemplate> global_template,
00251 v8::ExtensionConfiguration* extensions);
00252 ~Genesis();
00253
00254 Handle<Context> result() { return result_; }
00255
00256 Genesis* previous() { return previous_; }
00257 static Genesis* current() { return current_; }
00258
00259 private:
00260 Handle<Context> global_context_;
00261
00262
00263
00264
00265 Genesis* previous_;
00266 static Genesis* current_;
00267
00268 Handle<Context> global_context() { return global_context_; }
00269
00270 void CreateRoots(v8::Handle<v8::ObjectTemplate> global_template,
00271 Handle<Object> global_object);
00272 void InstallNativeFunctions();
00273 bool InstallNatives();
00274 bool InstallExtensions(v8::ExtensionConfiguration* extensions);
00275 bool InstallExtension(const char* name);
00276 bool InstallExtension(v8::RegisteredExtension* current);
00277 bool InstallSpecialObjects();
00278 bool ConfigureApiObject(Handle<JSObject> object,
00279 Handle<ObjectTemplateInfo> object_template);
00280 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
00281
00282
00283
00284
00285 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
00286 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
00287 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
00288
00289 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
00290 bool make_prototype_read_only,
00291 bool make_prototype_enumerable = false);
00292 void MakeFunctionInstancePrototypeWritable();
00293
00294 void AddSpecialFunction(Handle<JSObject> prototype,
00295 const char* name,
00296 Handle<Code> code);
00297
00298 void BuildSpecialFunctionTable();
00299
00300 static bool CompileBuiltin(int index);
00301 static bool CompileNative(Vector<const char> name, Handle<String> source);
00302 static bool CompileScriptCached(Vector<const char> name,
00303 Handle<String> source,
00304 SourceCodeCache* cache,
00305 v8::Extension* extension,
00306 bool use_runtime_context);
00307
00308 Handle<Context> result_;
00309 };
00310
00311 Genesis* Genesis::current_ = NULL;
00312
00313
00314 void Bootstrapper::Iterate(ObjectVisitor* v) {
00315 natives_cache.Iterate(v);
00316 extensions_cache.Iterate(v);
00317 PendingFixups::Iterate(v);
00318 }
00319
00320
00321
00322
00323 void Bootstrapper::AddFixup(Code* code, MacroAssembler* masm) {
00324 PendingFixups::Add(code, masm);
00325 }
00326
00327
00328 bool Bootstrapper::IsActive() {
00329 return Genesis::current() != NULL;
00330 }
00331
00332
00333 Handle<Context> Bootstrapper::CreateEnvironment(
00334 Handle<Object> global_object,
00335 v8::Handle<v8::ObjectTemplate> global_template,
00336 v8::ExtensionConfiguration* extensions) {
00337 Genesis genesis(global_object, global_template, extensions);
00338 return genesis.result();
00339 }
00340
00341
00342 static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
00343
00344 Handle<Map> old_to_map = Handle<Map>(object->map());
00345 Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map);
00346 new_to_map->set_prototype(*proto);
00347 object->set_map(*new_to_map);
00348 }
00349
00350
00351 void Bootstrapper::DetachGlobal(Handle<Context> env) {
00352 JSGlobalProxy::cast(env->global_proxy())->set_context(*Factory::null_value());
00353 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
00354 Factory::null_value());
00355 env->set_global_proxy(env->global());
00356 env->global()->set_global_receiver(env->global());
00357 }
00358
00359
00360 Genesis::~Genesis() {
00361 ASSERT(current_ == this);
00362 current_ = previous_;
00363 }
00364
00365
00366 static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
00367 const char* name,
00368 InstanceType type,
00369 int instance_size,
00370 Handle<JSObject> prototype,
00371 Builtins::Name call,
00372 bool is_ecma_native) {
00373 Handle<String> symbol = Factory::LookupAsciiSymbol(name);
00374 Handle<Code> call_code = Handle<Code>(Builtins::builtin(call));
00375 Handle<JSFunction> function =
00376 Factory::NewFunctionWithPrototype(symbol,
00377 type,
00378 instance_size,
00379 prototype,
00380 call_code,
00381 is_ecma_native);
00382 SetProperty(target, symbol, function, DONT_ENUM);
00383 if (is_ecma_native) {
00384 function->shared()->set_instance_class_name(*symbol);
00385 }
00386 return function;
00387 }
00388
00389
00390 Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
00391 bool make_prototype_read_only,
00392 bool make_prototype_enumerable) {
00393 Handle<DescriptorArray> result = Factory::empty_descriptor_array();
00394
00395
00396 PropertyAttributes attributes = static_cast<PropertyAttributes>(
00397 (make_prototype_enumerable ? 0 : DONT_ENUM)
00398 | DONT_DELETE
00399 | (make_prototype_read_only ? READ_ONLY : 0));
00400 result =
00401 Factory::CopyAppendProxyDescriptor(
00402 result,
00403 Factory::prototype_symbol(),
00404 Factory::NewProxy(&Accessors::FunctionPrototype),
00405 attributes);
00406
00407 attributes =
00408 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
00409
00410 result =
00411 Factory::CopyAppendProxyDescriptor(
00412 result,
00413 Factory::length_symbol(),
00414 Factory::NewProxy(&Accessors::FunctionLength),
00415 attributes);
00416
00417
00418 result =
00419 Factory::CopyAppendProxyDescriptor(
00420 result,
00421 Factory::name_symbol(),
00422 Factory::NewProxy(&Accessors::FunctionName),
00423 attributes);
00424
00425
00426 result =
00427 Factory::CopyAppendProxyDescriptor(
00428 result,
00429 Factory::arguments_symbol(),
00430 Factory::NewProxy(&Accessors::FunctionArguments),
00431 attributes);
00432
00433
00434 result =
00435 Factory::CopyAppendProxyDescriptor(
00436 result,
00437 Factory::caller_symbol(),
00438 Factory::NewProxy(&Accessors::FunctionCaller),
00439 attributes);
00440
00441 return result;
00442 }
00443
00444
00445 void Genesis::CreateRoots(v8::Handle<v8::ObjectTemplate> global_template,
00446 Handle<Object> global_object) {
00447 HandleScope scope;
00448
00449
00450
00451
00452 global_context_ =
00453 Handle<Context>::cast(
00454 GlobalHandles::Create(*Factory::NewGlobalContext()));
00455 Top::set_context(*global_context());
00456
00457
00458 v8::NeanderArray listeners;
00459 global_context()->set_message_listeners(*listeners.value());
00460
00461
00462 v8::NeanderArray debug_event_listeners;
00463 global_context()->set_debug_event_listeners(*debug_event_listeners.value());
00464
00465
00466 Handle<Map> fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
00467 global_context()->set_function_instance_map(*fm);
00468
00469
00470 Handle<DescriptorArray> function_map_descriptors =
00471 ComputeFunctionInstanceDescriptor(false, true);
00472 fm->set_instance_descriptors(*function_map_descriptors);
00473
00474
00475 fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
00476 global_context()->set_function_map(*fm);
00477 function_map_descriptors = ComputeFunctionInstanceDescriptor(true);
00478 fm->set_instance_descriptors(*function_map_descriptors);
00479
00480 Handle<String> object_name = Handle<String>(Heap::Object_symbol());
00481
00482 {
00483 Handle<JSFunction> object_fun =
00484 Factory::NewFunction(object_name, Factory::null_value());
00485 Handle<Map> object_function_map =
00486 Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
00487 object_fun->set_initial_map(*object_function_map);
00488 object_function_map->set_constructor(*object_fun);
00489
00490 global_context()->set_object_function(*object_fun);
00491
00492
00493 Handle<JSObject> prototype = Factory::NewJSObject(Top::object_function(),
00494 TENURED);
00495
00496 global_context()->set_initial_object_prototype(*prototype);
00497 SetPrototype(object_fun, prototype);
00498 object_function_map->
00499 set_instance_descriptors(Heap::empty_descriptor_array());
00500 }
00501
00502
00503
00504 Handle<String> symbol = Factory::LookupAsciiSymbol("Empty");
00505 Handle<JSFunction> empty_function =
00506 Factory::NewFunction(symbol, Factory::null_value());
00507
00508 {
00509 Handle<Code> code =
00510 Handle<Code>(Builtins::builtin(Builtins::EmptyFunction));
00511 Handle<String> source = Factory::NewStringFromAscii(CStrVector("() {}"));
00512
00513 empty_function->set_code(*code);
00514 empty_function->shared()->set_script(*Factory::NewScript(source));
00515 empty_function->shared()->set_start_position(0);
00516 empty_function->shared()->set_end_position(source->length());
00517 empty_function->shared()->DontAdaptArguments();
00518 global_context()->function_map()->set_prototype(*empty_function);
00519 global_context()->function_instance_map()->set_prototype(*empty_function);
00520
00521
00522 Handle<Map> empty_fm = Factory::CopyMap(fm);
00523 empty_fm->set_instance_descriptors(*function_map_descriptors);
00524 empty_fm->set_prototype(global_context()->object_function()->prototype());
00525 empty_function->set_map(*empty_fm);
00526 }
00527
00528 {
00529
00530 Handle<JSGlobalObject> object;
00531 {
00532 Handle<JSFunction> js_global_function;
00533 Handle<ObjectTemplateInfo> js_global_template;
00534 if (!global_template.IsEmpty()) {
00535
00536 Handle<ObjectTemplateInfo> data =
00537 v8::Utils::OpenHandle(*global_template);
00538 Handle<FunctionTemplateInfo> global_constructor =
00539 Handle<FunctionTemplateInfo>(
00540 FunctionTemplateInfo::cast(data->constructor()));
00541 Handle<Object> proto_template(global_constructor->prototype_template());
00542 if (!proto_template->IsUndefined()) {
00543 js_global_template =
00544 Handle<ObjectTemplateInfo>::cast(proto_template);
00545 }
00546 }
00547
00548 if (js_global_template.is_null()) {
00549 Handle<String> name = Handle<String>(Heap::empty_symbol());
00550 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
00551 js_global_function =
00552 Factory::NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
00553 JSGlobalObject::kSize, code, true);
00554
00555
00556 Handle<JSObject> prototype =
00557 Handle<JSObject>(
00558 JSObject::cast(js_global_function->instance_prototype()));
00559 SetProperty(prototype, Factory::constructor_symbol(),
00560 Top::object_function(), NONE);
00561 } else {
00562 Handle<FunctionTemplateInfo> js_global_constructor(
00563 FunctionTemplateInfo::cast(js_global_template->constructor()));
00564 js_global_function =
00565 Factory::CreateApiFunction(js_global_constructor,
00566 Factory::InnerGlobalObject);
00567 }
00568
00569 js_global_function->initial_map()->set_is_hidden_prototype();
00570 SetExpectedNofProperties(js_global_function, 100);
00571 object = Handle<JSGlobalObject>::cast(
00572 Factory::NewJSObject(js_global_function, TENURED));
00573 }
00574
00575
00576 object->set_global_context(*global_context());
00577
00578
00579 Handle<JSGlobalProxy> global_proxy;
00580 {
00581 Handle<JSFunction> global_proxy_function;
00582 if (global_template.IsEmpty()) {
00583 Handle<String> name = Handle<String>(Heap::empty_symbol());
00584 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
00585 global_proxy_function =
00586 Factory::NewFunction(name, JS_GLOBAL_PROXY_TYPE,
00587 JSGlobalProxy::kSize, code, true);
00588 } else {
00589 Handle<ObjectTemplateInfo> data =
00590 v8::Utils::OpenHandle(*global_template);
00591 Handle<FunctionTemplateInfo> global_constructor(
00592 FunctionTemplateInfo::cast(data->constructor()));
00593 global_proxy_function =
00594 Factory::CreateApiFunction(global_constructor,
00595 Factory::OuterGlobalObject);
00596 }
00597
00598 Handle<String> global_name = Factory::LookupAsciiSymbol("global");
00599 global_proxy_function->shared()->set_instance_class_name(*global_name);
00600 global_proxy_function->initial_map()->set_is_access_check_needed();
00601
00602
00603
00604 if (global_object.location() != NULL) {
00605 ASSERT(global_object->IsJSGlobalProxy());
00606 global_proxy =
00607 ReinitializeJSGlobalProxy(
00608 global_proxy_function,
00609 Handle<JSGlobalProxy>::cast(global_object));
00610 } else {
00611 global_proxy = Handle<JSGlobalProxy>::cast(
00612 Factory::NewJSObject(global_proxy_function, TENURED));
00613 }
00614
00615
00616
00617
00618
00619 object->set_global_receiver(*global_proxy);
00620 global_proxy->set_context(*global_context());
00621 }
00622
00623 {
00624
00625 global_context()->set_closure(*empty_function);
00626 global_context()->set_fcontext(*global_context());
00627 global_context()->set_previous(NULL);
00628
00629
00630 global_context()->set_extension(*object);
00631 global_context()->set_global(*object);
00632 global_context()->set_global_proxy(*global_proxy);
00633
00634 global_context()->set_security_token(*object);
00635 }
00636
00637 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
00638 SetProperty(global, object_name, Top::object_function(), DONT_ENUM);
00639 }
00640
00641 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
00642
00643
00644 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
00645 empty_function, Builtins::Illegal, true);
00646
00647 {
00648 Handle<JSFunction> array_function =
00649 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
00650 Top::initial_object_prototype(), Builtins::ArrayCode,
00651 true);
00652 array_function->shared()->DontAdaptArguments();
00653
00654
00655
00656 array_function->shared()->set_length(1);
00657 Handle<DescriptorArray> array_descriptors =
00658 Factory::CopyAppendProxyDescriptor(
00659 Factory::empty_descriptor_array(),
00660 Factory::length_symbol(),
00661 Factory::NewProxy(&Accessors::ArrayLength),
00662 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
00663
00664
00665 global_context()->set_js_array_map(array_function->initial_map());
00666 global_context()->js_array_map()->set_instance_descriptors(
00667 *array_descriptors);
00668
00669
00670
00671
00672 global_context()->set_array_function(*array_function);
00673 }
00674
00675 {
00676 Handle<JSFunction> number_fun =
00677 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
00678 Top::initial_object_prototype(), Builtins::Illegal,
00679 true);
00680 global_context()->set_number_function(*number_fun);
00681 }
00682
00683 {
00684 Handle<JSFunction> boolean_fun =
00685 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
00686 Top::initial_object_prototype(), Builtins::Illegal,
00687 true);
00688 global_context()->set_boolean_function(*boolean_fun);
00689 }
00690
00691 {
00692 Handle<JSFunction> string_fun =
00693 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
00694 Top::initial_object_prototype(), Builtins::Illegal,
00695 true);
00696 global_context()->set_string_function(*string_fun);
00697
00698 Handle<DescriptorArray> string_descriptors =
00699 Factory::CopyAppendProxyDescriptor(
00700 Factory::empty_descriptor_array(),
00701 Factory::length_symbol(),
00702 Factory::NewProxy(&Accessors::StringLength),
00703 static_cast<PropertyAttributes>(DONT_ENUM |
00704 DONT_DELETE |
00705 READ_ONLY));
00706
00707 Handle<Map> string_map =
00708 Handle<Map>(global_context()->string_function()->initial_map());
00709 string_map->set_instance_descriptors(*string_descriptors);
00710 }
00711
00712 {
00713
00714 Handle<JSFunction> date_fun =
00715 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
00716 Top::initial_object_prototype(), Builtins::Illegal,
00717 true);
00718
00719 global_context()->set_date_function(*date_fun);
00720 }
00721
00722
00723 {
00724
00725 Handle<JSFunction> regexp_fun =
00726 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
00727 Top::initial_object_prototype(), Builtins::Illegal,
00728 true);
00729
00730 global_context()->set_regexp_function(*regexp_fun);
00731 }
00732
00733 {
00734
00735
00736
00737 Handle<String> symbol = Factory::LookupAsciiSymbol("Arguments");
00738 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
00739 Handle<JSObject> prototype =
00740 Handle<JSObject>(
00741 JSObject::cast(global_context()->object_function()->prototype()));
00742
00743 Handle<JSFunction> function =
00744 Factory::NewFunctionWithPrototype(symbol,
00745 JS_OBJECT_TYPE,
00746 JSObject::kHeaderSize,
00747 prototype,
00748 code,
00749 false);
00750 ASSERT(!function->has_initial_map());
00751 function->shared()->set_instance_class_name(*symbol);
00752 function->shared()->set_expected_nof_properties(2);
00753 Handle<JSObject> result = Factory::NewJSObject(function);
00754
00755 global_context()->set_arguments_boilerplate(*result);
00756
00757
00758 SetProperty(result, Factory::callee_symbol(),
00759 Factory::undefined_value(),
00760 DONT_ENUM);
00761 SetProperty(result, Factory::length_symbol(),
00762 Factory::undefined_value(),
00763 DONT_ENUM);
00764
00765 #ifdef DEBUG
00766 LookupResult lookup;
00767 result->LocalLookup(Heap::callee_symbol(), &lookup);
00768 ASSERT(lookup.IsValid() && (lookup.type() == FIELD));
00769 ASSERT(lookup.GetFieldIndex() == Heap::arguments_callee_index);
00770
00771 result->LocalLookup(Heap::length_symbol(), &lookup);
00772 ASSERT(lookup.IsValid() && (lookup.type() == FIELD));
00773 ASSERT(lookup.GetFieldIndex() == Heap::arguments_length_index);
00774
00775 ASSERT(result->map()->inobject_properties() > Heap::arguments_callee_index);
00776 ASSERT(result->map()->inobject_properties() > Heap::arguments_length_index);
00777
00778
00779 ASSERT(result->HasFastProperties());
00780 ASSERT(result->HasFastElements());
00781 #endif
00782 }
00783
00784 {
00785
00786 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
00787 Handle<JSFunction> context_extension_fun =
00788 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE,
00789 JSObject::kHeaderSize, code, true);
00790
00791 Handle<String> name = Factory::LookupAsciiSymbol("context_extension");
00792 context_extension_fun->shared()->set_instance_class_name(*name);
00793 global_context()->set_context_extension_function(*context_extension_fun);
00794 }
00795
00796
00797 Handle<Code> code =
00798 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsFunction));
00799 Handle<JSFunction> delegate =
00800 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE,
00801 JSObject::kHeaderSize, code, true);
00802 global_context()->set_call_as_function_delegate(*delegate);
00803 delegate->shared()->DontAdaptArguments();
00804
00805 global_context()->set_special_function_table(Heap::empty_fixed_array());
00806
00807
00808 global_context()->set_out_of_memory(Heap::false_value());
00809 }
00810
00811
00812 bool Genesis::CompileBuiltin(int index) {
00813 Vector<const char> name = Natives::GetScriptName(index);
00814 Handle<String> source_code = Bootstrapper::NativesSourceLookup(index);
00815 return CompileNative(name, source_code);
00816 }
00817
00818
00819 bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
00820 HandleScope scope;
00821 Debugger::set_compiling_natives(true);
00822 bool result =
00823 CompileScriptCached(name, source, &natives_cache, NULL, true);
00824 ASSERT(Top::has_pending_exception() != result);
00825 if (!result) Top::clear_pending_exception();
00826 Debugger::set_compiling_natives(false);
00827 return result;
00828 }
00829
00830
00831 bool Genesis::CompileScriptCached(Vector<const char> name,
00832 Handle<String> source,
00833 SourceCodeCache* cache,
00834 v8::Extension* extension,
00835 bool use_runtime_context) {
00836 HandleScope scope;
00837 Handle<JSFunction> boilerplate;
00838
00839
00840
00841 if (!cache->Lookup(name, &boilerplate)) {
00842 #ifdef DEBUG
00843 ASSERT(source->IsAsciiRepresentation());
00844 #endif
00845 Handle<String> script_name = Factory::NewStringFromUtf8(name);
00846 boilerplate =
00847 Compiler::Compile(source, script_name, 0, 0, extension, NULL);
00848 if (boilerplate.is_null()) return false;
00849 cache->Add(name, boilerplate);
00850 }
00851
00852
00853
00854
00855 ASSERT(Top::context()->IsGlobalContext());
00856 Handle<Context> context =
00857 Handle<Context>(use_runtime_context
00858 ? Top::context()->runtime_context()
00859 : Top::context());
00860 Handle<JSFunction> fun =
00861 Factory::NewFunctionFromBoilerplate(boilerplate, context);
00862
00863
00864
00865 Handle<Object> receiver =
00866 Handle<Object>(use_runtime_context
00867 ? Top::context()->builtins()
00868 : Top::context()->global());
00869 bool has_pending_exception;
00870 Handle<Object> result =
00871 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
00872 if (has_pending_exception) return false;
00873 return PendingFixups::Process(
00874 Handle<JSBuiltinsObject>(Top::context()->builtins()));
00875 }
00876
00877
00878 #define INSTALL_NATIVE(Type, name, var) \
00879 Handle<String> var##_name = Factory::LookupAsciiSymbol(name); \
00880 global_context()->set_##var(Type::cast(global_context()-> \
00881 builtins()-> \
00882 GetProperty(*var##_name)));
00883
00884 void Genesis::InstallNativeFunctions() {
00885 HandleScope scope;
00886 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
00887 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
00888 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
00889 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
00890 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
00891 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
00892 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
00893 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
00894 INSTALL_NATIVE(JSFunction, "ToBoolean", to_boolean_fun);
00895 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
00896 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
00897 configure_instance_fun);
00898 INSTALL_NATIVE(JSFunction, "MakeMessage", make_message_fun);
00899 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
00900 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
00901 }
00902
00903 #undef INSTALL_NATIVE
00904
00905
00906 bool Genesis::InstallNatives() {
00907 HandleScope scope;
00908
00909
00910
00911
00912 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
00913 Handle<JSFunction> builtins_fun =
00914 Factory::NewFunction(Factory::empty_symbol(), JS_BUILTINS_OBJECT_TYPE,
00915 JSBuiltinsObject::kSize, code, true);
00916
00917 Handle<String> name = Factory::LookupAsciiSymbol("builtins");
00918 builtins_fun->shared()->set_instance_class_name(*name);
00919 SetExpectedNofProperties(builtins_fun, 100);
00920
00921
00922 Handle<JSBuiltinsObject> builtins =
00923 Handle<JSBuiltinsObject>::cast(Factory::NewJSObject(builtins_fun,
00924 TENURED));
00925 builtins->set_builtins(*builtins);
00926 builtins->set_global_context(*global_context());
00927 builtins->set_global_receiver(*builtins);
00928
00929
00930
00931
00932
00933 static const PropertyAttributes attributes =
00934 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
00935 SetProperty(builtins, Factory::LookupAsciiSymbol("global"),
00936 Handle<Object>(global_context()->global()), attributes);
00937
00938
00939 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
00940
00941
00942 Handle<JSFunction> bridge =
00943 Factory::NewFunction(Factory::empty_symbol(), Factory::undefined_value());
00944 ASSERT(bridge->context() == *Top::global_context());
00945
00946
00947 Handle<Context> context =
00948 Factory::NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
00949 context->set_global(*builtins);
00950
00951 global_context()->set_runtime_context(*context);
00952
00953 {
00954
00955 Handle<JSFunction> script_fun =
00956 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
00957 Top::initial_object_prototype(), Builtins::Illegal,
00958 false);
00959 Handle<JSObject> prototype =
00960 Factory::NewJSObject(Top::object_function(), TENURED);
00961 SetPrototype(script_fun, prototype);
00962 global_context()->set_script_function(*script_fun);
00963
00964
00965 PropertyAttributes common_attributes =
00966 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
00967 Handle<Proxy> proxy_source = Factory::NewProxy(&Accessors::ScriptSource);
00968 Handle<DescriptorArray> script_descriptors =
00969 Factory::CopyAppendProxyDescriptor(
00970 Factory::empty_descriptor_array(),
00971 Factory::LookupAsciiSymbol("source"),
00972 proxy_source,
00973 common_attributes);
00974 Handle<Proxy> proxy_data = Factory::NewProxy(&Accessors::ScriptName);
00975 script_descriptors =
00976 Factory::CopyAppendProxyDescriptor(
00977 script_descriptors,
00978 Factory::LookupAsciiSymbol("name"),
00979 proxy_data,
00980 common_attributes);
00981 Handle<Proxy> proxy_line_offset =
00982 Factory::NewProxy(&Accessors::ScriptLineOffset);
00983 script_descriptors =
00984 Factory::CopyAppendProxyDescriptor(
00985 script_descriptors,
00986 Factory::LookupAsciiSymbol("line_offset"),
00987 proxy_line_offset,
00988 common_attributes);
00989 Handle<Proxy> proxy_column_offset =
00990 Factory::NewProxy(&Accessors::ScriptColumnOffset);
00991 script_descriptors =
00992 Factory::CopyAppendProxyDescriptor(
00993 script_descriptors,
00994 Factory::LookupAsciiSymbol("column_offset"),
00995 proxy_column_offset,
00996 common_attributes);
00997 Handle<Proxy> proxy_type = Factory::NewProxy(&Accessors::ScriptType);
00998 script_descriptors =
00999 Factory::CopyAppendProxyDescriptor(
01000 script_descriptors,
01001 Factory::LookupAsciiSymbol("type"),
01002 proxy_type,
01003 common_attributes);
01004
01005 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
01006 script_map->set_instance_descriptors(*script_descriptors);
01007
01008
01009 Handle<Script> script = Factory::NewScript(Factory::empty_string());
01010 global_context()->set_empty_script(*script);
01011 }
01012
01013 if (FLAG_natives_file == NULL) {
01014
01015 for (int i = Natives::GetDelayCount();
01016 i < Natives::GetBuiltinsCount();
01017 i++) {
01018 if (!CompileBuiltin(i)) return false;
01019 }
01020
01021
01022 SetupLazy(Handle<JSFunction>(global_context()->date_function()),
01023 Natives::GetIndex("date"),
01024 Top::global_context(),
01025 Handle<Context>(Top::context()->runtime_context()));
01026 SetupLazy(Handle<JSFunction>(global_context()->regexp_function()),
01027 Natives::GetIndex("regexp"),
01028 Top::global_context(),
01029 Handle<Context>(Top::context()->runtime_context()));
01030
01031 } else if (strlen(FLAG_natives_file) != 0) {
01032
01033
01034 bool exists;
01035 Vector<const char> source = ReadFile(FLAG_natives_file, &exists);
01036 Handle<String> source_string = Factory::NewStringFromAscii(source);
01037 if (source.is_empty()) return false;
01038 bool result = CompileNative(CStrVector(FLAG_natives_file), source_string);
01039 if (!result) return false;
01040
01041 } else {
01042
01043 PrintF("Warning: Running without installed natives!\n");
01044 return true;
01045 }
01046
01047 InstallNativeFunctions();
01048
01049
01050 { Handle<String> key = Factory::function_class_symbol();
01051 Handle<JSFunction> function =
01052 Handle<JSFunction>::cast(GetProperty(Top::global(), key));
01053 Handle<JSObject> proto =
01054 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
01055
01056
01057 Handle<JSFunction> call =
01058 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
01059 Factory::NewJSObject(Top::object_function(), TENURED),
01060 Builtins::FunctionCall,
01061 false);
01062 Handle<JSFunction> apply =
01063 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
01064 Factory::NewJSObject(Top::object_function(), TENURED),
01065 Builtins::FunctionApply,
01066 false);
01067
01068
01069
01070
01071 call->shared()->DontAdaptArguments();
01072 ASSERT(call->is_compiled());
01073
01074
01075 apply->shared()->set_formal_parameter_count(2);
01076
01077
01078 call->shared()->set_length(1);
01079 apply->shared()->set_length(2);
01080 }
01081
01082
01083
01084
01085 ASSERT(builtins->HasFastProperties());
01086 #ifdef DEBUG
01087 builtins->Verify();
01088 #endif
01089 return true;
01090 }
01091
01092
01093 bool Genesis::InstallSpecialObjects() {
01094 HandleScope scope;
01095 Handle<JSGlobalObject> js_global(
01096 JSGlobalObject::cast(global_context()->global()));
01097
01098 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
01099 Handle<String> natives_string =
01100 Factory::LookupAsciiSymbol(FLAG_expose_natives_as);
01101 SetProperty(js_global, natives_string,
01102 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
01103 }
01104
01105
01106 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
01107
01108
01109 if (!Debug::Load())
01110 return true;
01111
01112
01113
01114 Debug::debug_context()->set_security_token(
01115 global_context()->security_token());
01116
01117 Handle<String> debug_string =
01118 Factory::LookupAsciiSymbol(FLAG_expose_debug_as);
01119 SetProperty(js_global, debug_string,
01120 Handle<Object>(Debug::debug_context()->global_proxy()), DONT_ENUM);
01121 }
01122
01123 return true;
01124 }
01125
01126
01127 bool Genesis::InstallExtensions(v8::ExtensionConfiguration* extensions) {
01128
01129 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
01130 while (current != NULL) {
01131 current->set_state(v8::UNVISITED);
01132 current = current->next();
01133 }
01134
01135 current = v8::RegisteredExtension::first_extension();
01136 while (current != NULL) {
01137 if (current->extension()->auto_enable())
01138 InstallExtension(current);
01139 current = current->next();
01140 }
01141
01142 if (FLAG_expose_gc) InstallExtension("v8/gc");
01143
01144 if (extensions == NULL) return true;
01145
01146 int count = v8::ImplementationUtilities::GetNameCount(extensions);
01147 const char** names = v8::ImplementationUtilities::GetNames(extensions);
01148 for (int i = 0; i < count; i++) {
01149 if (!InstallExtension(names[i]))
01150 return false;
01151 }
01152
01153 return true;
01154 }
01155
01156
01157
01158
01159 bool Genesis::InstallExtension(const char* name) {
01160 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
01161
01162 while (current != NULL) {
01163 if (strcmp(name, current->extension()->name()) == 0) break;
01164 current = current->next();
01165 }
01166
01167 if (current == NULL) {
01168 v8::Utils::ReportApiFailure(
01169 "v8::Context::New()", "Cannot find required extension");
01170 return false;
01171 }
01172 return InstallExtension(current);
01173 }
01174
01175
01176 bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
01177 HandleScope scope;
01178
01179 if (current->state() == v8::INSTALLED) return true;
01180
01181
01182 if (current->state() == v8::VISITED) {
01183 v8::Utils::ReportApiFailure(
01184 "v8::Context::New()", "Circular extension dependency");
01185 return false;
01186 }
01187 ASSERT(current->state() == v8::UNVISITED);
01188 current->set_state(v8::VISITED);
01189 v8::Extension* extension = current->extension();
01190
01191 for (int i = 0; i < extension->dependency_count(); i++) {
01192 if (!InstallExtension(extension->dependencies()[i])) return false;
01193 }
01194 Vector<const char> source = CStrVector(extension->source());
01195 Handle<String> source_code = Factory::NewStringFromAscii(source);
01196 bool result = CompileScriptCached(CStrVector(extension->name()),
01197 source_code,
01198 &extensions_cache, extension,
01199 false);
01200 ASSERT(Top::has_pending_exception() != result);
01201 if (!result) {
01202 Top::clear_pending_exception();
01203 v8::Utils::ReportApiFailure(
01204 "v8::Context::New()", "Error installing extension");
01205 }
01206 current->set_state(v8::INSTALLED);
01207 return result;
01208 }
01209
01210
01211 bool Genesis::ConfigureGlobalObjects(
01212 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
01213 Handle<JSObject> global_proxy(
01214 JSObject::cast(global_context()->global_proxy()));
01215 Handle<JSObject> js_global(JSObject::cast(global_context()->global()));
01216
01217 if (!global_proxy_template.IsEmpty()) {
01218
01219 Handle<ObjectTemplateInfo> proxy_data =
01220 v8::Utils::OpenHandle(*global_proxy_template);
01221 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
01222
01223
01224 Handle<FunctionTemplateInfo> proxy_constructor(
01225 FunctionTemplateInfo::cast(proxy_data->constructor()));
01226 if (!proxy_constructor->prototype_template()->IsUndefined()) {
01227 Handle<ObjectTemplateInfo> inner_data(
01228 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
01229 if (!ConfigureApiObject(js_global, inner_data)) return false;
01230 }
01231 }
01232
01233 SetObjectPrototype(global_proxy, js_global);
01234 return true;
01235 }
01236
01237
01238 bool Genesis::ConfigureApiObject(Handle<JSObject> object,
01239 Handle<ObjectTemplateInfo> object_template) {
01240 ASSERT(!object_template.is_null());
01241 ASSERT(object->IsInstanceOf(
01242 FunctionTemplateInfo::cast(object_template->constructor())));
01243
01244 bool pending_exception = false;
01245 Handle<JSObject> obj =
01246 Execution::InstantiateObject(object_template, &pending_exception);
01247 if (pending_exception) {
01248 ASSERT(Top::has_pending_exception());
01249 Top::clear_pending_exception();
01250 return false;
01251 }
01252 TransferObject(obj, object);
01253 return true;
01254 }
01255
01256
01257 void Genesis::TransferNamedProperties(Handle<JSObject> from,
01258 Handle<JSObject> to) {
01259 if (from->HasFastProperties()) {
01260 Handle<DescriptorArray> descs =
01261 Handle<DescriptorArray>(from->map()->instance_descriptors());
01262 int offset = 0;
01263 while (true) {
01264
01265
01266 DescriptorReader stream(*descs, offset);
01267 if (stream.eos()) break;
01268
01269
01270 offset = stream.next_position();
01271 PropertyDetails details = stream.GetDetails();
01272 switch (details.type()) {
01273 case FIELD: {
01274 HandleScope inner;
01275 Handle<String> key = Handle<String>(stream.GetKey());
01276 int index = stream.GetFieldIndex();
01277 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
01278 SetProperty(to, key, value, details.attributes());
01279 break;
01280 }
01281 case CONSTANT_FUNCTION: {
01282 HandleScope inner;
01283 Handle<String> key = Handle<String>(stream.GetKey());
01284 Handle<JSFunction> fun =
01285 Handle<JSFunction>(stream.GetConstantFunction());
01286 SetProperty(to, key, fun, details.attributes());
01287 break;
01288 }
01289 case CALLBACKS: {
01290 LookupResult result;
01291 to->LocalLookup(stream.GetKey(), &result);
01292
01293 if (result.IsValid()) continue;
01294 HandleScope inner;
01295 Handle<DescriptorArray> inst_descs =
01296 Handle<DescriptorArray>(to->map()->instance_descriptors());
01297 Handle<String> key = Handle<String>(stream.GetKey());
01298 Handle<Object> entry = Handle<Object>(stream.GetCallbacksObject());
01299 inst_descs = Factory::CopyAppendProxyDescriptor(inst_descs,
01300 key,
01301 entry,
01302 details.attributes());
01303 to->map()->set_instance_descriptors(*inst_descs);
01304 break;
01305 }
01306 case MAP_TRANSITION:
01307 case CONSTANT_TRANSITION:
01308 case NULL_DESCRIPTOR:
01309
01310 break;
01311 case NORMAL:
01312
01313 case INTERCEPTOR:
01314
01315 UNREACHABLE();
01316 break;
01317 }
01318 }
01319 } else {
01320 Handle<Dictionary> properties =
01321 Handle<Dictionary>(from->property_dictionary());
01322 int capacity = properties->Capacity();
01323 for (int i = 0; i < capacity; i++) {
01324 Object* raw_key(properties->KeyAt(i));
01325 if (properties->IsKey(raw_key)) {
01326 ASSERT(raw_key->IsString());
01327
01328 LookupResult result;
01329 to->LocalLookup(String::cast(raw_key), &result);
01330 if (result.IsValid()) continue;
01331
01332 Handle<String> key = Handle<String>(String::cast(raw_key));
01333 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
01334 PropertyDetails details = properties->DetailsAt(i);
01335 SetProperty(to, key, value, details.attributes());
01336 }
01337 }
01338 }
01339 }
01340
01341
01342 void Genesis::TransferIndexedProperties(Handle<JSObject> from,
01343 Handle<JSObject> to) {
01344
01345 Handle<FixedArray> from_elements =
01346 Handle<FixedArray>(FixedArray::cast(from->elements()));
01347 Handle<FixedArray> to_elements = Factory::CopyFixedArray(from_elements);
01348 to->set_elements(*to_elements);
01349 }
01350
01351
01352 void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
01353 HandleScope outer;
01354
01355 ASSERT(!from->IsJSArray());
01356 ASSERT(!to->IsJSArray());
01357
01358 TransferNamedProperties(from, to);
01359 TransferIndexedProperties(from, to);
01360
01361
01362 Handle<Map> old_to_map = Handle<Map>(to->map());
01363 Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map);
01364 new_to_map->set_prototype(from->map()->prototype());
01365 to->set_map(*new_to_map);
01366 }
01367
01368
01369 void Genesis::MakeFunctionInstancePrototypeWritable() {
01370
01371
01372 HandleScope scope;
01373
01374 Handle<DescriptorArray> function_map_descriptors =
01375 ComputeFunctionInstanceDescriptor(false, true);
01376 Handle<Map> fm = Factory::CopyMap(Top::function_map());
01377 fm->set_instance_descriptors(*function_map_descriptors);
01378 Top::context()->global_context()->set_function_map(*fm);
01379 }
01380
01381
01382 void Genesis::AddSpecialFunction(Handle<JSObject> prototype,
01383 const char* name,
01384 Handle<Code> code) {
01385 Handle<String> key = Factory::LookupAsciiSymbol(name);
01386 Handle<Object> value = Handle<Object>(prototype->GetProperty(*key));
01387 if (value->IsJSFunction()) {
01388 Handle<JSFunction> optimized = Factory::NewFunction(key,
01389 JS_OBJECT_TYPE,
01390 JSObject::kHeaderSize,
01391 code,
01392 false);
01393 optimized->shared()->DontAdaptArguments();
01394 int len = global_context()->special_function_table()->length();
01395 Handle<FixedArray> new_array = Factory::NewFixedArray(len + 3);
01396 for (int index = 0; index < len; index++) {
01397 new_array->set(index,
01398 global_context()->special_function_table()->get(index));
01399 }
01400 new_array->set(len+0, *prototype);
01401 new_array->set(len+1, *value);
01402 new_array->set(len+2, *optimized);
01403 global_context()->set_special_function_table(*new_array);
01404 }
01405 }
01406
01407
01408 void Genesis::BuildSpecialFunctionTable() {
01409 HandleScope scope;
01410 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
01411
01412 Handle<JSFunction> function =
01413 Handle<JSFunction>(
01414 JSFunction::cast(global->GetProperty(Heap::Array_symbol())));
01415 Handle<JSObject> prototype =
01416 Handle<JSObject>(JSObject::cast(function->prototype()));
01417 AddSpecialFunction(prototype, "pop",
01418 Handle<Code>(Builtins::builtin(Builtins::ArrayPop)));
01419 AddSpecialFunction(prototype, "push",
01420 Handle<Code>(Builtins::builtin(Builtins::ArrayPush)));
01421 }
01422
01423
01424 Genesis::Genesis(Handle<Object> global_object,
01425 v8::Handle<v8::ObjectTemplate> global_template,
01426 v8::ExtensionConfiguration* extensions) {
01427
01428
01429
01430 previous_ = current_;
01431 current_ = this;
01432 result_ = NULL;
01433
01434
01435 if (!V8::HasBeenSetup() && !V8::Initialize(NULL)) return;
01436
01437
01438
01439 HandleScope scope;
01440 SaveContext context;
01441
01442 CreateRoots(global_template, global_object);
01443 if (!InstallNatives()) return;
01444
01445 MakeFunctionInstancePrototypeWritable();
01446 BuildSpecialFunctionTable();
01447
01448 if (!ConfigureGlobalObjects(global_template)) return;
01449
01450 if (!InstallExtensions(extensions)) return;
01451
01452 if (!InstallSpecialObjects()) return;
01453
01454 result_ = global_context_;
01455 }
01456
01457 } }