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