OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 1447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1458 | 1458 |
1459 | 1459 |
1460 function FunctionToString() { | 1460 function FunctionToString() { |
1461 return FunctionSourceString(this); | 1461 return FunctionSourceString(this); |
1462 } | 1462 } |
1463 | 1463 |
1464 | 1464 |
1465 // ES5 15.3.4.5 | 1465 // ES5 15.3.4.5 |
1466 function FunctionBind(this_arg) { // Length is 1. | 1466 function FunctionBind(this_arg) { // Length is 1. |
1467 if (!IS_SPEC_FUNCTION(this)) { | 1467 if (!IS_SPEC_FUNCTION(this)) { |
1468 throw new $TypeError('Bind must be called on a function'); | 1468 throw new $TypeError('Bind must be called on a function'); |
1469 } | 1469 } |
1470 // this_arg is not an argument that should be bound. | 1470 var boundFunction = function () { |
1471 var argc_bound = (%_ArgumentsLength() || 1) - 1; | 1471 // This function must not use any object literals (Object, Array, RegExp), |
1472 var fn = this; | 1472 // since the literals-array is being used to store the bound data. |
| 1473 if (%_IsConstructCall()) { |
| 1474 return %NewObjectFromBound(boundFunction); |
| 1475 } |
| 1476 var bindings = %BoundFunctionGetBindings(boundFunction); |
| 1477 var argc = %_ArgumentsLength(); |
| 1478 if (argc == 0) { |
| 1479 return %Apply(bindings[0], bindings[1], bindings, 2, bindings.length - 2); |
| 1480 } |
| 1481 if (bindings.length === 2) { |
| 1482 return %Apply(bindings[0], bindings[1], arguments, 0, argc); |
| 1483 } |
| 1484 var bound_argc = bindings.length - 2; |
| 1485 var argv = new InternalArray(bound_argc + argc); |
| 1486 for (var i = 0; i < bound_argc; i++) { |
| 1487 argv[i] = bindings[i + 2]; |
| 1488 } |
| 1489 for (var j = 0; j < argc; j++) { |
| 1490 argv[i++] = %_Arguments(j); |
| 1491 } |
| 1492 return %Apply(bindings[0], bindings[1], argv, 0, bound_argc + argc); |
| 1493 }; |
1473 | 1494 |
1474 if (argc_bound == 0) { | 1495 %FunctionRemovePrototype(boundFunction); |
1475 var result = function() { | 1496 // This runtime function finds any remaining arguments on the stack, |
1476 if (%_IsConstructCall()) { | 1497 // so we don't pass the arguments object. |
1477 // %NewObjectFromBound implicitly uses arguments passed to this | 1498 var result = %FunctionBindArguments(boundFunction, this, this_arg); |
1478 // function. We do not pass the arguments object explicitly to avoid | |
1479 // materializing it and guarantee that this function will be optimized. | |
1480 return %NewObjectFromBound(fn, null); | |
1481 } | |
1482 return %Apply(fn, this_arg, arguments, 0, %_ArgumentsLength()); | |
1483 }; | |
1484 } else { | |
1485 var bound_args = new InternalArray(argc_bound); | |
1486 for(var i = 0; i < argc_bound; i++) { | |
1487 bound_args[i] = %_Arguments(i+1); | |
1488 } | |
1489 | |
1490 var result = function() { | |
1491 // If this is a construct call we use a special runtime method | |
1492 // to generate the actual object using the bound function. | |
1493 if (%_IsConstructCall()) { | |
1494 // %NewObjectFromBound implicitly uses arguments passed to this | |
1495 // function. We do not pass the arguments object explicitly to avoid | |
1496 // materializing it and guarantee that this function will be optimized. | |
1497 return %NewObjectFromBound(fn, bound_args); | |
1498 } | |
1499 | |
1500 // Combine the args we got from the bind call with the args | |
1501 // given as argument to the invocation. | |
1502 var argc = %_ArgumentsLength(); | |
1503 var args = new InternalArray(argc + argc_bound); | |
1504 // Add bound arguments. | |
1505 for (var i = 0; i < argc_bound; i++) { | |
1506 args[i] = bound_args[i]; | |
1507 } | |
1508 // Add arguments from call. | |
1509 for (var i = 0; i < argc; i++) { | |
1510 args[argc_bound + i] = %_Arguments(i); | |
1511 } | |
1512 return %Apply(fn, this_arg, args, 0, argc + argc_bound); | |
1513 }; | |
1514 } | |
1515 | 1499 |
1516 // We already have caller and arguments properties on functions, | 1500 // We already have caller and arguments properties on functions, |
1517 // which are non-configurable. It therefore makes no sence to | 1501 // which are non-configurable. It therefore makes no sence to |
1518 // try to redefine these as defined by the spec. The spec says | 1502 // try to redefine these as defined by the spec. The spec says |
1519 // that bind should make these throw a TypeError if get or set | 1503 // that bind should make these throw a TypeError if get or set |
1520 // is called and make them non-enumerable and non-configurable. | 1504 // is called and make them non-enumerable and non-configurable. |
1521 // To be consistent with our normal functions we leave this as it is. | 1505 // To be consistent with our normal functions we leave this as it is. |
1522 | 1506 // TODO(lrn): Do set these to be thrower. |
1523 %FunctionRemovePrototype(result); | |
1524 %FunctionSetBound(result); | |
1525 // Set the correct length. If this is a function proxy, this.length might | |
1526 // throw, or return a bogus result. Leave length alone in that case. | |
1527 // TODO(rossberg): This is underspecified in the current proxy proposal. | |
1528 try { | |
1529 var old_length = ToInteger(this.length); | |
1530 var length = (old_length - argc_bound) > 0 ? old_length - argc_bound : 0; | |
1531 %BoundFunctionSetLength(result, length); | |
1532 } catch(x) {} | |
1533 return result; | 1507 return result; |
1534 } | 1508 } |
1535 | 1509 |
1536 | 1510 |
1537 function NewFunction(arg1) { // length == 1 | 1511 function NewFunction(arg1) { // length == 1 |
1538 var n = %_ArgumentsLength(); | 1512 var n = %_ArgumentsLength(); |
1539 var p = ''; | 1513 var p = ''; |
1540 if (n > 1) { | 1514 if (n > 1) { |
1541 p = new InternalArray(n - 1); | 1515 p = new InternalArray(n - 1); |
1542 for (var i = 0; i < n - 1; i++) p[i] = %_Arguments(i); | 1516 for (var i = 0; i < n - 1; i++) p[i] = %_Arguments(i); |
(...skipping 19 matching lines...) Expand all Loading... |
1562 | 1536 |
1563 function SetUpFunction() { | 1537 function SetUpFunction() { |
1564 %CheckIsBootstrapping(); | 1538 %CheckIsBootstrapping(); |
1565 InstallFunctions($Function.prototype, DONT_ENUM, $Array( | 1539 InstallFunctions($Function.prototype, DONT_ENUM, $Array( |
1566 "bind", FunctionBind, | 1540 "bind", FunctionBind, |
1567 "toString", FunctionToString | 1541 "toString", FunctionToString |
1568 )); | 1542 )); |
1569 } | 1543 } |
1570 | 1544 |
1571 SetUpFunction(); | 1545 SetUpFunction(); |
OLD | NEW |