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 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This files contains runtime support implemented in JavaScript. | 5 // This files contains runtime support implemented in JavaScript. |
6 | 6 |
7 // CAUTION: Some of the functions specified in this file are called | 7 // CAUTION: Some of the functions specified in this file are called |
8 // directly from compiled code. These are the functions with names in | 8 // directly from compiled code. These are the functions with names in |
9 // ALL CAPS. The compiled code passes the first argument in 'this'. | 9 // ALL CAPS. The compiled code passes the first argument in 'this'. |
10 | 10 |
11 | 11 |
12 // The following declarations are shared with other native JS files. | 12 // The following declarations are shared with other native JS files. |
13 // They are all declared at this one spot to avoid redeclaration errors. | 13 // They are all declared at this one spot to avoid redeclaration errors. |
14 var $defaultString; | 14 var $defaultString; |
15 var $NaN; | 15 var $NaN; |
16 var $nonNumberToNumber; | 16 var $nonNumberToNumber; |
17 var $nonStringToString; | 17 var $nonStringToString; |
18 var $sameValue; | 18 var $sameValue; |
19 var $sameValueZero; | 19 var $sameValueZero; |
20 var $toInteger; | 20 var $toInteger; |
21 var $toLength; | 21 var $toLength; |
22 var $toName; | |
23 var $toNumber; | 22 var $toNumber; |
24 var $toPositiveInteger; | 23 var $toPositiveInteger; |
25 var $toPrimitive; | 24 var $toPrimitive; |
26 var $toString; | 25 var $toString; |
27 | 26 |
28 (function(global, utils) { | 27 (function(global, utils) { |
29 | 28 |
30 %CheckIsBootstrapping(); | 29 %CheckIsBootstrapping(); |
31 | 30 |
32 var GlobalArray = global.Array; | 31 var GlobalArray = global.Array; |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 function IN(x) { | 426 function IN(x) { |
428 if (!IS_SPEC_OBJECT(x)) { | 427 if (!IS_SPEC_OBJECT(x)) { |
429 throw %make_type_error(kInvalidInOperatorUse, this, x); | 428 throw %make_type_error(kInvalidInOperatorUse, this, x); |
430 } | 429 } |
431 if (%_IsNonNegativeSmi(this)) { | 430 if (%_IsNonNegativeSmi(this)) { |
432 if (IS_ARRAY(x) && %_HasFastPackedElements(x)) { | 431 if (IS_ARRAY(x) && %_HasFastPackedElements(x)) { |
433 return this < x.length; | 432 return this < x.length; |
434 } | 433 } |
435 return %HasElement(x, this); | 434 return %HasElement(x, this); |
436 } | 435 } |
437 return %HasProperty(x, %to_name(this)); | 436 return %HasProperty(x, this); |
438 } | 437 } |
439 | 438 |
440 | 439 |
441 function CALL_NON_FUNCTION() { | 440 function CALL_NON_FUNCTION() { |
442 var delegate = %GetFunctionDelegate(this); | 441 var delegate = %GetFunctionDelegate(this); |
443 if (!IS_FUNCTION(delegate)) { | 442 if (!IS_FUNCTION(delegate)) { |
444 var callsite = %RenderCallSite(); | 443 var callsite = %RenderCallSite(); |
445 if (callsite == "") callsite = typeof this; | 444 if (callsite == "") callsite = typeof this; |
446 throw %make_type_error(kCalledNonCallable, callsite); | 445 throw %make_type_error(kCalledNonCallable, callsite); |
447 } | 446 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 return %to_number_fun(this); | 608 return %to_number_fun(this); |
610 } | 609 } |
611 | 610 |
612 | 611 |
613 // Convert the receiver to a string - forward to ToString. | 612 // Convert the receiver to a string - forward to ToString. |
614 function TO_STRING() { | 613 function TO_STRING() { |
615 return %to_string_fun(this); | 614 return %to_string_fun(this); |
616 } | 615 } |
617 | 616 |
618 | 617 |
619 // Convert the receiver to a string or symbol - forward to ToName. | |
620 function TO_NAME() { | |
621 return %to_name(this); | |
622 } | |
623 | |
624 | |
625 /* ------------------------------------- | 618 /* ------------------------------------- |
626 - - - C o n v e r s i o n s - - - | 619 - - - C o n v e r s i o n s - - - |
627 ------------------------------------- | 620 ------------------------------------- |
628 */ | 621 */ |
629 | 622 |
630 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, | 623 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, |
631 // (1) for number hint, and (2) for string hint. | 624 // (1) for number hint, and (2) for string hint. |
632 function ToPrimitive(x, hint) { | 625 function ToPrimitive(x, hint) { |
633 if (!IS_SPEC_OBJECT(x)) return x; | 626 if (!IS_SPEC_OBJECT(x)) return x; |
634 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; | 627 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 | 676 |
684 function NonStringToString(x) { | 677 function NonStringToString(x) { |
685 if (IS_NUMBER(x)) return %_NumberToString(x); | 678 if (IS_NUMBER(x)) return %_NumberToString(x); |
686 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 679 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
687 if (IS_UNDEFINED(x)) return 'undefined'; | 680 if (IS_UNDEFINED(x)) return 'undefined'; |
688 // Types that can't be converted to string are caught in DefaultString. | 681 // Types that can't be converted to string are caught in DefaultString. |
689 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x)); | 682 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x)); |
690 } | 683 } |
691 | 684 |
692 | 685 |
693 // ES6 symbols | |
694 function ToName(x) { | |
695 return IS_SYMBOL(x) ? x : ToString(x); | |
696 } | |
697 | |
698 | |
699 // ECMA-262, section 9.4, page 34. | 686 // ECMA-262, section 9.4, page 34. |
700 function ToInteger(x) { | 687 function ToInteger(x) { |
701 if (%_IsSmi(x)) return x; | 688 if (%_IsSmi(x)) return x; |
702 return %NumberToInteger(ToNumber(x)); | 689 return %NumberToInteger(ToNumber(x)); |
703 } | 690 } |
704 | 691 |
705 | 692 |
706 // ES6, draft 08-24-14, section 7.1.15 | 693 // ES6, draft 08-24-14, section 7.1.15 |
707 function ToLength(arg) { | 694 function ToLength(arg) { |
708 arg = ToInteger(arg); | 695 arg = ToInteger(arg); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 // Exports | 813 // Exports |
827 | 814 |
828 $defaultString = DefaultString; | 815 $defaultString = DefaultString; |
829 $NaN = %GetRootNaN(); | 816 $NaN = %GetRootNaN(); |
830 $nonNumberToNumber = NonNumberToNumber; | 817 $nonNumberToNumber = NonNumberToNumber; |
831 $nonStringToString = NonStringToString; | 818 $nonStringToString = NonStringToString; |
832 $sameValue = SameValue; | 819 $sameValue = SameValue; |
833 $sameValueZero = SameValueZero; | 820 $sameValueZero = SameValueZero; |
834 $toInteger = ToInteger; | 821 $toInteger = ToInteger; |
835 $toLength = ToLength; | 822 $toLength = ToLength; |
836 $toName = ToName; | |
837 $toNumber = ToNumber; | 823 $toNumber = ToNumber; |
838 $toPositiveInteger = ToPositiveInteger; | 824 $toPositiveInteger = ToPositiveInteger; |
839 $toPrimitive = ToPrimitive; | 825 $toPrimitive = ToPrimitive; |
840 $toString = ToString; | 826 $toString = ToString; |
841 | 827 |
842 %InstallToContext([ | 828 %InstallToContext([ |
843 "add_builtin", ADD, | 829 "add_builtin", ADD, |
844 "add_strong_builtin", ADD_STRONG, | 830 "add_strong_builtin", ADD_STRONG, |
845 "apply_prepare_builtin", APPLY_PREPARE, | 831 "apply_prepare_builtin", APPLY_PREPARE, |
846 "bit_and_builtin", BIT_AND, | 832 "bit_and_builtin", BIT_AND, |
(...skipping 23 matching lines...) Expand all Loading... |
870 "sar_strong_builtin", SAR_STRONG, | 856 "sar_strong_builtin", SAR_STRONG, |
871 "shl_builtin", SHL, | 857 "shl_builtin", SHL, |
872 "shl_strong_builtin", SHL_STRONG, | 858 "shl_strong_builtin", SHL_STRONG, |
873 "shr_builtin", SHR, | 859 "shr_builtin", SHR, |
874 "shr_strong_builtin", SHR_STRONG, | 860 "shr_strong_builtin", SHR_STRONG, |
875 "stack_overflow_builtin", STACK_OVERFLOW, | 861 "stack_overflow_builtin", STACK_OVERFLOW, |
876 "string_add_left_builtin", STRING_ADD_LEFT, | 862 "string_add_left_builtin", STRING_ADD_LEFT, |
877 "string_add_right_builtin", STRING_ADD_RIGHT, | 863 "string_add_right_builtin", STRING_ADD_RIGHT, |
878 "sub_builtin", SUB, | 864 "sub_builtin", SUB, |
879 "sub_strong_builtin", SUB_STRONG, | 865 "sub_strong_builtin", SUB_STRONG, |
880 "to_name_builtin", TO_NAME, | |
881 "to_string_builtin", TO_STRING, | 866 "to_string_builtin", TO_STRING, |
882 ]); | 867 ]); |
883 | 868 |
884 %InstallToContext([ | 869 %InstallToContext([ |
885 "concat_iterable_to_array", ConcatIterableToArray, | 870 "concat_iterable_to_array", ConcatIterableToArray, |
886 "non_number_to_number", NonNumberToNumber, | 871 "non_number_to_number", NonNumberToNumber, |
887 "non_string_to_string", NonStringToString, | 872 "non_string_to_string", NonStringToString, |
888 "to_integer_fun", ToInteger, | 873 "to_integer_fun", ToInteger, |
889 "to_length_fun", ToLength, | 874 "to_length_fun", ToLength, |
890 "to_name", ToName, | |
891 "to_number_fun", ToNumber, | 875 "to_number_fun", ToNumber, |
892 "to_primitive", ToPrimitive, | 876 "to_primitive", ToPrimitive, |
893 "to_string_fun", ToString, | 877 "to_string_fun", ToString, |
894 ]); | 878 ]); |
895 | 879 |
896 utils.Export(function(to) { | 880 utils.Export(function(to) { |
897 to.ToBoolean = ToBoolean; | 881 to.ToBoolean = ToBoolean; |
898 to.ToLength = ToLength; | 882 to.ToLength = ToLength; |
899 to.ToName = ToName; | |
900 to.ToNumber = ToNumber; | 883 to.ToNumber = ToNumber; |
901 to.ToPrimitive = ToPrimitive; | 884 to.ToPrimitive = ToPrimitive; |
902 to.ToString = ToString; | 885 to.ToString = ToString; |
903 }); | 886 }); |
904 | 887 |
905 }) | 888 }) |
OLD | NEW |