OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 1738 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1749 // which are non-configurable. It therefore makes no sence to | 1749 // which are non-configurable. It therefore makes no sence to |
1750 // try to redefine these as defined by the spec. The spec says | 1750 // try to redefine these as defined by the spec. The spec says |
1751 // that bind should make these throw a TypeError if get or set | 1751 // that bind should make these throw a TypeError if get or set |
1752 // is called and make them non-enumerable and non-configurable. | 1752 // is called and make them non-enumerable and non-configurable. |
1753 // To be consistent with our normal functions we leave this as it is. | 1753 // To be consistent with our normal functions we leave this as it is. |
1754 // TODO(lrn): Do set these to be thrower. | 1754 // TODO(lrn): Do set these to be thrower. |
1755 return result; | 1755 return result; |
1756 } | 1756 } |
1757 | 1757 |
1758 | 1758 |
1759 function NewFunction(arg1) { // length == 1 | 1759 // InternalArray, string, boolean -> Function |
1760 var n = %_ArgumentsLength(); | 1760 function NewFunction(formals, body, is_generator) { |
1761 var p = ''; | 1761 var formals_string = ''; |
1762 if (n > 1) { | 1762 if (formals.length > 0) { |
1763 p = new InternalArray(n - 1); | 1763 formals_string = Join(formals, formals.length, ',', NonStringToString); |
1764 for (var i = 0; i < n - 1; i++) p[i] = %_Arguments(i); | |
1765 p = Join(p, n - 1, ',', NonStringToString); | |
1766 // If the formal parameters string include ) - an illegal | 1764 // If the formal parameters string include ) - an illegal |
1767 // character - it may make the combined function expression | 1765 // character - it may make the combined function expression |
1768 // compile. We avoid this problem by checking for this early on. | 1766 // compile. We avoid this problem by checking for this early on. |
1769 if (p.indexOf(')') != -1) throw MakeSyntaxError('paren_in_arg_string',[]); | 1767 if (formals_string.indexOf(')') != -1) |
1768 throw MakeSyntaxError('paren_in_arg_string', []); | |
1770 // If the formal parameters include an unbalanced block comment, the | 1769 // If the formal parameters include an unbalanced block comment, the |
1771 // function must be rejected. Since JavaScript does not allow nested | 1770 // function must be rejected. Since JavaScript does not allow nested |
1772 // comments we can include a trailing block comment to catch this. | 1771 // comments we can include a trailing block comment to catch this. |
1773 p += '\n/' + '**/'; | 1772 formals_string += '\n/' + '**/'; |
1774 } | 1773 } |
1775 var body = (n > 0) ? ToString(%_Arguments(n - 1)) : ''; | 1774 var head = is_generator ? '(function*(' : '(function('; |
1776 var source = '(function(' + p + ') {\n' + body + '\n})'; | 1775 var source = head + formals_string + ') {\n' + body + '\n})'; |
1777 | 1776 |
1778 var global_receiver = %GlobalReceiver(global); | 1777 var global_receiver = %GlobalReceiver(global); |
1779 var f = %_CallFunction(global_receiver, %CompileString(source, true)); | 1778 var f = %_CallFunction(global_receiver, %CompileString(source, true)); |
1780 | 1779 |
1781 %FunctionMarkNameShouldPrintAsAnonymous(f); | 1780 %FunctionMarkNameShouldPrintAsAnonymous(f); |
1782 return f; | 1781 return f; |
1783 } | 1782 } |
1784 | 1783 |
1785 | 1784 |
1785 function FunctionConstructor(arg1) { // length == 1 | |
1786 var n = %_ArgumentsLength(); | |
1787 if (n > 0) { | |
Michael Starzinger
2013/05/14 13:51:31
We should try to have a fast-path for the case whe
| |
1788 var formals = new InternalArray(n - 1); | |
1789 for (var i = 0; i < n - 1; i++) formals[i] = %_Arguments(i); | |
1790 return NewFunction(formals, ToString(%_Arguments(n - 1)), false); | |
1791 } else { | |
1792 return NewFunction(new InternalArray(0), '', false); | |
1793 } | |
1794 } | |
1795 | |
1796 | |
1786 // ---------------------------------------------------------------------------- | 1797 // ---------------------------------------------------------------------------- |
1787 | 1798 |
1788 function SetUpFunction() { | 1799 function SetUpFunction() { |
1789 %CheckIsBootstrapping(); | 1800 %CheckIsBootstrapping(); |
1790 | 1801 |
1791 %SetCode($Function, NewFunction); | 1802 %SetCode($Function, FunctionConstructor); |
1792 %SetProperty($Function.prototype, "constructor", $Function, DONT_ENUM); | 1803 %SetProperty($Function.prototype, "constructor", $Function, DONT_ENUM); |
1793 | 1804 |
1794 InstallFunctions($Function.prototype, DONT_ENUM, $Array( | 1805 InstallFunctions($Function.prototype, DONT_ENUM, $Array( |
1795 "bind", FunctionBind, | 1806 "bind", FunctionBind, |
1796 "toString", FunctionToString | 1807 "toString", FunctionToString |
1797 )); | 1808 )); |
1798 } | 1809 } |
1799 | 1810 |
1800 SetUpFunction(); | 1811 SetUpFunction(); |
OLD | NEW |