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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 // ECMA-262 Section 11.9.3. | 96 // ECMA-262 Section 11.9.3. |
97 EQUALS = function EQUALS(y) { | 97 EQUALS = function EQUALS(y) { |
98 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); | 98 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); |
99 var x = this; | 99 var x = this; |
100 | 100 |
101 while (true) { | 101 while (true) { |
102 if (IS_NUMBER(x)) { | 102 if (IS_NUMBER(x)) { |
103 while (true) { | 103 while (true) { |
104 if (IS_NUMBER(y)) return %NumberEquals(x, y); | 104 if (IS_NUMBER(y)) return %NumberEquals(x, y); |
105 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 105 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
106 if (IS_SYMBOL(y)) return 1; // not equal | 106 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
107 if (!IS_SPEC_OBJECT(y)) { | 107 if (!IS_SPEC_OBJECT(y)) { |
108 // String or boolean. | 108 // String or boolean. |
109 return %NumberEquals(x, %$toNumber(y)); | 109 return %NumberEquals(x, %$toNumber(y)); |
110 } | 110 } |
111 y = %$toPrimitive(y, NO_HINT); | 111 y = %$toPrimitive(y, NO_HINT); |
112 } | 112 } |
113 } else if (IS_STRING(x)) { | 113 } else if (IS_STRING(x)) { |
114 while (true) { | 114 while (true) { |
115 if (IS_STRING(y)) return %StringEquals(x, y); | 115 if (IS_STRING(y)) return %StringEquals(x, y); |
116 if (IS_SYMBOL(y)) return 1; // not equal | 116 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
117 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); | 117 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); |
118 if (IS_BOOLEAN(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); | 118 if (IS_BOOLEAN(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); |
119 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 119 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
120 y = %$toPrimitive(y, NO_HINT); | 120 y = %$toPrimitive(y, NO_HINT); |
121 } | 121 } |
122 } else if (IS_SYMBOL(x)) { | 122 } else if (IS_SYMBOL(x)) { |
123 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1; | 123 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
124 return 1; // not equal | 124 return 1; // not equal |
125 } else if (IS_BOOLEAN(x)) { | 125 } else if (IS_BOOLEAN(x)) { |
126 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; | 126 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
127 if (IS_NULL_OR_UNDEFINED(y)) return 1; | 127 if (IS_NULL_OR_UNDEFINED(y)) return 1; |
128 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); | 128 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); |
129 if (IS_STRING(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); | 129 if (IS_STRING(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); |
130 if (IS_SYMBOL(y)) return 1; // not equal | 130 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
131 // y is object. | 131 // y is object. |
132 x = %$toNumber(x); | 132 x = %$toNumber(x); |
133 y = %$toPrimitive(y, NO_HINT); | 133 y = %$toPrimitive(y, NO_HINT); |
134 } else if (IS_NULL_OR_UNDEFINED(x)) { | 134 } else if (IS_NULL_OR_UNDEFINED(x)) { |
135 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; | 135 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; |
| 136 } else if (IS_FLOAT32X4(x)) { |
| 137 if (IS_FLOAT32X4(y)) |
| 138 return %Float32x4Equals(x, y); |
| 139 return 1; // not equal |
136 } else { | 140 } else { |
137 // x is an object. | 141 // x is an object. |
138 if (IS_SPEC_OBJECT(y)) { | 142 if (IS_SPEC_OBJECT(y)) { |
139 return %_ObjectEquals(x, y) ? 0 : 1; | 143 return %_ObjectEquals(x, y) ? 0 : 1; |
140 } | 144 } |
141 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 145 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
142 if (IS_SYMBOL(y)) return 1; // not equal | 146 if (IS_SYMBOL(y) || IS_FLOAT32X4(y)) return 1; // not equal |
143 if (IS_BOOLEAN(y)) y = %$toNumber(y); | 147 if (IS_BOOLEAN(y)) y = %$toNumber(y); |
144 x = %$toPrimitive(x, NO_HINT); | 148 x = %$toPrimitive(x, NO_HINT); |
145 } | 149 } |
146 } | 150 } |
147 } | 151 } |
148 | 152 |
149 // ECMA-262, section 11.9.4, page 56. | 153 // ECMA-262, section 11.9.4, page 56. |
150 STRICT_EQUALS = function STRICT_EQUALS(x) { | 154 STRICT_EQUALS = function STRICT_EQUALS(x) { |
151 if (IS_STRING(this)) { | 155 if (IS_STRING(this)) { |
152 if (!IS_STRING(x)) return 1; // not equal | 156 if (!IS_STRING(x)) return 1; // not equal |
153 return %StringEquals(this, x); | 157 return %StringEquals(this, x); |
154 } | 158 } |
155 | 159 |
156 if (IS_NUMBER(this)) { | 160 if (IS_NUMBER(this)) { |
157 if (!IS_NUMBER(x)) return 1; // not equal | 161 if (!IS_NUMBER(x)) return 1; // not equal |
158 return %NumberEquals(this, x); | 162 return %NumberEquals(this, x); |
159 } | 163 } |
160 | 164 |
| 165 if (IS_FLOAT32X4(this) && IS_FLOAT32X4(x)) |
| 166 return %Float32x4Equals(this, x); |
| 167 |
161 // If anything else gets here, we just do simple identity check. | 168 // If anything else gets here, we just do simple identity check. |
162 // Objects (including functions), null, undefined and booleans were | 169 // Objects (including functions), null, undefined and booleans were |
163 // checked in the CompareStub, so there should be nothing left. | 170 // checked in the CompareStub, so there should be nothing left. |
164 return %_ObjectEquals(this, x) ? 0 : 1; | 171 return %_ObjectEquals(this, x) ? 0 : 1; |
165 } | 172 } |
166 | 173 |
167 | 174 |
168 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as | 175 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as |
169 // the result when either (or both) the operands are NaN. | 176 // the result when either (or both) the operands are NaN. |
170 COMPARE = function COMPARE(x, ncr) { | 177 COMPARE = function COMPARE(x, ncr) { |
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 */ | 760 */ |
754 | 761 |
755 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, | 762 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, |
756 // (1) for number hint, and (2) for string hint. | 763 // (1) for number hint, and (2) for string hint. |
757 function ToPrimitive(x, hint) { | 764 function ToPrimitive(x, hint) { |
758 // Fast case check. | 765 // Fast case check. |
759 if (IS_STRING(x)) return x; | 766 if (IS_STRING(x)) return x; |
760 // Normal behavior. | 767 // Normal behavior. |
761 if (!IS_SPEC_OBJECT(x)) return x; | 768 if (!IS_SPEC_OBJECT(x)) return x; |
762 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError(kSymbolToPrimitive); | 769 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError(kSymbolToPrimitive); |
| 770 if (IS_FLOAT32X4(x)) return x; |
763 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; | 771 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; |
764 return (hint == NUMBER_HINT) ? DefaultNumber(x) : DefaultString(x); | 772 return (hint == NUMBER_HINT) ? DefaultNumber(x) : DefaultString(x); |
765 } | 773 } |
766 | 774 |
767 | 775 |
768 // ECMA-262, section 9.2, page 30 | 776 // ECMA-262, section 9.2, page 30 |
769 function ToBoolean(x) { | 777 function ToBoolean(x) { |
770 if (IS_BOOLEAN(x)) return x; | 778 if (IS_BOOLEAN(x)) return x; |
771 if (IS_STRING(x)) return x.length != 0; | 779 if (IS_STRING(x)) return x.length != 0; |
772 if (x == null) return false; | 780 if (x == null) return false; |
773 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); | 781 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); |
774 return true; | 782 return true; |
775 } | 783 } |
776 | 784 |
777 | 785 |
778 // ECMA-262, section 9.3, page 31. | 786 // ECMA-262, section 9.3, page 31. |
779 function ToNumber(x) { | 787 function ToNumber(x) { |
780 if (IS_NUMBER(x)) return x; | 788 if (IS_NUMBER(x)) return x; |
781 if (IS_STRING(x)) { | 789 if (IS_STRING(x)) { |
782 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | 790 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
783 : %StringToNumber(x); | 791 : %StringToNumber(x); |
784 } | 792 } |
785 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 793 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
786 if (IS_UNDEFINED(x)) return NAN; | 794 if (IS_UNDEFINED(x)) return NAN; |
787 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); | 795 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); |
| 796 if (IS_FLOAT32X4(x)) throw MakeTypeError(kSimdToNumber); |
788 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); | 797 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); |
789 } | 798 } |
790 | 799 |
791 function NonNumberToNumber(x) { | 800 function NonNumberToNumber(x) { |
792 if (IS_STRING(x)) { | 801 if (IS_STRING(x)) { |
793 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | 802 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
794 : %StringToNumber(x); | 803 : %StringToNumber(x); |
795 } | 804 } |
796 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 805 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
797 if (IS_UNDEFINED(x)) return NAN; | 806 if (IS_UNDEFINED(x)) return NAN; |
798 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); | 807 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); |
| 808 if (IS_FLOAT32X4(x)) throw MakeTypeError(kSimdToNumber); |
799 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); | 809 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); |
800 } | 810 } |
801 | 811 |
802 | 812 |
803 // ECMA-262, section 9.8, page 35. | 813 // ECMA-262, section 9.8, page 35. |
804 function ToString(x) { | 814 function ToString(x) { |
805 if (IS_STRING(x)) return x; | 815 if (IS_STRING(x)) return x; |
806 if (IS_NUMBER(x)) return %_NumberToString(x); | 816 if (IS_NUMBER(x)) return %_NumberToString(x); |
807 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 817 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
808 if (IS_UNDEFINED(x)) return 'undefined'; | 818 if (IS_UNDEFINED(x)) return 'undefined'; |
(...skipping 15 matching lines...) Expand all Loading... |
824 return IS_SYMBOL(x) ? x : ToString(x); | 834 return IS_SYMBOL(x) ? x : ToString(x); |
825 } | 835 } |
826 | 836 |
827 | 837 |
828 // ECMA-262, section 9.9, page 36. | 838 // ECMA-262, section 9.9, page 36. |
829 function ToObject(x) { | 839 function ToObject(x) { |
830 if (IS_STRING(x)) return new GlobalString(x); | 840 if (IS_STRING(x)) return new GlobalString(x); |
831 if (IS_NUMBER(x)) return new GlobalNumber(x); | 841 if (IS_NUMBER(x)) return new GlobalNumber(x); |
832 if (IS_BOOLEAN(x)) return new GlobalBoolean(x); | 842 if (IS_BOOLEAN(x)) return new GlobalBoolean(x); |
833 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); | 843 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); |
| 844 if (IS_FLOAT32X4(x)) return %NewFloat32x4Wrapper(x); |
834 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { | 845 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { |
835 throw MakeTypeError(kUndefinedOrNullToObject); | 846 throw MakeTypeError(kUndefinedOrNullToObject); |
836 } | 847 } |
837 return x; | 848 return x; |
838 } | 849 } |
839 | 850 |
840 | 851 |
841 // ECMA-262, section 9.4, page 34. | 852 // ECMA-262, section 9.4, page 34. |
842 function ToInteger(x) { | 853 function ToInteger(x) { |
843 if (%_IsSmi(x)) return x; | 854 if (%_IsSmi(x)) return x; |
(...skipping 27 matching lines...) Expand all Loading... |
871 // ES5, section 9.12 | 882 // ES5, section 9.12 |
872 function SameValue(x, y) { | 883 function SameValue(x, y) { |
873 if (typeof x != typeof y) return false; | 884 if (typeof x != typeof y) return false; |
874 if (IS_NUMBER(x)) { | 885 if (IS_NUMBER(x)) { |
875 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; | 886 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; |
876 // x is +0 and y is -0 or vice versa. | 887 // x is +0 and y is -0 or vice versa. |
877 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { | 888 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { |
878 return false; | 889 return false; |
879 } | 890 } |
880 } | 891 } |
| 892 if (IS_FLOAT32X4(x)) { |
| 893 return %Float32x4SameValue(x, y); |
| 894 } |
881 return x === y; | 895 return x === y; |
882 } | 896 } |
883 | 897 |
884 | 898 |
885 // ES6, section 7.2.4 | 899 // ES6, section 7.2.4 |
886 function SameValueZero(x, y) { | 900 function SameValueZero(x, y) { |
887 if (typeof x != typeof y) return false; | 901 if (typeof x != typeof y) return false; |
888 if (IS_NUMBER(x)) { | 902 if (IS_NUMBER(x)) { |
889 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; | 903 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; |
890 } | 904 } |
| 905 if (IS_FLOAT32X4(x)) { |
| 906 return %Float32x4SameValueZero(x, y); |
| 907 } |
891 return x === y; | 908 return x === y; |
892 } | 909 } |
893 | 910 |
| 911 |
894 function ConcatIterableToArray(target, iterable) { | 912 function ConcatIterableToArray(target, iterable) { |
895 var index = target.length; | 913 var index = target.length; |
896 for (var element of iterable) { | 914 for (var element of iterable) { |
897 %AddElement(target, index++, element); | 915 %AddElement(target, index++, element); |
898 } | 916 } |
899 return target; | 917 return target; |
900 } | 918 } |
901 | 919 |
902 | 920 |
903 /* --------------------------------- | 921 /* --------------------------------- |
(...skipping 15 matching lines...) Expand all Loading... |
919 function IsConcatSpreadable(O) { | 937 function IsConcatSpreadable(O) { |
920 if (!IS_SPEC_OBJECT(O)) return false; | 938 if (!IS_SPEC_OBJECT(O)) return false; |
921 var spreadable = O[symbolIsConcatSpreadable]; | 939 var spreadable = O[symbolIsConcatSpreadable]; |
922 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); | 940 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); |
923 return ToBoolean(spreadable); | 941 return ToBoolean(spreadable); |
924 } | 942 } |
925 | 943 |
926 | 944 |
927 // ECMA-262, section 8.6.2.6, page 28. | 945 // ECMA-262, section 8.6.2.6, page 28. |
928 function DefaultNumber(x) { | 946 function DefaultNumber(x) { |
929 if (!IS_SYMBOL_WRAPPER(x)) { | 947 if (!IS_SYMBOL_WRAPPER(x) && !IS_FLOAT32X4_WRAPPER(x)) { |
930 var valueOf = x.valueOf; | 948 var valueOf = x.valueOf; |
931 if (IS_SPEC_FUNCTION(valueOf)) { | 949 if (IS_SPEC_FUNCTION(valueOf)) { |
932 var v = %_CallFunction(x, valueOf); | 950 var v = %_CallFunction(x, valueOf); |
933 if (IsPrimitive(v)) return v; | 951 if (IsPrimitive(v)) return v; |
934 } | 952 } |
935 | 953 |
936 var toString = x.toString; | 954 var toString = x.toString; |
937 if (IS_SPEC_FUNCTION(toString)) { | 955 if (IS_SPEC_FUNCTION(toString)) { |
938 var s = %_CallFunction(x, toString); | 956 var s = %_CallFunction(x, toString); |
939 if (IsPrimitive(s)) return s; | 957 if (IsPrimitive(s)) return s; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 $toLength = ToLength; | 1009 $toLength = ToLength; |
992 $toName = ToName; | 1010 $toName = ToName; |
993 $toNumber = ToNumber; | 1011 $toNumber = ToNumber; |
994 $toObject = ToObject; | 1012 $toObject = ToObject; |
995 $toPositiveInteger = ToPositiveInteger; | 1013 $toPositiveInteger = ToPositiveInteger; |
996 $toPrimitive = ToPrimitive; | 1014 $toPrimitive = ToPrimitive; |
997 $toString = ToString; | 1015 $toString = ToString; |
998 $toUint32 = ToUint32; | 1016 $toUint32 = ToUint32; |
999 | 1017 |
1000 }) | 1018 }) |
OLD | NEW |