| 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 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 PropertyDescriptor.prototype.getSet = function() { | 484 PropertyDescriptor.prototype.getSet = function() { |
| 485 return this.set_; | 485 return this.set_; |
| 486 } | 486 } |
| 487 | 487 |
| 488 | 488 |
| 489 PropertyDescriptor.prototype.hasSetter = function() { | 489 PropertyDescriptor.prototype.hasSetter = function() { |
| 490 return this.hasSetter_; | 490 return this.hasSetter_; |
| 491 } | 491 } |
| 492 | 492 |
| 493 | 493 |
| 494 // Converts an array returned from Runtime_GetOwnProperty to an actual |
| 495 // property descriptor. For a description of the array layout please |
| 496 // see the runtime.cc file. |
| 497 function ConvertDescriptorArrayToDescriptor(desc_array) { |
| 498 if (desc_array == false) { |
| 499 throw 'Internal error: invalid desc_array'; |
| 500 } |
| 494 | 501 |
| 495 // ES5 section 8.12.1. | 502 if (IS_UNDEFINED(desc_array)) { |
| 496 function GetOwnProperty(obj, p) { | 503 return void 0; |
| 504 } |
| 505 |
| 497 var desc = new PropertyDescriptor(); | 506 var desc = new PropertyDescriptor(); |
| 498 | 507 // This is an accessor. |
| 499 // GetOwnProperty returns an array indexed by the constants | 508 if (desc_array[IS_ACCESSOR_INDEX]) { |
| 500 // defined in macros.py. | 509 desc.setGet(desc_array[GETTER_INDEX]); |
| 501 // If p is not a property on obj undefined is returned. | 510 desc.setSet(desc_array[SETTER_INDEX]); |
| 502 var props = %GetOwnProperty(ToObject(obj), ToString(p)); | |
| 503 | |
| 504 if (IS_UNDEFINED(props)) return void 0; | |
| 505 | |
| 506 // This is an accessor | |
| 507 if (props[IS_ACCESSOR_INDEX]) { | |
| 508 desc.setGet(props[GETTER_INDEX]); | |
| 509 desc.setSet(props[SETTER_INDEX]); | |
| 510 } else { | 511 } else { |
| 511 desc.setValue(props[VALUE_INDEX]); | 512 desc.setValue(desc_array[VALUE_INDEX]); |
| 512 desc.setWritable(props[WRITABLE_INDEX]); | 513 desc.setWritable(desc_array[WRITABLE_INDEX]); |
| 513 } | 514 } |
| 514 desc.setEnumerable(props[ENUMERABLE_INDEX]); | 515 desc.setEnumerable(desc_array[ENUMERABLE_INDEX]); |
| 515 desc.setConfigurable(props[CONFIGURABLE_INDEX]); | 516 desc.setConfigurable(desc_array[CONFIGURABLE_INDEX]); |
| 516 | 517 |
| 517 return desc; | 518 return desc; |
| 518 } | 519 } |
| 519 | 520 |
| 520 | 521 |
| 521 // ES5 section 8.12.2. | 522 // ES5 section 8.12.2. |
| 522 function GetProperty(obj, p) { | 523 function GetProperty(obj, p) { |
| 523 var prop = GetOwnProperty(obj); | 524 var prop = GetOwnProperty(obj); |
| 524 if (!IS_UNDEFINED(prop)) return prop; | 525 if (!IS_UNDEFINED(prop)) return prop; |
| 525 var proto = obj.__proto__; | 526 var proto = obj.__proto__; |
| 526 if (IS_NULL(proto)) return void 0; | 527 if (IS_NULL(proto)) return void 0; |
| 527 return GetProperty(proto, p); | 528 return GetProperty(proto, p); |
| 528 } | 529 } |
| 529 | 530 |
| 530 | 531 |
| 531 // ES5 section 8.12.6 | 532 // ES5 section 8.12.6 |
| 532 function HasProperty(obj, p) { | 533 function HasProperty(obj, p) { |
| 533 var desc = GetProperty(obj, p); | 534 var desc = GetProperty(obj, p); |
| 534 return IS_UNDEFINED(desc) ? false : true; | 535 return IS_UNDEFINED(desc) ? false : true; |
| 535 } | 536 } |
| 536 | 537 |
| 537 | 538 |
| 539 // ES5 section 8.12.1. |
| 540 function GetOwnProperty(obj, p) { |
| 541 // GetOwnProperty returns an array indexed by the constants |
| 542 // defined in macros.py. |
| 543 // If p is not a property on obj undefined is returned. |
| 544 var props = %GetOwnProperty(ToObject(obj), ToString(p)); |
| 545 |
| 546 // A false value here means that access checks failed. |
| 547 if (props == false) return void 0; |
| 548 |
| 549 return ConvertDescriptorArrayToDescriptor(props); |
| 550 } |
| 551 |
| 552 |
| 538 // ES5 8.12.9. | 553 // ES5 8.12.9. |
| 539 function DefineOwnProperty(obj, p, desc, should_throw) { | 554 function DefineOwnProperty(obj, p, desc, should_throw) { |
| 540 var current = GetOwnProperty(obj, p); | 555 var current_or_access = %GetOwnProperty(ToObject(obj), ToString(p)); |
| 556 // A false value here means that access checks failed. |
| 557 if (current_or_access == false) return void 0; |
| 558 |
| 559 var current = ConvertDescriptorArrayToDescriptor(current_or_access); |
| 541 var extensible = %IsExtensible(ToObject(obj)); | 560 var extensible = %IsExtensible(ToObject(obj)); |
| 542 | 561 |
| 543 // Error handling according to spec. | 562 // Error handling according to spec. |
| 544 // Step 3 | 563 // Step 3 |
| 545 if (IS_UNDEFINED(current) && !extensible) | 564 if (IS_UNDEFINED(current) && !extensible) |
| 546 throw MakeTypeError("define_disallowed", ["defineProperty"]); | 565 throw MakeTypeError("define_disallowed", ["defineProperty"]); |
| 547 | 566 |
| 548 if (!IS_UNDEFINED(current) && !current.isConfigurable()) { | 567 if (!IS_UNDEFINED(current)) { |
| 549 // Step 5 and 6 | 568 // Step 5 and 6 |
| 550 if ((!desc.hasEnumerable() || | 569 if ((IsGenericDescriptor(desc) || |
| 551 SameValue(desc.isEnumerable() && current.isEnumerable())) && | 570 IsDataDescriptor(desc) == IsDataDescriptor(current)) && |
| 571 (!desc.hasEnumerable() || |
| 572 SameValue(desc.isEnumerable(), current.isEnumerable())) && |
| 552 (!desc.hasConfigurable() || | 573 (!desc.hasConfigurable() || |
| 553 SameValue(desc.isConfigurable(), current.isConfigurable())) && | 574 SameValue(desc.isConfigurable(), current.isConfigurable())) && |
| 554 (!desc.hasWritable() || | 575 (!desc.hasWritable() || |
| 555 SameValue(desc.isWritable(), current.isWritable())) && | 576 SameValue(desc.isWritable(), current.isWritable())) && |
| 556 (!desc.hasValue() || | 577 (!desc.hasValue() || |
| 557 SameValue(desc.getValue(), current.getValue())) && | 578 SameValue(desc.getValue(), current.getValue())) && |
| 558 (!desc.hasGetter() || | 579 (!desc.hasGetter() || |
| 559 SameValue(desc.getGet(), current.getGet())) && | 580 SameValue(desc.getGet(), current.getGet())) && |
| 560 (!desc.hasSetter() || | 581 (!desc.hasSetter() || |
| 561 SameValue(desc.getSet(), current.getSet()))) { | 582 SameValue(desc.getSet(), current.getSet()))) { |
| 562 return true; | 583 return true; |
| 563 } | 584 } |
| 564 | 585 if (!current.isConfigurable()) { |
| 565 // Step 7 | 586 // Step 7 |
| 566 if (desc.isConfigurable() || desc.isEnumerable() != current.isEnumerable()) | 587 if (desc.isConfigurable() || |
| 567 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); | 588 (desc.hasEnumerable() && |
| 568 // Step 9 | 589 desc.isEnumerable() != current.isEnumerable())) |
| 569 if (IsDataDescriptor(current) != IsDataDescriptor(desc)) | |
| 570 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); | |
| 571 // Step 10 | |
| 572 if (IsDataDescriptor(current) && IsDataDescriptor(desc)) { | |
| 573 if (!current.isWritable() && desc.isWritable()) | |
| 574 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); | 590 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 575 if (!current.isWritable() && desc.hasValue() && | 591 // Step 8 |
| 576 !SameValue(desc.getValue(), current.getValue())) { | 592 if (!IsGenericDescriptor(desc)) { |
| 577 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); | 593 // Step 9a |
| 594 if (IsDataDescriptor(current) != IsDataDescriptor(desc)) |
| 595 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 596 // Step 10a |
| 597 if (IsDataDescriptor(current) && IsDataDescriptor(desc)) { |
| 598 if (!current.isWritable() && desc.isWritable()) |
| 599 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 600 if (!current.isWritable() && desc.hasValue() && |
| 601 !SameValue(desc.getValue(), current.getValue())) { |
| 602 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 603 } |
| 604 } |
| 605 // Step 11 |
| 606 if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) { |
| 607 if (desc.hasSetter() && !SameValue(desc.getSet(), current.getSet())){ |
| 608 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 609 } |
| 610 if (desc.hasGetter() && !SameValue(desc.getGet(),current.getGet())) |
| 611 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 612 } |
| 578 } | 613 } |
| 579 } | 614 } |
| 580 // Step 11 | |
| 581 if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) { | |
| 582 if (desc.hasSetter() && !SameValue(desc.getSet(), current.getSet())){ | |
| 583 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); | |
| 584 } | |
| 585 if (desc.hasGetter() && !SameValue(desc.getGet(),current.getGet())) | |
| 586 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); | |
| 587 } | |
| 588 } | 615 } |
| 589 | 616 |
| 590 // Send flags - enumerable and configurable are common - writable is | 617 // Send flags - enumerable and configurable are common - writable is |
| 591 // only send to the data descriptor. | 618 // only send to the data descriptor. |
| 592 // Take special care if enumerable and configurable is not defined on | 619 // Take special care if enumerable and configurable is not defined on |
| 593 // desc (we need to preserve the existing values from current). | 620 // desc (we need to preserve the existing values from current). |
| 594 var flag = NONE; | 621 var flag = NONE; |
| 595 if (desc.hasEnumerable()) { | 622 if (desc.hasEnumerable()) { |
| 596 flag |= desc.isEnumerable() ? 0 : DONT_ENUM; | 623 flag |= desc.isEnumerable() ? 0 : DONT_ENUM; |
| 597 } else if (!IS_UNDEFINED(current)) { | 624 } else if (!IS_UNDEFINED(current)) { |
| 598 flag |= current.isEnumerable() ? 0 : DONT_ENUM; | 625 flag |= current.isEnumerable() ? 0 : DONT_ENUM; |
| 599 } else { | 626 } else { |
| 600 flag |= DONT_ENUM; | 627 flag |= DONT_ENUM; |
| 601 } | 628 } |
| 602 | 629 |
| 603 if (desc.hasConfigurable()) { | 630 if (desc.hasConfigurable()) { |
| 604 flag |= desc.isConfigurable() ? 0 : DONT_DELETE; | 631 flag |= desc.isConfigurable() ? 0 : DONT_DELETE; |
| 605 } else if (!IS_UNDEFINED(current)) { | 632 } else if (!IS_UNDEFINED(current)) { |
| 606 flag |= current.isConfigurable() ? 0 : DONT_DELETE; | 633 flag |= current.isConfigurable() ? 0 : DONT_DELETE; |
| 607 } else | 634 } else |
| 608 flag |= DONT_DELETE; | 635 flag |= DONT_DELETE; |
| 609 | 636 |
| 610 if (IsDataDescriptor(desc) || IsGenericDescriptor(desc)) { | 637 if (IsDataDescriptor(desc) || |
| 638 (IsGenericDescriptor(desc) && |
| 639 (IS_UNDEFINED(current) || IsDataDescriptor(current)))) { |
| 640 // There are 3 cases that lead here: |
| 641 // Step 4a - defining a new data property. |
| 642 // Steps 9b & 12 - replacing an existing accessor property with a data |
| 643 // property. |
| 644 // Step 12 - updating an existing data property with a data or generic |
| 645 // descriptor. |
| 646 |
| 611 if (desc.hasWritable()) { | 647 if (desc.hasWritable()) { |
| 612 flag |= desc.isWritable() ? 0 : READ_ONLY; | 648 flag |= desc.isWritable() ? 0 : READ_ONLY; |
| 613 } else if (!IS_UNDEFINED(current)) { | 649 } else if (!IS_UNDEFINED(current)) { |
| 614 flag |= current.isWritable() ? 0 : READ_ONLY; | 650 flag |= current.isWritable() ? 0 : READ_ONLY; |
| 615 } else { | 651 } else { |
| 616 flag |= READ_ONLY; | 652 flag |= READ_ONLY; |
| 617 } | 653 } |
| 654 |
| 618 var value = void 0; // Default value is undefined. | 655 var value = void 0; // Default value is undefined. |
| 619 if (desc.hasValue()) { | 656 if (desc.hasValue()) { |
| 620 value = desc.getValue(); | 657 value = desc.getValue(); |
| 621 } else if (!IS_UNDEFINED(current)) { | 658 } else if (!IS_UNDEFINED(current) && IsDataDescriptor(current)) { |
| 622 value = current.getValue(); | 659 value = current.getValue(); |
| 623 } | 660 } |
| 661 |
| 624 %DefineOrRedefineDataProperty(obj, p, value, flag); | 662 %DefineOrRedefineDataProperty(obj, p, value, flag); |
| 663 } else if (IsGenericDescriptor(desc)) { |
| 664 // Step 12 - updating an existing accessor property with generic |
| 665 // descriptor. Changing flags only. |
| 666 %DefineOrRedefineAccessorProperty(obj, p, GETTER, current.getGet(), flag); |
| 625 } else { | 667 } else { |
| 626 if (desc.hasGetter() && | 668 // There are 3 cases that lead here: |
| 627 (IS_FUNCTION(desc.getGet()) || IS_UNDEFINED(desc.getGet()))) { | 669 // Step 4b - defining a new accessor property. |
| 628 %DefineOrRedefineAccessorProperty(obj, p, GETTER, desc.getGet(), flag); | 670 // Steps 9c & 12 - replacing an existing data property with an accessor |
| 671 // property. |
| 672 // Step 12 - updating an existing accessor property with an accessor |
| 673 // descriptor. |
| 674 if (desc.hasGetter()) { |
| 675 %DefineOrRedefineAccessorProperty(obj, p, GETTER, desc.getGet(), flag); |
| 629 } | 676 } |
| 630 if (desc.hasSetter() && | 677 if (desc.hasSetter()) { |
| 631 (IS_FUNCTION(desc.getSet()) || IS_UNDEFINED(desc.getSet()))) { | |
| 632 %DefineOrRedefineAccessorProperty(obj, p, SETTER, desc.getSet(), flag); | 678 %DefineOrRedefineAccessorProperty(obj, p, SETTER, desc.getSet(), flag); |
| 633 } | 679 } |
| 634 } | 680 } |
| 635 return true; | 681 return true; |
| 636 } | 682 } |
| 637 | 683 |
| 638 | 684 |
| 639 // ES5 section 15.2.3.2. | 685 // ES5 section 15.2.3.2. |
| 640 function ObjectGetPrototypeOf(obj) { | 686 function ObjectGetPrototypeOf(obj) { |
| 641 if (!IS_SPEC_OBJECT(obj)) | 687 if (!IS_SPEC_OBJECT(obj)) |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1100 } | 1146 } |
| 1101 | 1147 |
| 1102 | 1148 |
| 1103 // ES5 15.3.4.5 | 1149 // ES5 15.3.4.5 |
| 1104 function FunctionBind(this_arg) { // Length is 1. | 1150 function FunctionBind(this_arg) { // Length is 1. |
| 1105 if (!IS_FUNCTION(this)) { | 1151 if (!IS_FUNCTION(this)) { |
| 1106 throw new $TypeError('Bind must be called on a function'); | 1152 throw new $TypeError('Bind must be called on a function'); |
| 1107 } | 1153 } |
| 1108 // this_arg is not an argument that should be bound. | 1154 // this_arg is not an argument that should be bound. |
| 1109 var argc_bound = (%_ArgumentsLength() || 1) - 1; | 1155 var argc_bound = (%_ArgumentsLength() || 1) - 1; |
| 1110 if (argc_bound > 0) { | 1156 var fn = this; |
| 1157 if (argc_bound == 0) { |
| 1158 var result = function() { |
| 1159 if (%_IsConstructCall()) { |
| 1160 // %NewObjectFromBound implicitly uses arguments passed to this |
| 1161 // function. We do not pass the arguments object explicitly to avoid |
| 1162 // materializing it and guarantee that this function will be optimized. |
| 1163 return %NewObjectFromBound(fn, null); |
| 1164 } |
| 1165 |
| 1166 return fn.apply(this_arg, arguments); |
| 1167 }; |
| 1168 } else { |
| 1111 var bound_args = new $Array(argc_bound); | 1169 var bound_args = new $Array(argc_bound); |
| 1112 for(var i = 0; i < argc_bound; i++) { | 1170 for(var i = 0; i < argc_bound; i++) { |
| 1113 bound_args[i] = %_Arguments(i+1); | 1171 bound_args[i] = %_Arguments(i+1); |
| 1114 } | 1172 } |
| 1173 |
| 1174 var result = function() { |
| 1175 // If this is a construct call we use a special runtime method |
| 1176 // to generate the actual object using the bound function. |
| 1177 if (%_IsConstructCall()) { |
| 1178 // %NewObjectFromBound implicitly uses arguments passed to this |
| 1179 // function. We do not pass the arguments object explicitly to avoid |
| 1180 // materializing it and guarantee that this function will be optimized. |
| 1181 return %NewObjectFromBound(fn, bound_args); |
| 1182 } |
| 1183 |
| 1184 // Combine the args we got from the bind call with the args |
| 1185 // given as argument to the invocation. |
| 1186 var argc = %_ArgumentsLength(); |
| 1187 var args = new $Array(argc + argc_bound); |
| 1188 // Add bound arguments. |
| 1189 for (var i = 0; i < argc_bound; i++) { |
| 1190 args[i] = bound_args[i]; |
| 1191 } |
| 1192 // Add arguments from call. |
| 1193 for (var i = 0; i < argc; i++) { |
| 1194 args[argc_bound + i] = %_Arguments(i); |
| 1195 } |
| 1196 return fn.apply(this_arg, args); |
| 1197 }; |
| 1115 } | 1198 } |
| 1116 var fn = this; | |
| 1117 var result = function() { | |
| 1118 // Combine the args we got from the bind call with the args | |
| 1119 // given as argument to the invocation. | |
| 1120 var argc = %_ArgumentsLength(); | |
| 1121 var args = new $Array(argc + argc_bound); | |
| 1122 // Add bound arguments. | |
| 1123 for (var i = 0; i < argc_bound; i++) { | |
| 1124 args[i] = bound_args[i]; | |
| 1125 } | |
| 1126 // Add arguments from call. | |
| 1127 for (var i = 0; i < argc; i++) { | |
| 1128 args[argc_bound + i] = %_Arguments(i); | |
| 1129 } | |
| 1130 // If this is a construct call we use a special runtime method | |
| 1131 // to generate the actual object using the bound function. | |
| 1132 if (%_IsConstructCall()) { | |
| 1133 return %NewObjectFromBound(fn, args); | |
| 1134 } | |
| 1135 return fn.apply(this_arg, args); | |
| 1136 }; | |
| 1137 | 1199 |
| 1138 // We already have caller and arguments properties on functions, | 1200 // We already have caller and arguments properties on functions, |
| 1139 // which are non-configurable. It therefore makes no sence to | 1201 // which are non-configurable. It therefore makes no sence to |
| 1140 // try to redefine these as defined by the spec. The spec says | 1202 // try to redefine these as defined by the spec. The spec says |
| 1141 // that bind should make these throw a TypeError if get or set | 1203 // that bind should make these throw a TypeError if get or set |
| 1142 // is called and make them non-enumerable and non-configurable. | 1204 // is called and make them non-enumerable and non-configurable. |
| 1143 // To be consistent with our normal functions we leave this as it is. | 1205 // To be consistent with our normal functions we leave this as it is. |
| 1144 | 1206 |
| 1145 // Set the correct length. | 1207 // Set the correct length. |
| 1146 var length = (this.length - argc_bound) > 0 ? this.length - argc_bound : 0; | 1208 var length = (this.length - argc_bound) > 0 ? this.length - argc_bound : 0; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1177 // ---------------------------------------------------------------------------- | 1239 // ---------------------------------------------------------------------------- |
| 1178 | 1240 |
| 1179 function SetupFunction() { | 1241 function SetupFunction() { |
| 1180 InstallFunctions($Function.prototype, DONT_ENUM, $Array( | 1242 InstallFunctions($Function.prototype, DONT_ENUM, $Array( |
| 1181 "bind", FunctionBind, | 1243 "bind", FunctionBind, |
| 1182 "toString", FunctionToString | 1244 "toString", FunctionToString |
| 1183 )); | 1245 )); |
| 1184 } | 1246 } |
| 1185 | 1247 |
| 1186 SetupFunction(); | 1248 SetupFunction(); |
| OLD | NEW |