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)) { |
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 if (IS_FLOAT32X4(x)) { |
| 141 if (IS_FLOAT32X4(y)) |
| 142 return %Float32x4Equals(x, y); |
| 143 return 1; // not equal |
140 } else { | 144 } else { |
141 // x is an object. | 145 // x is an object. |
142 if (IS_SPEC_OBJECT(y)) { | 146 if (IS_SPEC_OBJECT(y)) { |
143 return %_ObjectEquals(x, y) ? 0 : 1; | 147 return %_ObjectEquals(x, y) ? 0 : 1; |
144 } | 148 } |
145 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 149 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
146 if (IS_SYMBOL(y)) return 1; // not equal | 150 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
147 if (IS_BOOLEAN(y)) y = %$toNumber(y); | 151 if (IS_BOOLEAN(y)) y = %$toNumber(y); |
148 x = %$toPrimitive(x, NO_HINT); | 152 x = %$toPrimitive(x, NO_HINT); |
149 } | 153 } |
150 } | 154 } |
151 } | 155 } |
152 | 156 |
153 // ECMA-262, section 11.9.4, page 56. | 157 // ECMA-262, section 11.9.4, page 56. |
154 STRICT_EQUALS = function STRICT_EQUALS(x) { | 158 STRICT_EQUALS = function STRICT_EQUALS(x) { |
155 if (IS_STRING(this)) { | 159 if (IS_STRING(this)) { |
156 if (!IS_STRING(x)) return 1; // not equal | 160 if (!IS_STRING(x)) return 1; // not equal |
157 return %StringEquals(this, x); | 161 return %StringEquals(this, x); |
158 } | 162 } |
159 | 163 |
160 if (IS_NUMBER(this)) { | 164 if (IS_NUMBER(this)) { |
161 if (!IS_NUMBER(x)) return 1; // not equal | 165 if (!IS_NUMBER(x)) return 1; // not equal |
162 return %NumberEquals(this, x); | 166 return %NumberEquals(this, x); |
163 } | 167 } |
164 | 168 |
| 169 if (IS_FLOAT32X4(this) && IS_FLOAT32X4(x)) |
| 170 return %Float32x4Equals(this, x); |
| 171 |
165 // If anything else gets here, we just do simple identity check. | 172 // If anything else gets here, we just do simple identity check. |
166 // Objects (including functions), null, undefined and booleans were | 173 // Objects (including functions), null, undefined and booleans were |
167 // checked in the CompareStub, so there should be nothing left. | 174 // checked in the CompareStub, so there should be nothing left. |
168 return %_ObjectEquals(this, x) ? 0 : 1; | 175 return %_ObjectEquals(this, x) ? 0 : 1; |
169 } | 176 } |
170 | 177 |
171 | 178 |
172 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as | 179 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as |
173 // the result when either (or both) the operands are NaN. | 180 // the result when either (or both) the operands are NaN. |
174 COMPARE = function COMPARE(x, ncr) { | 181 COMPARE = function COMPARE(x, ncr) { |
(...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 */ | 810 */ |
804 | 811 |
805 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, | 812 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, |
806 // (1) for number hint, and (2) for string hint. | 813 // (1) for number hint, and (2) for string hint. |
807 function ToPrimitive(x, hint) { | 814 function ToPrimitive(x, hint) { |
808 // Fast case check. | 815 // Fast case check. |
809 if (IS_STRING(x)) return x; | 816 if (IS_STRING(x)) return x; |
810 // Normal behavior. | 817 // Normal behavior. |
811 if (!IS_SPEC_OBJECT(x)) return x; | 818 if (!IS_SPEC_OBJECT(x)) return x; |
812 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError(kSymbolToPrimitive); | 819 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError(kSymbolToPrimitive); |
| 820 if (IS_FLOAT32X4(x)) return x; |
813 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; | 821 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; |
814 return (hint == NUMBER_HINT) ? DefaultNumber(x) : DefaultString(x); | 822 return (hint == NUMBER_HINT) ? DefaultNumber(x) : DefaultString(x); |
815 } | 823 } |
816 | 824 |
817 | 825 |
818 // ECMA-262, section 9.2, page 30 | 826 // ECMA-262, section 9.2, page 30 |
819 function ToBoolean(x) { | 827 function ToBoolean(x) { |
820 if (IS_BOOLEAN(x)) return x; | 828 if (IS_BOOLEAN(x)) return x; |
821 if (IS_STRING(x)) return x.length != 0; | 829 if (IS_STRING(x)) return x.length != 0; |
822 if (x == null) return false; | 830 if (x == null) return false; |
823 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); | 831 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); |
824 return true; | 832 return true; |
825 } | 833 } |
826 | 834 |
827 | 835 |
828 // ECMA-262, section 9.3, page 31. | 836 // ECMA-262, section 9.3, page 31. |
829 function ToNumber(x) { | 837 function ToNumber(x) { |
830 if (IS_NUMBER(x)) return x; | 838 if (IS_NUMBER(x)) return x; |
831 if (IS_STRING(x)) { | 839 if (IS_STRING(x)) { |
832 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | 840 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
833 : %StringToNumber(x); | 841 : %StringToNumber(x); |
834 } | 842 } |
835 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 843 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
836 if (IS_UNDEFINED(x)) return NAN; | 844 if (IS_UNDEFINED(x)) return NAN; |
837 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); | 845 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); |
| 846 if (IS_FLOAT32X4(x)) throw MakeTypeError(kSimdToNumber); |
838 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); | 847 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); |
839 } | 848 } |
840 | 849 |
841 function NonNumberToNumber(x) { | 850 function NonNumberToNumber(x) { |
842 if (IS_STRING(x)) { | 851 if (IS_STRING(x)) { |
843 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | 852 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
844 : %StringToNumber(x); | 853 : %StringToNumber(x); |
845 } | 854 } |
846 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 855 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
847 if (IS_UNDEFINED(x)) return NAN; | 856 if (IS_UNDEFINED(x)) return NAN; |
848 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); | 857 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); |
| 858 if (IS_FLOAT32X4(x)) throw MakeTypeError(kSimdToNumber); |
849 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); | 859 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); |
850 } | 860 } |
851 | 861 |
852 | 862 |
853 // ECMA-262, section 9.8, page 35. | 863 // ECMA-262, section 9.8, page 35. |
854 function ToString(x) { | 864 function ToString(x) { |
855 if (IS_STRING(x)) return x; | 865 if (IS_STRING(x)) return x; |
856 if (IS_NUMBER(x)) return %_NumberToString(x); | 866 if (IS_NUMBER(x)) return %_NumberToString(x); |
857 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 867 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
858 if (IS_UNDEFINED(x)) return 'undefined'; | 868 if (IS_UNDEFINED(x)) return 'undefined'; |
(...skipping 15 matching lines...) Expand all Loading... |
874 return IS_SYMBOL(x) ? x : ToString(x); | 884 return IS_SYMBOL(x) ? x : ToString(x); |
875 } | 885 } |
876 | 886 |
877 | 887 |
878 // ECMA-262, section 9.9, page 36. | 888 // ECMA-262, section 9.9, page 36. |
879 function ToObject(x) { | 889 function ToObject(x) { |
880 if (IS_STRING(x)) return new GlobalString(x); | 890 if (IS_STRING(x)) return new GlobalString(x); |
881 if (IS_NUMBER(x)) return new GlobalNumber(x); | 891 if (IS_NUMBER(x)) return new GlobalNumber(x); |
882 if (IS_BOOLEAN(x)) return new GlobalBoolean(x); | 892 if (IS_BOOLEAN(x)) return new GlobalBoolean(x); |
883 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); | 893 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); |
| 894 if (IS_FLOAT32X4(x)) return %NewFloat32x4Wrapper(x); |
884 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { | 895 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { |
885 throw MakeTypeError(kUndefinedOrNullToObject); | 896 throw MakeTypeError(kUndefinedOrNullToObject); |
886 } | 897 } |
887 return x; | 898 return x; |
888 } | 899 } |
889 | 900 |
890 | 901 |
891 // ECMA-262, section 9.4, page 34. | 902 // ECMA-262, section 9.4, page 34. |
892 function ToInteger(x) { | 903 function ToInteger(x) { |
893 if (%_IsSmi(x)) return x; | 904 if (%_IsSmi(x)) return x; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 function IsConcatSpreadable(O) { | 980 function IsConcatSpreadable(O) { |
970 if (!IS_SPEC_OBJECT(O)) return false; | 981 if (!IS_SPEC_OBJECT(O)) return false; |
971 var spreadable = O[symbolIsConcatSpreadable]; | 982 var spreadable = O[symbolIsConcatSpreadable]; |
972 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); | 983 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); |
973 return ToBoolean(spreadable); | 984 return ToBoolean(spreadable); |
974 } | 985 } |
975 | 986 |
976 | 987 |
977 // ECMA-262, section 8.6.2.6, page 28. | 988 // ECMA-262, section 8.6.2.6, page 28. |
978 function DefaultNumber(x) { | 989 function DefaultNumber(x) { |
979 if (!IS_SYMBOL_WRAPPER(x)) { | 990 if (!IS_SYMBOL_WRAPPER(x) && !IS_FLOAT32X4_WRAPPER(x)) { |
980 var valueOf = x.valueOf; | 991 var valueOf = x.valueOf; |
981 if (IS_SPEC_FUNCTION(valueOf)) { | 992 if (IS_SPEC_FUNCTION(valueOf)) { |
982 var v = %_CallFunction(x, valueOf); | 993 var v = %_CallFunction(x, valueOf); |
983 if (IsPrimitive(v)) return v; | 994 if (IsPrimitive(v)) return v; |
984 } | 995 } |
985 | 996 |
986 var toString = x.toString; | 997 var toString = x.toString; |
987 if (IS_SPEC_FUNCTION(toString)) { | 998 if (IS_SPEC_FUNCTION(toString)) { |
988 var s = %_CallFunction(x, toString); | 999 var s = %_CallFunction(x, toString); |
989 if (IsPrimitive(s)) return s; | 1000 if (IsPrimitive(s)) return s; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1041 $toLength = ToLength; | 1052 $toLength = ToLength; |
1042 $toName = ToName; | 1053 $toName = ToName; |
1043 $toNumber = ToNumber; | 1054 $toNumber = ToNumber; |
1044 $toObject = ToObject; | 1055 $toObject = ToObject; |
1045 $toPositiveInteger = ToPositiveInteger; | 1056 $toPositiveInteger = ToPositiveInteger; |
1046 $toPrimitive = ToPrimitive; | 1057 $toPrimitive = ToPrimitive; |
1047 $toString = ToString; | 1058 $toString = ToString; |
1048 $toUint32 = ToUint32; | 1059 $toUint32 = ToUint32; |
1049 | 1060 |
1050 }) | 1061 }) |
OLD | NEW |