| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 // 'from'. | 238 // 'from'. |
| 239 void TransferObject(Handle<JSObject> from, Handle<JSObject> to); | 239 void TransferObject(Handle<JSObject> from, Handle<JSObject> to); |
| 240 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to); | 240 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 241 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to); | 241 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 242 | 242 |
| 243 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor( | 243 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor( |
| 244 bool make_prototype_read_only, | 244 bool make_prototype_read_only, |
| 245 bool make_prototype_enumerable = false); | 245 bool make_prototype_enumerable = false); |
| 246 void MakeFunctionInstancePrototypeWritable(); | 246 void MakeFunctionInstancePrototypeWritable(); |
| 247 | 247 |
| 248 void AddSpecialFunction(Handle<JSObject> prototype, | 248 Handle<JSFunction> MakeFunctionForBuiltin(Handle<String> name, |
| 249 const char* name, | 249 Handle<Code> code); |
| 250 Handle<Code> code); | |
| 251 | 250 |
| 252 void BuildSpecialFunctionTable(); | 251 void OverrideWithSpecialFunction(Handle<JSObject> prototype, |
| 252 const char* name, |
| 253 Handle<Code> code); |
| 254 |
| 255 void InstallSpecialFunctions(); |
| 253 | 256 |
| 254 static bool CompileBuiltin(int index); | 257 static bool CompileBuiltin(int index); |
| 255 static bool CompileNative(Vector<const char> name, Handle<String> source); | 258 static bool CompileNative(Vector<const char> name, Handle<String> source); |
| 256 static bool CompileScriptCached(Vector<const char> name, | 259 static bool CompileScriptCached(Vector<const char> name, |
| 257 Handle<String> source, | 260 Handle<String> source, |
| 258 SourceCodeCache* cache, | 261 SourceCodeCache* cache, |
| 259 v8::Extension* extension, | 262 v8::Extension* extension, |
| 260 bool use_runtime_context); | 263 bool use_runtime_context); |
| 261 | 264 |
| 262 Handle<Context> result_; | 265 Handle<Context> result_; |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 770 // Setup the call-as-constructor delegate. | 773 // Setup the call-as-constructor delegate. |
| 771 Handle<Code> code = | 774 Handle<Code> code = |
| 772 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsConstructor)); | 775 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsConstructor)); |
| 773 Handle<JSFunction> delegate = | 776 Handle<JSFunction> delegate = |
| 774 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE, | 777 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE, |
| 775 JSObject::kHeaderSize, code, true); | 778 JSObject::kHeaderSize, code, true); |
| 776 global_context()->set_call_as_constructor_delegate(*delegate); | 779 global_context()->set_call_as_constructor_delegate(*delegate); |
| 777 delegate->shared()->DontAdaptArguments(); | 780 delegate->shared()->DontAdaptArguments(); |
| 778 } | 781 } |
| 779 | 782 |
| 780 global_context()->set_special_function_table(Heap::empty_fixed_array()); | |
| 781 | |
| 782 // Initialize the out of memory slot. | 783 // Initialize the out of memory slot. |
| 783 global_context()->set_out_of_memory(Heap::false_value()); | 784 global_context()->set_out_of_memory(Heap::false_value()); |
| 784 | 785 |
| 785 // Initialize the data slot. | 786 // Initialize the data slot. |
| 786 global_context()->set_data(Heap::undefined_value()); | 787 global_context()->set_data(Heap::undefined_value()); |
| 787 } | 788 } |
| 788 | 789 |
| 789 | 790 |
| 790 bool Genesis::CompileBuiltin(int index) { | 791 bool Genesis::CompileBuiltin(int index) { |
| 791 Vector<const char> name = Natives::GetScriptName(index); | 792 Vector<const char> name = Natives::GetScriptName(index); |
| (...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1450 HandleScope scope; | 1451 HandleScope scope; |
| 1451 | 1452 |
| 1452 Handle<DescriptorArray> function_map_descriptors = | 1453 Handle<DescriptorArray> function_map_descriptors = |
| 1453 ComputeFunctionInstanceDescriptor(false); | 1454 ComputeFunctionInstanceDescriptor(false); |
| 1454 Handle<Map> fm = Factory::CopyMapDropDescriptors(Top::function_map()); | 1455 Handle<Map> fm = Factory::CopyMapDropDescriptors(Top::function_map()); |
| 1455 fm->set_instance_descriptors(*function_map_descriptors); | 1456 fm->set_instance_descriptors(*function_map_descriptors); |
| 1456 Top::context()->global_context()->set_function_map(*fm); | 1457 Top::context()->global_context()->set_function_map(*fm); |
| 1457 } | 1458 } |
| 1458 | 1459 |
| 1459 | 1460 |
| 1460 void Genesis::AddSpecialFunction(Handle<JSObject> prototype, | 1461 Handle<JSFunction> Genesis::MakeFunctionForBuiltin(Handle<String> name, |
| 1461 const char* name, | 1462 Handle<Code> code) { |
| 1462 Handle<Code> code) { | 1463 Handle<JSFunction> optimized = Factory::NewFunction(name, |
| 1463 Handle<String> key = Factory::LookupAsciiSymbol(name); | 1464 JS_OBJECT_TYPE, |
| 1464 Handle<Object> value = Handle<Object>(prototype->GetProperty(*key)); | 1465 JSObject::kHeaderSize, |
| 1465 if (value->IsJSFunction()) { | 1466 code, |
| 1466 Handle<JSFunction> optimized = Factory::NewFunction(key, | 1467 false); |
| 1467 JS_OBJECT_TYPE, | 1468 optimized->shared()->DontAdaptArguments(); |
| 1468 JSObject::kHeaderSize, | 1469 return optimized; |
| 1469 code, | |
| 1470 false); | |
| 1471 optimized->shared()->DontAdaptArguments(); | |
| 1472 int len = global_context()->special_function_table()->length(); | |
| 1473 Handle<FixedArray> new_array = Factory::NewFixedArray(len + 3); | |
| 1474 for (int index = 0; index < len; index++) { | |
| 1475 new_array->set(index, | |
| 1476 global_context()->special_function_table()->get(index)); | |
| 1477 } | |
| 1478 new_array->set(len+0, *prototype); | |
| 1479 new_array->set(len+1, *value); | |
| 1480 new_array->set(len+2, *optimized); | |
| 1481 global_context()->set_special_function_table(*new_array); | |
| 1482 } | |
| 1483 } | 1470 } |
| 1484 | 1471 |
| 1485 | 1472 |
| 1486 void Genesis::BuildSpecialFunctionTable() { | 1473 void Genesis::OverrideWithSpecialFunction(Handle<JSObject> prototype, |
| 1474 const char* name, |
| 1475 Handle<Code> code) { |
| 1476 Handle<String> key = Factory::LookupAsciiSymbol(name); |
| 1477 Handle<Object> old_value = GetProperty(prototype, key); |
| 1478 // Check if the function is present in the first place. |
| 1479 // For example, FLAG_natives_file could affect if Array functions |
| 1480 // are installed at all. |
| 1481 if (!old_value->IsJSFunction()) return; |
| 1482 int old_length = Handle<JSFunction>::cast(old_value)->shared()->length(); |
| 1483 Handle<JSFunction> optimized = MakeFunctionForBuiltin(key, code); |
| 1484 optimized->shared()->set_length(old_length); |
| 1485 SetProperty(prototype, key, optimized, NONE); |
| 1486 } |
| 1487 |
| 1488 |
| 1489 void Genesis::InstallSpecialFunctions() { |
| 1487 HandleScope scope; | 1490 HandleScope scope; |
| 1488 Handle<JSObject> global = Handle<JSObject>(global_context()->global()); | 1491 Handle<JSObject> global = Handle<JSObject>(global_context()->global()); |
| 1489 // Add special versions for some Array.prototype functions. | 1492 // Add special versions for some Array.prototype functions. |
| 1490 Handle<JSFunction> function = | 1493 Handle<JSFunction> function = |
| 1491 Handle<JSFunction>( | 1494 Handle<JSFunction>( |
| 1492 JSFunction::cast(global->GetProperty(Heap::Array_symbol()))); | 1495 JSFunction::cast(global->GetProperty(Heap::Array_symbol()))); |
| 1493 Handle<JSObject> visible_prototype = | 1496 Handle<JSObject> visible_prototype = |
| 1494 Handle<JSObject>(JSObject::cast(function->prototype())); | 1497 Handle<JSObject>(JSObject::cast(function->prototype())); |
| 1495 // Remember to put those specializations on the hidden prototype if present. | 1498 // Remember to put those specializations on the hidden prototype if present. |
| 1496 Handle<JSObject> special_prototype; | 1499 Handle<JSObject> special_prototype; |
| 1497 Handle<Object> superproto(visible_prototype->GetPrototype()); | 1500 Handle<Object> superproto(visible_prototype->GetPrototype()); |
| 1498 if (superproto->IsJSObject() && | 1501 if (superproto->IsJSObject() && |
| 1499 JSObject::cast(*superproto)->map()->is_hidden_prototype()) { | 1502 JSObject::cast(*superproto)->map()->is_hidden_prototype()) { |
| 1500 special_prototype = Handle<JSObject>::cast(superproto); | 1503 special_prototype = Handle<JSObject>::cast(superproto); |
| 1501 } else { | 1504 } else { |
| 1502 special_prototype = visible_prototype; | 1505 special_prototype = visible_prototype; |
| 1503 } | 1506 } |
| 1504 AddSpecialFunction(special_prototype, "pop", | 1507 OverrideWithSpecialFunction( |
| 1505 Handle<Code>(Builtins::builtin(Builtins::ArrayPop))); | 1508 special_prototype, "pop", |
| 1506 AddSpecialFunction(special_prototype, "push", | 1509 Handle<Code>(Builtins::builtin(Builtins::ArrayPop))); |
| 1507 Handle<Code>(Builtins::builtin(Builtins::ArrayPush))); | 1510 OverrideWithSpecialFunction( |
| 1508 AddSpecialFunction(special_prototype, "shift", | 1511 special_prototype, "push", |
| 1509 Handle<Code>(Builtins::builtin(Builtins::ArrayShift))); | 1512 Handle<Code>(Builtins::builtin(Builtins::ArrayPush))); |
| 1510 AddSpecialFunction(special_prototype, "unshift", | 1513 OverrideWithSpecialFunction( |
| 1511 Handle<Code>(Builtins::builtin(Builtins::ArrayUnshift))); | 1514 special_prototype, "shift", |
| 1512 AddSpecialFunction(special_prototype, "slice", | 1515 Handle<Code>(Builtins::builtin(Builtins::ArrayShift))); |
| 1513 Handle<Code>(Builtins::builtin(Builtins::ArraySlice))); | 1516 OverrideWithSpecialFunction( |
| 1514 AddSpecialFunction(special_prototype, "splice", | 1517 special_prototype, "unshift", |
| 1515 Handle<Code>(Builtins::builtin(Builtins::ArraySplice))); | 1518 Handle<Code>(Builtins::builtin(Builtins::ArrayUnshift))); |
| 1519 OverrideWithSpecialFunction( |
| 1520 special_prototype, "slice", |
| 1521 Handle<Code>(Builtins::builtin(Builtins::ArraySlice))); |
| 1522 OverrideWithSpecialFunction( |
| 1523 special_prototype, "splice", |
| 1524 Handle<Code>(Builtins::builtin(Builtins::ArraySplice))); |
| 1516 } | 1525 } |
| 1517 | 1526 |
| 1518 | 1527 |
| 1519 Genesis::Genesis(Handle<Object> global_object, | 1528 Genesis::Genesis(Handle<Object> global_object, |
| 1520 v8::Handle<v8::ObjectTemplate> global_template, | 1529 v8::Handle<v8::ObjectTemplate> global_template, |
| 1521 v8::ExtensionConfiguration* extensions) { | 1530 v8::ExtensionConfiguration* extensions) { |
| 1522 // Link this genesis object into the stacked genesis chain. This | 1531 // Link this genesis object into the stacked genesis chain. This |
| 1523 // must be done before any early exits because the destructor | 1532 // must be done before any early exits because the destructor |
| 1524 // will always do unlinking. | 1533 // will always do unlinking. |
| 1525 previous_ = current_; | 1534 previous_ = current_; |
| 1526 current_ = this; | 1535 current_ = this; |
| 1527 result_ = Handle<Context>::null(); | 1536 result_ = Handle<Context>::null(); |
| 1528 | 1537 |
| 1529 // If V8 isn't running and cannot be initialized, just return. | 1538 // If V8 isn't running and cannot be initialized, just return. |
| 1530 if (!V8::IsRunning() && !V8::Initialize(NULL)) return; | 1539 if (!V8::IsRunning() && !V8::Initialize(NULL)) return; |
| 1531 | 1540 |
| 1532 // Before creating the roots we must save the context and restore it | 1541 // Before creating the roots we must save the context and restore it |
| 1533 // on all function exits. | 1542 // on all function exits. |
| 1534 HandleScope scope; | 1543 HandleScope scope; |
| 1535 SaveContext context; | 1544 SaveContext context; |
| 1536 | 1545 |
| 1537 CreateRoots(global_template, global_object); | 1546 CreateRoots(global_template, global_object); |
| 1538 | 1547 |
| 1539 if (!InstallNatives()) return; | 1548 if (!InstallNatives()) return; |
| 1540 | 1549 |
| 1541 MakeFunctionInstancePrototypeWritable(); | 1550 MakeFunctionInstancePrototypeWritable(); |
| 1542 BuildSpecialFunctionTable(); | 1551 InstallSpecialFunctions(); |
| 1543 | 1552 |
| 1544 if (!ConfigureGlobalObjects(global_template)) return; | 1553 if (!ConfigureGlobalObjects(global_template)) return; |
| 1545 | 1554 |
| 1546 if (!InstallExtensions(extensions)) return; | 1555 if (!InstallExtensions(extensions)) return; |
| 1547 | 1556 |
| 1548 if (!InstallSpecialObjects()) return; | 1557 if (!InstallSpecialObjects()) return; |
| 1549 | 1558 |
| 1550 result_ = global_context_; | 1559 result_ = global_context_; |
| 1551 } | 1560 } |
| 1552 | 1561 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1591 } | 1600 } |
| 1592 | 1601 |
| 1593 | 1602 |
| 1594 // Restore statics that are thread local. | 1603 // Restore statics that are thread local. |
| 1595 char* Genesis::RestoreState(char* from) { | 1604 char* Genesis::RestoreState(char* from) { |
| 1596 current_ = *reinterpret_cast<Genesis**>(from); | 1605 current_ = *reinterpret_cast<Genesis**>(from); |
| 1597 return from + sizeof(current_); | 1606 return from + sizeof(current_); |
| 1598 } | 1607 } |
| 1599 | 1608 |
| 1600 } } // namespace v8::internal | 1609 } } // namespace v8::internal |
| OLD | NEW |