Chromium Code Reviews| 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 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 // ECMA-262 Section 11.9.3. | 100 // ECMA-262 Section 11.9.3. |
| 101 EQUALS = function EQUALS(y) { | 101 EQUALS = function EQUALS(y) { |
| 102 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); | 102 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); |
| 103 var x = this; | 103 var x = this; |
| 104 | 104 |
| 105 while (true) { | 105 while (true) { |
| 106 if (IS_NUMBER(x)) { | 106 if (IS_NUMBER(x)) { |
| 107 while (true) { | 107 while (true) { |
| 108 if (IS_NUMBER(y)) return %NumberEquals(x, y); | 108 if (IS_NUMBER(y)) return %NumberEquals(x, y); |
| 109 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 109 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
| 110 if (IS_SYMBOL(y)) return 1; // not equal | 110 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
| 111 if (!IS_SPEC_OBJECT(y)) { | 111 if (!IS_SPEC_OBJECT(y)) { |
| 112 // String or boolean. | 112 // String or boolean. |
| 113 return %NumberEquals(x, %$toNumber(y)); | 113 return %NumberEquals(x, %$toNumber(y)); |
| 114 } | 114 } |
| 115 y = %$toPrimitive(y, NO_HINT); | 115 y = %$toPrimitive(y, NO_HINT); |
| 116 } | 116 } |
| 117 } else if (IS_STRING(x)) { | 117 } else if (IS_STRING(x)) { |
| 118 while (true) { | 118 while (true) { |
| 119 if (IS_STRING(y)) return %StringEquals(x, y); | 119 if (IS_STRING(y)) return %StringEquals(x, y); |
| 120 if (IS_SYMBOL(y)) return 1; // not equal | 120 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
| 121 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); | 121 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); |
| 122 if (IS_BOOLEAN(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); | 122 if (IS_BOOLEAN(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); |
| 123 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 123 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
| 124 y = %$toPrimitive(y, NO_HINT); | 124 y = %$toPrimitive(y, NO_HINT); |
| 125 } | 125 } |
| 126 } else if (IS_SYMBOL(x)) { | 126 } else if (IS_SYMBOL(x) || IS_FLOAT32X4(x)) { |
|
rossberg
2015/07/02 13:35:43
This seems wrong. There needs to be a separate cas
bbudge
2015/07/06 23:59:05
I misunderstood the spec to state that comparisons
| |
| 127 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1; | 127 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
| 128 return 1; // not equal | 128 return 1; // not equal |
| 129 } else if (IS_BOOLEAN(x)) { | 129 } else if (IS_BOOLEAN(x)) { |
| 130 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; | 130 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
| 131 if (IS_NULL_OR_UNDEFINED(y)) return 1; | 131 if (IS_NULL_OR_UNDEFINED(y)) return 1; |
| 132 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); | 132 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); |
| 133 if (IS_STRING(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); | 133 if (IS_STRING(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); |
| 134 if (IS_SYMBOL(y)) return 1; // not equal | 134 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
| 135 // y is object. | 135 // y is object. |
| 136 x = %$toNumber(x); | 136 x = %$toNumber(x); |
| 137 y = %$toPrimitive(y, NO_HINT); | 137 y = %$toPrimitive(y, NO_HINT); |
| 138 } else if (IS_NULL_OR_UNDEFINED(x)) { | 138 } else if (IS_NULL_OR_UNDEFINED(x)) { |
| 139 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; | 139 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; |
| 140 } else { | 140 } else { |
| 141 // x is an object. | 141 // x is an object. |
| 142 if (IS_SPEC_OBJECT(y)) { | 142 if (IS_SPEC_OBJECT(y)) { |
| 143 return %_ObjectEquals(x, y) ? 0 : 1; | 143 return %_ObjectEquals(x, y) ? 0 : 1; |
| 144 } | 144 } |
| 145 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 145 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
| 146 if (IS_SYMBOL(y)) return 1; // not equal | 146 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
| 147 if (IS_BOOLEAN(y)) y = %$toNumber(y); | 147 if (IS_BOOLEAN(y)) y = %$toNumber(y); |
| 148 x = %$toPrimitive(x, NO_HINT); | 148 x = %$toPrimitive(x, NO_HINT); |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 | 152 |
| 153 // ECMA-262, section 11.9.4, page 56. | 153 // ECMA-262, section 11.9.4, page 56. |
| 154 STRICT_EQUALS = function STRICT_EQUALS(x) { | 154 STRICT_EQUALS = function STRICT_EQUALS(x) { |
|
rossberg
2015/07/02 13:35:43
This needs a new case for simds as well.
bbudge
2015/07/06 23:59:04
Done.
| |
| 155 if (IS_STRING(this)) { | 155 if (IS_STRING(this)) { |
| 156 if (!IS_STRING(x)) return 1; // not equal | 156 if (!IS_STRING(x)) return 1; // not equal |
| 157 return %StringEquals(this, x); | 157 return %StringEquals(this, x); |
| 158 } | 158 } |
| 159 | 159 |
| 160 if (IS_NUMBER(this)) { | 160 if (IS_NUMBER(this)) { |
| 161 if (!IS_NUMBER(x)) return 1; // not equal | 161 if (!IS_NUMBER(x)) return 1; // not equal |
| 162 return %NumberEquals(this, x); | 162 return %NumberEquals(this, x); |
| 163 } | 163 } |
| 164 | 164 |
| (...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 803 */ | 803 */ |
| 804 | 804 |
| 805 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, | 805 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, |
| 806 // (1) for number hint, and (2) for string hint. | 806 // (1) for number hint, and (2) for string hint. |
| 807 function ToPrimitive(x, hint) { | 807 function ToPrimitive(x, hint) { |
| 808 // Fast case check. | 808 // Fast case check. |
| 809 if (IS_STRING(x)) return x; | 809 if (IS_STRING(x)) return x; |
| 810 // Normal behavior. | 810 // Normal behavior. |
| 811 if (!IS_SPEC_OBJECT(x)) return x; | 811 if (!IS_SPEC_OBJECT(x)) return x; |
| 812 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError(kSymbolToPrimitive); | 812 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError(kSymbolToPrimitive); |
| 813 if (IS_FLOAT32X4(x)) return x; | |
| 813 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; | 814 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; |
| 814 return (hint == NUMBER_HINT) ? DefaultNumber(x) : DefaultString(x); | 815 return (hint == NUMBER_HINT) ? DefaultNumber(x) : DefaultString(x); |
| 815 } | 816 } |
| 816 | 817 |
| 817 | 818 |
| 818 // ECMA-262, section 9.2, page 30 | 819 // ECMA-262, section 9.2, page 30 |
| 819 function ToBoolean(x) { | 820 function ToBoolean(x) { |
| 820 if (IS_BOOLEAN(x)) return x; | 821 if (IS_BOOLEAN(x)) return x; |
| 821 if (IS_STRING(x)) return x.length != 0; | 822 if (IS_STRING(x)) return x.length != 0; |
| 822 if (x == null) return false; | 823 if (x == null) return false; |
| 823 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); | 824 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); |
| 824 return true; | 825 return true; |
| 825 } | 826 } |
| 826 | 827 |
| 827 | 828 |
| 828 // ECMA-262, section 9.3, page 31. | 829 // ECMA-262, section 9.3, page 31. |
| 829 function ToNumber(x) { | 830 function ToNumber(x) { |
| 830 if (IS_NUMBER(x)) return x; | 831 if (IS_NUMBER(x)) return x; |
| 831 if (IS_STRING(x)) { | 832 if (IS_STRING(x)) { |
| 832 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | 833 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
| 833 : %StringToNumber(x); | 834 : %StringToNumber(x); |
| 834 } | 835 } |
| 835 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 836 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
| 836 if (IS_UNDEFINED(x)) return NAN; | 837 if (IS_UNDEFINED(x)) return NAN; |
| 837 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); | 838 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); |
| 839 if (IS_FLOAT32X4(x)) throw MakeTypeError(kSimdToNumber); | |
| 838 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); | 840 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); |
| 839 } | 841 } |
| 840 | 842 |
| 841 function NonNumberToNumber(x) { | 843 function NonNumberToNumber(x) { |
| 842 if (IS_STRING(x)) { | 844 if (IS_STRING(x)) { |
| 843 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | 845 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
| 844 : %StringToNumber(x); | 846 : %StringToNumber(x); |
| 845 } | 847 } |
| 846 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 848 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
| 847 if (IS_UNDEFINED(x)) return NAN; | 849 if (IS_UNDEFINED(x)) return NAN; |
| 848 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); | 850 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); |
| 851 if (IS_FLOAT32X4(x)) throw MakeTypeError(kSimdToNumber); | |
| 849 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); | 852 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); |
| 850 } | 853 } |
| 851 | 854 |
| 852 | 855 |
| 853 // ECMA-262, section 9.8, page 35. | 856 // ECMA-262, section 9.8, page 35. |
| 854 function ToString(x) { | 857 function ToString(x) { |
| 855 if (IS_STRING(x)) return x; | 858 if (IS_STRING(x)) return x; |
| 856 if (IS_NUMBER(x)) return %_NumberToString(x); | 859 if (IS_NUMBER(x)) return %_NumberToString(x); |
| 857 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 860 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
| 858 if (IS_UNDEFINED(x)) return 'undefined'; | 861 if (IS_UNDEFINED(x)) return 'undefined'; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 874 return IS_SYMBOL(x) ? x : ToString(x); | 877 return IS_SYMBOL(x) ? x : ToString(x); |
| 875 } | 878 } |
| 876 | 879 |
| 877 | 880 |
| 878 // ECMA-262, section 9.9, page 36. | 881 // ECMA-262, section 9.9, page 36. |
| 879 function ToObject(x) { | 882 function ToObject(x) { |
| 880 if (IS_STRING(x)) return new GlobalString(x); | 883 if (IS_STRING(x)) return new GlobalString(x); |
| 881 if (IS_NUMBER(x)) return new GlobalNumber(x); | 884 if (IS_NUMBER(x)) return new GlobalNumber(x); |
| 882 if (IS_BOOLEAN(x)) return new GlobalBoolean(x); | 885 if (IS_BOOLEAN(x)) return new GlobalBoolean(x); |
| 883 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); | 886 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); |
| 887 if (IS_FLOAT32X4(x)) return %NewFloat32x4Wrapper(x); | |
| 884 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { | 888 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { |
| 885 throw MakeTypeError(kUndefinedOrNullToObject); | 889 throw MakeTypeError(kUndefinedOrNullToObject); |
| 886 } | 890 } |
| 887 return x; | 891 return x; |
| 888 } | 892 } |
| 889 | 893 |
| 890 | 894 |
| 891 // ECMA-262, section 9.4, page 34. | 895 // ECMA-262, section 9.4, page 34. |
| 892 function ToInteger(x) { | 896 function ToInteger(x) { |
| 893 if (%_IsSmi(x)) return x; | 897 if (%_IsSmi(x)) return x; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 969 function IsConcatSpreadable(O) { | 973 function IsConcatSpreadable(O) { |
| 970 if (!IS_SPEC_OBJECT(O)) return false; | 974 if (!IS_SPEC_OBJECT(O)) return false; |
| 971 var spreadable = O[symbolIsConcatSpreadable]; | 975 var spreadable = O[symbolIsConcatSpreadable]; |
| 972 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); | 976 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); |
| 973 return ToBoolean(spreadable); | 977 return ToBoolean(spreadable); |
| 974 } | 978 } |
| 975 | 979 |
| 976 | 980 |
| 977 // ECMA-262, section 8.6.2.6, page 28. | 981 // ECMA-262, section 8.6.2.6, page 28. |
| 978 function DefaultNumber(x) { | 982 function DefaultNumber(x) { |
| 979 if (!IS_SYMBOL_WRAPPER(x)) { | 983 if (!IS_SYMBOL_WRAPPER(x) && !IS_FLOAT32X4_WRAPPER(x)) { |
| 980 var valueOf = x.valueOf; | 984 var valueOf = x.valueOf; |
| 981 if (IS_SPEC_FUNCTION(valueOf)) { | 985 if (IS_SPEC_FUNCTION(valueOf)) { |
| 982 var v = %_CallFunction(x, valueOf); | 986 var v = %_CallFunction(x, valueOf); |
| 983 if (IsPrimitive(v)) return v; | 987 if (IsPrimitive(v)) return v; |
| 984 } | 988 } |
| 985 | 989 |
| 986 var toString = x.toString; | 990 var toString = x.toString; |
| 987 if (IS_SPEC_FUNCTION(toString)) { | 991 if (IS_SPEC_FUNCTION(toString)) { |
| 988 var s = %_CallFunction(x, toString); | 992 var s = %_CallFunction(x, toString); |
| 989 if (IsPrimitive(s)) return s; | 993 if (IsPrimitive(s)) return s; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1041 $toLength = ToLength; | 1045 $toLength = ToLength; |
| 1042 $toName = ToName; | 1046 $toName = ToName; |
| 1043 $toNumber = ToNumber; | 1047 $toNumber = ToNumber; |
| 1044 $toObject = ToObject; | 1048 $toObject = ToObject; |
| 1045 $toPositiveInteger = ToPositiveInteger; | 1049 $toPositiveInteger = ToPositiveInteger; |
| 1046 $toPrimitive = ToPrimitive; | 1050 $toPrimitive = ToPrimitive; |
| 1047 $toString = ToString; | 1051 $toString = ToString; |
| 1048 $toUint32 = ToUint32; | 1052 $toUint32 = ToUint32; |
| 1049 | 1053 |
| 1050 }) | 1054 }) |
| OLD | NEW |