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 /* ----------------------------------- | 12 /* ----------------------------------- |
13 - - - C o m p a r i s o n - - - | 13 - - - C o m p a r i s o n - - - |
14 ----------------------------------- | 14 ----------------------------------- |
15 */ | 15 */ |
16 | 16 |
17 // The following declarations are shared with other native JS files. | 17 // The following declarations are shared with other native JS files. |
18 // They are all declared at this one spot to avoid redeclaration errors. | 18 // They are all declared at this one spot to avoid redeclaration errors. |
19 var $Object = global.Object; | 19 var EQUALS; |
20 var $Array = global.Array; | 20 var STRICT_EQUALS; |
21 var $String = global.String; | 21 var COMPARE; |
22 var $Number = global.Number; | 22 var ADD; |
23 var $Function = global.Function; | 23 var ADD_STRONG; |
24 var $Boolean = global.Boolean; | 24 var STRING_ADD_LEFT; |
25 var $NaN = %GetRootNaN(); | 25 var STRING_ADD_LEFT_STRONG; |
| 26 var STRING_ADD_RIGHT; |
| 27 var STRING_ADD_RIGHT_STRONG; |
| 28 var SUB; |
| 29 var SUB_STRONG; |
| 30 var MUL; |
| 31 var MUL_STRONG; |
| 32 var DIV; |
| 33 var DIV_STRONG; |
| 34 var MOD; |
| 35 var MOD_STRONG; |
| 36 var BIT_OR; |
| 37 var BIT_OR_STRONG; |
| 38 var BIT_AND; |
| 39 var BIT_AND_STRONG; |
| 40 var BIT_XOR; |
| 41 var BIT_XOR_STRONG; |
| 42 var SHL; |
| 43 var SHL_STRONG; |
| 44 var SAR; |
| 45 var SAR_STRONG; |
| 46 var SHR; |
| 47 var SHR_STRONG; |
| 48 var DELETE; |
| 49 var IN; |
| 50 var INSTANCE_OF; |
| 51 var FILTER_KEY; |
| 52 var CALL_NON_FUNCTION; |
| 53 var CALL_NON_FUNCTION_AS_CONSTRUCTOR; |
| 54 var CALL_FUNCTION_PROXY; |
| 55 var CALL_FUNCTION_PROXY_AS_CONSTRUCTOR; |
| 56 var APPLY_PREPARE; |
| 57 var REFLECT_APPLY_PREPARE; |
| 58 var REFLECT_CONSTRUCT_PREPARE; |
| 59 var STACK_OVERFLOW; |
| 60 var TO_OBJECT; |
| 61 var TO_NUMBER; |
| 62 var TO_STRING; |
| 63 var TO_NAME; |
| 64 |
| 65 var STRING_LENGTH_STUB; |
| 66 |
| 67 var $defaultNumber; |
| 68 var $defaultString; |
| 69 var $NaN; |
| 70 var $nonNumberToNumber; |
| 71 var $nonStringToString; |
| 72 var $sameValue; |
| 73 var $sameValueZero; |
| 74 var $toBoolean; |
| 75 var $toInt32; |
| 76 var $toInteger; |
| 77 var $toLength; |
| 78 var $toName; |
| 79 var $toNumber; |
| 80 var $toObject; |
| 81 var $toPositiveInteger; |
| 82 var $toPrimitive; |
| 83 var $toString; |
| 84 var $toUint32; |
| 85 |
| 86 (function() { |
| 87 |
| 88 %CheckIsBootstrapping(); |
| 89 |
| 90 var GlobalArray = global.Array; |
| 91 var GlobalBoolean = global.Boolean; |
| 92 var GlobalString = global.String; |
| 93 var GlobalNumber = global.Number; |
| 94 |
| 95 // ---------------------------------------------------------------------------- |
26 | 96 |
27 // ECMA-262 Section 11.9.3. | 97 // ECMA-262 Section 11.9.3. |
28 function EQUALS(y) { | 98 EQUALS = function EQUALS(y) { |
29 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); | 99 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); |
30 var x = this; | 100 var x = this; |
31 | 101 |
32 while (true) { | 102 while (true) { |
33 if (IS_NUMBER(x)) { | 103 if (IS_NUMBER(x)) { |
34 while (true) { | 104 while (true) { |
35 if (IS_NUMBER(y)) return %NumberEquals(x, y); | 105 if (IS_NUMBER(y)) return %NumberEquals(x, y); |
36 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 106 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
37 if (IS_SYMBOL(y)) return 1; // not equal | 107 if (IS_SYMBOL(y)) return 1; // not equal |
38 if (!IS_SPEC_OBJECT(y)) { | 108 if (!IS_SPEC_OBJECT(y)) { |
39 // String or boolean. | 109 // String or boolean. |
40 return %NumberEquals(x, %ToNumber(y)); | 110 return %NumberEquals(x, %$toNumber(y)); |
41 } | 111 } |
42 y = %ToPrimitive(y, NO_HINT); | 112 y = %$toPrimitive(y, NO_HINT); |
43 } | 113 } |
44 } else if (IS_STRING(x)) { | 114 } else if (IS_STRING(x)) { |
45 while (true) { | 115 while (true) { |
46 if (IS_STRING(y)) return %StringEquals(x, y); | 116 if (IS_STRING(y)) return %StringEquals(x, y); |
47 if (IS_SYMBOL(y)) return 1; // not equal | 117 if (IS_SYMBOL(y)) return 1; // not equal |
48 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); | 118 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); |
49 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); | 119 if (IS_BOOLEAN(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); |
50 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 120 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
51 y = %ToPrimitive(y, NO_HINT); | 121 y = %$toPrimitive(y, NO_HINT); |
52 } | 122 } |
53 } else if (IS_SYMBOL(x)) { | 123 } else if (IS_SYMBOL(x)) { |
54 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1; | 124 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
55 return 1; // not equal | 125 return 1; // not equal |
56 } else if (IS_BOOLEAN(x)) { | 126 } else if (IS_BOOLEAN(x)) { |
57 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; | 127 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
58 if (IS_NULL_OR_UNDEFINED(y)) return 1; | 128 if (IS_NULL_OR_UNDEFINED(y)) return 1; |
59 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); | 129 if (IS_NUMBER(y)) return %NumberEquals(%$toNumber(x), y); |
60 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); | 130 if (IS_STRING(y)) return %NumberEquals(%$toNumber(x), %$toNumber(y)); |
61 if (IS_SYMBOL(y)) return 1; // not equal | 131 if (IS_SYMBOL(y)) return 1; // not equal |
62 // y is object. | 132 // y is object. |
63 x = %ToNumber(x); | 133 x = %$toNumber(x); |
64 y = %ToPrimitive(y, NO_HINT); | 134 y = %$toPrimitive(y, NO_HINT); |
65 } else if (IS_NULL_OR_UNDEFINED(x)) { | 135 } else if (IS_NULL_OR_UNDEFINED(x)) { |
66 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; | 136 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; |
67 } else { | 137 } else { |
68 // x is an object. | 138 // x is an object. |
69 if (IS_SPEC_OBJECT(y)) { | 139 if (IS_SPEC_OBJECT(y)) { |
70 return %_ObjectEquals(x, y) ? 0 : 1; | 140 return %_ObjectEquals(x, y) ? 0 : 1; |
71 } | 141 } |
72 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 142 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
73 if (IS_SYMBOL(y)) return 1; // not equal | 143 if (IS_SYMBOL(y)) return 1; // not equal |
74 if (IS_BOOLEAN(y)) y = %ToNumber(y); | 144 if (IS_BOOLEAN(y)) y = %$toNumber(y); |
75 x = %ToPrimitive(x, NO_HINT); | 145 x = %$toPrimitive(x, NO_HINT); |
76 } | 146 } |
77 } | 147 } |
78 } | 148 } |
79 | 149 |
80 // ECMA-262, section 11.9.4, page 56. | 150 // ECMA-262, section 11.9.4, page 56. |
81 function STRICT_EQUALS(x) { | 151 STRICT_EQUALS = function STRICT_EQUALS(x) { |
82 if (IS_STRING(this)) { | 152 if (IS_STRING(this)) { |
83 if (!IS_STRING(x)) return 1; // not equal | 153 if (!IS_STRING(x)) return 1; // not equal |
84 return %StringEquals(this, x); | 154 return %StringEquals(this, x); |
85 } | 155 } |
86 | 156 |
87 if (IS_NUMBER(this)) { | 157 if (IS_NUMBER(this)) { |
88 if (!IS_NUMBER(x)) return 1; // not equal | 158 if (!IS_NUMBER(x)) return 1; // not equal |
89 return %NumberEquals(this, x); | 159 return %NumberEquals(this, x); |
90 } | 160 } |
91 | 161 |
92 // If anything else gets here, we just do simple identity check. | 162 // If anything else gets here, we just do simple identity check. |
93 // Objects (including functions), null, undefined and booleans were | 163 // Objects (including functions), null, undefined and booleans were |
94 // checked in the CompareStub, so there should be nothing left. | 164 // checked in the CompareStub, so there should be nothing left. |
95 return %_ObjectEquals(this, x) ? 0 : 1; | 165 return %_ObjectEquals(this, x) ? 0 : 1; |
96 } | 166 } |
97 | 167 |
98 | 168 |
99 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as | 169 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as |
100 // the result when either (or both) the operands are NaN. | 170 // the result when either (or both) the operands are NaN. |
101 function COMPARE(x, ncr) { | 171 COMPARE = function COMPARE(x, ncr) { |
102 var left; | 172 var left; |
103 var right; | 173 var right; |
104 // Fast cases for string, numbers and undefined compares. | 174 // Fast cases for string, numbers and undefined compares. |
105 if (IS_STRING(this)) { | 175 if (IS_STRING(this)) { |
106 if (IS_STRING(x)) return %_StringCompare(this, x); | 176 if (IS_STRING(x)) return %_StringCompare(this, x); |
107 if (IS_UNDEFINED(x)) return ncr; | 177 if (IS_UNDEFINED(x)) return ncr; |
108 left = this; | 178 left = this; |
109 } else if (IS_NUMBER(this)) { | 179 } else if (IS_NUMBER(this)) { |
110 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr); | 180 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr); |
111 if (IS_UNDEFINED(x)) return ncr; | 181 if (IS_UNDEFINED(x)) return ncr; |
112 left = this; | 182 left = this; |
113 } else if (IS_UNDEFINED(this)) { | 183 } else if (IS_UNDEFINED(this)) { |
114 if (!IS_UNDEFINED(x)) { | 184 if (!IS_UNDEFINED(x)) { |
115 %ToPrimitive(x, NUMBER_HINT); | 185 %$toPrimitive(x, NUMBER_HINT); |
116 } | 186 } |
117 return ncr; | 187 return ncr; |
118 } else if (IS_UNDEFINED(x)) { | 188 } else if (IS_UNDEFINED(x)) { |
119 %ToPrimitive(this, NUMBER_HINT); | 189 %$toPrimitive(this, NUMBER_HINT); |
120 return ncr; | 190 return ncr; |
121 } else { | 191 } else { |
122 left = %ToPrimitive(this, NUMBER_HINT); | 192 left = %$toPrimitive(this, NUMBER_HINT); |
123 } | 193 } |
124 | 194 |
125 right = %ToPrimitive(x, NUMBER_HINT); | 195 right = %$toPrimitive(x, NUMBER_HINT); |
126 if (IS_STRING(left) && IS_STRING(right)) { | 196 if (IS_STRING(left) && IS_STRING(right)) { |
127 return %_StringCompare(left, right); | 197 return %_StringCompare(left, right); |
128 } else { | 198 } else { |
129 var left_number = %ToNumber(left); | 199 var left_number = %$toNumber(left); |
130 var right_number = %ToNumber(right); | 200 var right_number = %$toNumber(right); |
131 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr; | 201 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr; |
132 return %NumberCompare(left_number, right_number, ncr); | 202 return %NumberCompare(left_number, right_number, ncr); |
133 } | 203 } |
134 } | 204 } |
135 | 205 |
136 | 206 |
137 | 207 |
138 /* ----------------------------------- | 208 /* ----------------------------------- |
139 - - - A r i t h m e t i c - - - | 209 - - - A r i t h m e t i c - - - |
140 ----------------------------------- | 210 ----------------------------------- |
141 */ | 211 */ |
142 | 212 |
143 // ECMA-262, section 11.6.1, page 50. | 213 // ECMA-262, section 11.6.1, page 50. |
144 function ADD(x) { | 214 ADD = function ADD(x) { |
145 // Fast case: Check for number operands and do the addition. | 215 // Fast case: Check for number operands and do the addition. |
146 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x); | 216 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x); |
147 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x); | 217 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x); |
148 | 218 |
149 // Default implementation. | 219 // Default implementation. |
150 var a = %ToPrimitive(this, NO_HINT); | 220 var a = %$toPrimitive(this, NO_HINT); |
151 var b = %ToPrimitive(x, NO_HINT); | 221 var b = %$toPrimitive(x, NO_HINT); |
152 | 222 |
153 if (IS_STRING(a)) { | 223 if (IS_STRING(a)) { |
154 return %_StringAdd(a, %ToString(b)); | 224 return %_StringAdd(a, %$toString(b)); |
155 } else if (IS_STRING(b)) { | 225 } else if (IS_STRING(b)) { |
156 return %_StringAdd(%NonStringToString(a), b); | 226 return %_StringAdd(%$nonStringToString(a), b); |
157 } else { | 227 } else { |
158 return %NumberAdd(%ToNumber(a), %ToNumber(b)); | 228 return %NumberAdd(%$toNumber(a), %$toNumber(b)); |
159 } | 229 } |
160 } | 230 } |
161 | 231 |
162 | 232 |
163 // Strong mode ADD throws if an implicit conversion would be performed | 233 // Strong mode ADD throws if an implicit conversion would be performed |
164 function ADD_STRONG(x) { | 234 ADD_STRONG = function ADD_STRONG(x) { |
165 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x); | 235 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x); |
166 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x); | 236 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x); |
167 | 237 |
168 throw %MakeTypeError('strong_implicit_cast'); | 238 throw %MakeTypeError('strong_implicit_cast'); |
169 } | 239 } |
170 | 240 |
171 | 241 |
172 // Left operand (this) is already a string. | 242 // Left operand (this) is already a string. |
173 function STRING_ADD_LEFT(y) { | 243 STRING_ADD_LEFT = function STRING_ADD_LEFT(y) { |
174 if (!IS_STRING(y)) { | 244 if (!IS_STRING(y)) { |
175 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) { | 245 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) { |
176 y = %_ValueOf(y); | 246 y = %_ValueOf(y); |
177 } else { | 247 } else { |
178 y = IS_NUMBER(y) | 248 y = IS_NUMBER(y) |
179 ? %_NumberToString(y) | 249 ? %_NumberToString(y) |
180 : %ToString(%ToPrimitive(y, NO_HINT)); | 250 : %$toString(%$toPrimitive(y, NO_HINT)); |
181 } | 251 } |
182 } | 252 } |
183 return %_StringAdd(this, y); | 253 return %_StringAdd(this, y); |
184 } | 254 } |
185 | 255 |
186 | 256 |
187 // Left operand (this) is already a string. | 257 // Left operand (this) is already a string. |
188 function STRING_ADD_LEFT_STRONG(y) { | 258 STRING_ADD_LEFT_STRONG = function STRING_ADD_LEFT_STRONG(y) { |
189 if (IS_STRING(y)) { | 259 if (IS_STRING(y)) { |
190 return %_StringAdd(this, y); | 260 return %_StringAdd(this, y); |
191 } | 261 } |
192 throw %MakeTypeError('strong_implicit_cast'); | 262 throw %MakeTypeError('strong_implicit_cast'); |
193 } | 263 } |
194 | 264 |
195 | 265 |
196 // Right operand (y) is already a string. | 266 // Right operand (y) is already a string. |
197 function STRING_ADD_RIGHT(y) { | 267 STRING_ADD_RIGHT = function STRING_ADD_RIGHT(y) { |
198 var x = this; | 268 var x = this; |
199 if (!IS_STRING(x)) { | 269 if (!IS_STRING(x)) { |
200 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) { | 270 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) { |
201 x = %_ValueOf(x); | 271 x = %_ValueOf(x); |
202 } else { | 272 } else { |
203 x = IS_NUMBER(x) | 273 x = IS_NUMBER(x) |
204 ? %_NumberToString(x) | 274 ? %_NumberToString(x) |
205 : %ToString(%ToPrimitive(x, NO_HINT)); | 275 : %$toString(%$toPrimitive(x, NO_HINT)); |
206 } | 276 } |
207 } | 277 } |
208 return %_StringAdd(x, y); | 278 return %_StringAdd(x, y); |
209 } | 279 } |
210 | 280 |
211 | 281 |
212 // Right operand (y) is already a string. | 282 // Right operand (y) is already a string. |
213 function STRING_ADD_RIGHT_STRONG(y) { | 283 STRING_ADD_RIGHT_STRONG = function STRING_ADD_RIGHT_STRONG(y) { |
214 if (IS_STRING(this)) { | 284 if (IS_STRING(this)) { |
215 return %_StringAdd(this, y); | 285 return %_StringAdd(this, y); |
216 } | 286 } |
217 throw %MakeTypeError('strong_implicit_cast'); | 287 throw %MakeTypeError('strong_implicit_cast'); |
218 } | 288 } |
219 | 289 |
220 | 290 |
221 // ECMA-262, section 11.6.2, page 50. | 291 // ECMA-262, section 11.6.2, page 50. |
222 function SUB(y) { | 292 SUB = function SUB(y) { |
223 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); | 293 var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this); |
224 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 294 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
225 return %NumberSub(x, y); | 295 return %NumberSub(x, y); |
226 } | 296 } |
227 | 297 |
228 | 298 |
229 // Strong mode SUB throws if an implicit conversion would be performed | 299 // Strong mode SUB throws if an implicit conversion would be performed |
230 function SUB_STRONG(y) { | 300 SUB_STRONG = function SUB_STRONG(y) { |
231 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 301 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
232 return %NumberSub(this, y); | 302 return %NumberSub(this, y); |
233 } | 303 } |
234 throw %MakeTypeError('strong_implicit_cast'); | 304 throw %MakeTypeError('strong_implicit_cast'); |
235 } | 305 } |
236 | 306 |
237 | 307 |
238 // ECMA-262, section 11.5.1, page 48. | 308 // ECMA-262, section 11.5.1, page 48. |
239 function MUL(y) { | 309 MUL = function MUL(y) { |
240 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); | 310 var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this); |
241 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 311 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
242 return %NumberMul(x, y); | 312 return %NumberMul(x, y); |
243 } | 313 } |
244 | 314 |
245 | 315 |
246 // Strong mode MUL throws if an implicit conversion would be performed | 316 // Strong mode MUL throws if an implicit conversion would be performed |
247 function MUL_STRONG(y) { | 317 MUL_STRONG = function MUL_STRONG(y) { |
248 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 318 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
249 return %NumberMul(this, y); | 319 return %NumberMul(this, y); |
250 } | 320 } |
251 throw %MakeTypeError('strong_implicit_cast'); | 321 throw %MakeTypeError('strong_implicit_cast'); |
252 } | 322 } |
253 | 323 |
254 | 324 |
255 // ECMA-262, section 11.5.2, page 49. | 325 // ECMA-262, section 11.5.2, page 49. |
256 function DIV(y) { | 326 DIV = function DIV(y) { |
257 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); | 327 var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this); |
258 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 328 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
259 return %NumberDiv(x, y); | 329 return %NumberDiv(x, y); |
260 } | 330 } |
261 | 331 |
262 | 332 |
263 // Strong mode DIV throws if an implicit conversion would be performed | 333 // Strong mode DIV throws if an implicit conversion would be performed |
264 function DIV_STRONG(y) { | 334 DIV_STRONG = function DIV_STRONG(y) { |
265 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 335 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
266 return %NumberDiv(this, y); | 336 return %NumberDiv(this, y); |
267 } | 337 } |
268 throw %MakeTypeError('strong_implicit_cast'); | 338 throw %MakeTypeError('strong_implicit_cast'); |
269 } | 339 } |
270 | 340 |
271 | 341 |
272 // ECMA-262, section 11.5.3, page 49. | 342 // ECMA-262, section 11.5.3, page 49. |
273 function MOD(y) { | 343 MOD = function MOD(y) { |
274 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); | 344 var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this); |
275 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 345 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
276 return %NumberMod(x, y); | 346 return %NumberMod(x, y); |
277 } | 347 } |
278 | 348 |
279 | 349 |
280 // Strong mode MOD throws if an implicit conversion would be performed | 350 // Strong mode MOD throws if an implicit conversion would be performed |
281 function MOD_STRONG(y) { | 351 MOD_STRONG = function MOD_STRONG(y) { |
282 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 352 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
283 return %NumberMod(this, y); | 353 return %NumberMod(this, y); |
284 } | 354 } |
285 throw %MakeTypeError('strong_implicit_cast'); | 355 throw %MakeTypeError('strong_implicit_cast'); |
286 } | 356 } |
287 | 357 |
288 | 358 |
289 /* ------------------------------------------- | 359 /* ------------------------------------------- |
290 - - - B i t o p e r a t i o n s - - - | 360 - - - B i t o p e r a t i o n s - - - |
291 ------------------------------------------- | 361 ------------------------------------------- |
292 */ | 362 */ |
293 | 363 |
294 // ECMA-262, section 11.10, page 57. | 364 // ECMA-262, section 11.10, page 57. |
295 function BIT_OR(y) { | 365 BIT_OR = function BIT_OR(y) { |
296 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); | 366 var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this); |
297 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 367 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
298 return %NumberOr(x, y); | 368 return %NumberOr(x, y); |
299 } | 369 } |
300 | 370 |
301 | 371 |
302 // Strong mode BIT_OR throws if an implicit conversion would be performed | 372 // Strong mode BIT_OR throws if an implicit conversion would be performed |
303 function BIT_OR_STRONG(y) { | 373 BIT_OR_STRONG = function BIT_OR_STRONG(y) { |
304 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 374 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
305 return %NumberOr(this, y); | 375 return %NumberOr(this, y); |
306 } | 376 } |
307 throw %MakeTypeError('strong_implicit_cast'); | 377 throw %MakeTypeError('strong_implicit_cast'); |
308 } | 378 } |
309 | 379 |
310 | 380 |
311 // ECMA-262, section 11.10, page 57. | 381 // ECMA-262, section 11.10, page 57. |
312 function BIT_AND(y) { | 382 BIT_AND = function BIT_AND(y) { |
313 var x; | 383 var x; |
314 if (IS_NUMBER(this)) { | 384 if (IS_NUMBER(this)) { |
315 x = this; | 385 x = this; |
316 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 386 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
317 } else { | 387 } else { |
318 x = %NonNumberToNumber(this); | 388 x = %$nonNumberToNumber(this); |
319 // Make sure to convert the right operand to a number before | 389 // Make sure to convert the right operand to a number before |
320 // bailing out in the fast case, but after converting the | 390 // bailing out in the fast case, but after converting the |
321 // left operand. This ensures that valueOf methods on the right | 391 // left operand. This ensures that valueOf methods on the right |
322 // operand are always executed. | 392 // operand are always executed. |
323 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 393 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
324 // Optimize for the case where we end up AND'ing a value | 394 // Optimize for the case where we end up AND'ing a value |
325 // that doesn't convert to a number. This is common in | 395 // that doesn't convert to a number. This is common in |
326 // certain benchmarks. | 396 // certain benchmarks. |
327 if (NUMBER_IS_NAN(x)) return 0; | 397 if (NUMBER_IS_NAN(x)) return 0; |
328 } | 398 } |
329 return %NumberAnd(x, y); | 399 return %NumberAnd(x, y); |
330 } | 400 } |
331 | 401 |
332 | 402 |
333 // Strong mode BIT_AND throws if an implicit conversion would be performed | 403 // Strong mode BIT_AND throws if an implicit conversion would be performed |
334 function BIT_AND_STRONG(y) { | 404 BIT_AND_STRONG = function BIT_AND_STRONG(y) { |
335 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 405 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
336 return %NumberAnd(this, y); | 406 return %NumberAnd(this, y); |
337 } | 407 } |
338 throw %MakeTypeError('strong_implicit_cast'); | 408 throw %MakeTypeError('strong_implicit_cast'); |
339 } | 409 } |
340 | 410 |
341 | 411 |
342 // ECMA-262, section 11.10, page 57. | 412 // ECMA-262, section 11.10, page 57. |
343 function BIT_XOR(y) { | 413 BIT_XOR = function BIT_XOR(y) { |
344 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); | 414 var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this); |
345 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 415 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
346 return %NumberXor(x, y); | 416 return %NumberXor(x, y); |
347 } | 417 } |
348 | 418 |
349 | 419 |
350 // Strong mode BIT_XOR throws if an implicit conversion would be performed | 420 // Strong mode BIT_XOR throws if an implicit conversion would be performed |
351 function BIT_XOR_STRONG(y) { | 421 BIT_XOR_STRONG = function BIT_XOR_STRONG(y) { |
352 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 422 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
353 return %NumberXor(this, y); | 423 return %NumberXor(this, y); |
354 } | 424 } |
355 throw %MakeTypeError('strong_implicit_cast'); | 425 throw %MakeTypeError('strong_implicit_cast'); |
356 } | 426 } |
357 | 427 |
358 | 428 |
359 // ECMA-262, section 11.7.1, page 51. | 429 // ECMA-262, section 11.7.1, page 51. |
360 function SHL(y) { | 430 SHL = function SHL(y) { |
361 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); | 431 var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this); |
362 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 432 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
363 return %NumberShl(x, y); | 433 return %NumberShl(x, y); |
364 } | 434 } |
365 | 435 |
366 | 436 |
367 // Strong mode SHL throws if an implicit conversion would be performed | 437 // Strong mode SHL throws if an implicit conversion would be performed |
368 function SHL_STRONG(y) { | 438 SHL_STRONG = function SHL_STRONG(y) { |
369 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 439 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
370 return %NumberShl(this, y); | 440 return %NumberShl(this, y); |
371 } | 441 } |
372 throw %MakeTypeError('strong_implicit_cast'); | 442 throw %MakeTypeError('strong_implicit_cast'); |
373 } | 443 } |
374 | 444 |
375 | 445 |
376 // ECMA-262, section 11.7.2, page 51. | 446 // ECMA-262, section 11.7.2, page 51. |
377 function SAR(y) { | 447 SAR = function SAR(y) { |
378 var x; | 448 var x; |
379 if (IS_NUMBER(this)) { | 449 if (IS_NUMBER(this)) { |
380 x = this; | 450 x = this; |
381 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 451 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
382 } else { | 452 } else { |
383 x = %NonNumberToNumber(this); | 453 x = %$nonNumberToNumber(this); |
384 // Make sure to convert the right operand to a number before | 454 // Make sure to convert the right operand to a number before |
385 // bailing out in the fast case, but after converting the | 455 // bailing out in the fast case, but after converting the |
386 // left operand. This ensures that valueOf methods on the right | 456 // left operand. This ensures that valueOf methods on the right |
387 // operand are always executed. | 457 // operand are always executed. |
388 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 458 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
389 // Optimize for the case where we end up shifting a value | 459 // Optimize for the case where we end up shifting a value |
390 // that doesn't convert to a number. This is common in | 460 // that doesn't convert to a number. This is common in |
391 // certain benchmarks. | 461 // certain benchmarks. |
392 if (NUMBER_IS_NAN(x)) return 0; | 462 if (NUMBER_IS_NAN(x)) return 0; |
393 } | 463 } |
394 return %NumberSar(x, y); | 464 return %NumberSar(x, y); |
395 } | 465 } |
396 | 466 |
397 | 467 |
398 // Strong mode SAR throws if an implicit conversion would be performed | 468 // Strong mode SAR throws if an implicit conversion would be performed |
399 function SAR_STRONG(y) { | 469 SAR_STRONG = function SAR_STRONG(y) { |
400 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 470 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
401 return %NumberSar(this, y); | 471 return %NumberSar(this, y); |
402 } | 472 } |
403 throw %MakeTypeError('strong_implicit_cast'); | 473 throw %MakeTypeError('strong_implicit_cast'); |
404 } | 474 } |
405 | 475 |
406 | 476 |
407 // ECMA-262, section 11.7.3, page 52. | 477 // ECMA-262, section 11.7.3, page 52. |
408 function SHR(y) { | 478 SHR = function SHR(y) { |
409 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); | 479 var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this); |
410 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); | 480 if (!IS_NUMBER(y)) y = %$nonNumberToNumber(y); |
411 return %NumberShr(x, y); | 481 return %NumberShr(x, y); |
412 } | 482 } |
413 | 483 |
414 | 484 |
415 // Strong mode SHR throws if an implicit conversion would be performed | 485 // Strong mode SHR throws if an implicit conversion would be performed |
416 function SHR_STRONG(y) { | 486 SHR_STRONG = function SHR_STRONG(y) { |
417 if (IS_NUMBER(this) && IS_NUMBER(y)) { | 487 if (IS_NUMBER(this) && IS_NUMBER(y)) { |
418 return %NumberShr(this, y); | 488 return %NumberShr(this, y); |
419 } | 489 } |
420 throw %MakeTypeError('strong_implicit_cast'); | 490 throw %MakeTypeError('strong_implicit_cast'); |
421 } | 491 } |
422 | 492 |
423 | 493 |
424 /* ----------------------------- | 494 /* ----------------------------- |
425 - - - H e l p e r s - - - | 495 - - - H e l p e r s - - - |
426 ----------------------------- | 496 ----------------------------- |
427 */ | 497 */ |
428 | 498 |
429 // ECMA-262, section 11.4.1, page 46. | 499 // ECMA-262, section 11.4.1, page 46. |
430 function DELETE(key, language_mode) { | 500 DELETE = function DELETE(key, language_mode) { |
431 return %DeleteProperty(%ToObject(this), %ToName(key), language_mode); | 501 return %DeleteProperty(%$toObject(this), %$toName(key), language_mode); |
432 } | 502 } |
433 | 503 |
434 | 504 |
435 // ECMA-262, section 11.8.7, page 54. | 505 // ECMA-262, section 11.8.7, page 54. |
436 function IN(x) { | 506 IN = function IN(x) { |
437 if (!IS_SPEC_OBJECT(x)) { | 507 if (!IS_SPEC_OBJECT(x)) { |
438 throw %MakeTypeError(kInvalidInOperatorUse, this, x); | 508 throw %MakeTypeError(kInvalidInOperatorUse, this, x); |
439 } | 509 } |
440 if (%_IsNonNegativeSmi(this)) { | 510 if (%_IsNonNegativeSmi(this)) { |
441 if (IS_ARRAY(x) && %_HasFastPackedElements(x)) { | 511 if (IS_ARRAY(x) && %_HasFastPackedElements(x)) { |
442 return this < x.length; | 512 return this < x.length; |
443 } | 513 } |
444 return %HasElement(x, this); | 514 return %HasElement(x, this); |
445 } | 515 } |
446 return %HasProperty(x, %ToName(this)); | 516 return %HasProperty(x, %$toName(this)); |
447 } | 517 } |
448 | 518 |
449 | 519 |
450 // ECMA-262, section 11.8.6, page 54. To make the implementation more | 520 // ECMA-262, section 11.8.6, page 54. To make the implementation more |
451 // efficient, the return value should be zero if the 'this' is an | 521 // efficient, the return value should be zero if the 'this' is an |
452 // instance of F, and non-zero if not. This makes it possible to avoid | 522 // instance of F, and non-zero if not. This makes it possible to avoid |
453 // an expensive ToBoolean conversion in the generated code. | 523 // an expensive ToBoolean conversion in the generated code. |
454 function INSTANCE_OF(F) { | 524 INSTANCE_OF = function INSTANCE_OF(F) { |
455 var V = this; | 525 var V = this; |
456 if (!IS_SPEC_FUNCTION(F)) { | 526 if (!IS_SPEC_FUNCTION(F)) { |
457 throw %MakeTypeError(kInstanceofFunctionExpected, F); | 527 throw %MakeTypeError(kInstanceofFunctionExpected, F); |
458 } | 528 } |
459 | 529 |
460 // If V is not an object, return false. | 530 // If V is not an object, return false. |
461 if (!IS_SPEC_OBJECT(V)) { | 531 if (!IS_SPEC_OBJECT(V)) { |
462 return 1; | 532 return 1; |
463 } | 533 } |
464 | 534 |
(...skipping 10 matching lines...) Expand all Loading... |
475 } | 545 } |
476 | 546 |
477 // Return whether or not O is in the prototype chain of V. | 547 // Return whether or not O is in the prototype chain of V. |
478 return %IsInPrototypeChain(O, V) ? 0 : 1; | 548 return %IsInPrototypeChain(O, V) ? 0 : 1; |
479 } | 549 } |
480 | 550 |
481 | 551 |
482 // Filter a given key against an object by checking if the object | 552 // Filter a given key against an object by checking if the object |
483 // has a property with the given key; return the key as a string if | 553 // has a property with the given key; return the key as a string if |
484 // it has. Otherwise returns 0 (smi). Used in for-in statements. | 554 // it has. Otherwise returns 0 (smi). Used in for-in statements. |
485 function FILTER_KEY(key) { | 555 FILTER_KEY = function FILTER_KEY(key) { |
486 var string = %ToName(key); | 556 var string = %$toName(key); |
487 if (%HasProperty(this, string)) return string; | 557 if (%HasProperty(this, string)) return string; |
488 return 0; | 558 return 0; |
489 } | 559 } |
490 | 560 |
491 | 561 |
492 function CALL_NON_FUNCTION() { | 562 CALL_NON_FUNCTION = function CALL_NON_FUNCTION() { |
493 var delegate = %GetFunctionDelegate(this); | 563 var delegate = %GetFunctionDelegate(this); |
494 if (!IS_FUNCTION(delegate)) { | 564 if (!IS_FUNCTION(delegate)) { |
495 var callsite = %RenderCallSite(); | 565 var callsite = %RenderCallSite(); |
496 if (callsite == "") callsite = typeof this; | 566 if (callsite == "") callsite = typeof this; |
497 throw %MakeTypeError(kCalledNonCallable, callsite); | 567 throw %MakeTypeError(kCalledNonCallable, callsite); |
498 } | 568 } |
499 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); | 569 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); |
500 } | 570 } |
501 | 571 |
502 | 572 |
503 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() { | 573 CALL_NON_FUNCTION_AS_CONSTRUCTOR = function CALL_NON_FUNCTION_AS_CONSTRUCTOR() { |
504 var delegate = %GetConstructorDelegate(this); | 574 var delegate = %GetConstructorDelegate(this); |
505 if (!IS_FUNCTION(delegate)) { | 575 if (!IS_FUNCTION(delegate)) { |
506 var callsite = %RenderCallSite(); | 576 var callsite = %RenderCallSite(); |
507 if (callsite == "") callsite = typeof this; | 577 if (callsite == "") callsite = typeof this; |
508 throw %MakeTypeError(kCalledNonCallable, callsite); | 578 throw %MakeTypeError(kCalledNonCallable, callsite); |
509 } | 579 } |
510 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); | 580 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); |
511 } | 581 } |
512 | 582 |
513 | 583 |
514 function CALL_FUNCTION_PROXY() { | 584 CALL_FUNCTION_PROXY = function CALL_FUNCTION_PROXY() { |
515 var arity = %_ArgumentsLength() - 1; | 585 var arity = %_ArgumentsLength() - 1; |
516 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg. | 586 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg. |
517 var trap = %GetCallTrap(proxy); | 587 var trap = %GetCallTrap(proxy); |
518 return %Apply(trap, this, arguments, 0, arity); | 588 return %Apply(trap, this, arguments, 0, arity); |
519 } | 589 } |
520 | 590 |
521 | 591 |
522 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() { | 592 CALL_FUNCTION_PROXY_AS_CONSTRUCTOR = |
| 593 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR () { |
523 var proxy = this; | 594 var proxy = this; |
524 var trap = %GetConstructTrap(proxy); | 595 var trap = %GetConstructTrap(proxy); |
525 return %Apply(trap, this, arguments, 0, %_ArgumentsLength()); | 596 return %Apply(trap, this, arguments, 0, %_ArgumentsLength()); |
526 } | 597 } |
527 | 598 |
528 | 599 |
529 function APPLY_PREPARE(args) { | 600 APPLY_PREPARE = function APPLY_PREPARE(args) { |
530 var length; | 601 var length; |
531 // First check whether length is a positive Smi and args is an | 602 // First check whether length is a positive Smi and args is an |
532 // array. This is the fast case. If this fails, we do the slow case | 603 // array. This is the fast case. If this fails, we do the slow case |
533 // that takes care of more eventualities. | 604 // that takes care of more eventualities. |
534 if (IS_ARRAY(args)) { | 605 if (IS_ARRAY(args)) { |
535 length = args.length; | 606 length = args.length; |
536 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && | 607 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && |
537 IS_SPEC_FUNCTION(this)) { | 608 IS_SPEC_FUNCTION(this)) { |
538 return length; | 609 return length; |
539 } | 610 } |
540 } | 611 } |
541 | 612 |
542 length = (args == null) ? 0 : %ToUint32(args.length); | 613 length = (args == null) ? 0 : %$toUint32(args.length); |
543 | 614 |
544 // We can handle any number of apply arguments if the stack is | 615 // We can handle any number of apply arguments if the stack is |
545 // big enough, but sanity check the value to avoid overflow when | 616 // big enough, but sanity check the value to avoid overflow when |
546 // multiplying with pointer size. | 617 // multiplying with pointer size. |
547 if (length > kSafeArgumentsLength) throw %MakeRangeError(kStackOverflow); | 618 if (length > kSafeArgumentsLength) throw %MakeRangeError(kStackOverflow); |
548 | 619 |
549 if (!IS_SPEC_FUNCTION(this)) { | 620 if (!IS_SPEC_FUNCTION(this)) { |
550 throw %MakeTypeError(kApplyNonFunction, %ToString(this), typeof this); | 621 throw %MakeTypeError(kApplyNonFunction, %$toString(this), typeof this); |
551 } | 622 } |
552 | 623 |
553 // Make sure the arguments list has the right type. | 624 // Make sure the arguments list has the right type. |
554 if (args != null && !IS_SPEC_OBJECT(args)) { | 625 if (args != null && !IS_SPEC_OBJECT(args)) { |
555 throw %MakeTypeError(kWrongArgs, "Function.prototype.apply"); | 626 throw %MakeTypeError(kWrongArgs, "Function.prototype.apply"); |
556 } | 627 } |
557 | 628 |
558 // Return the length which is the number of arguments to copy to the | 629 // Return the length which is the number of arguments to copy to the |
559 // stack. It is guaranteed to be a small integer at this point. | 630 // stack. It is guaranteed to be a small integer at this point. |
560 return length; | 631 return length; |
561 } | 632 } |
562 | 633 |
563 | 634 |
564 function REFLECT_APPLY_PREPARE(args) { | 635 REFLECT_APPLY_PREPARE = function REFLECT_APPLY_PREPARE(args) { |
565 var length; | 636 var length; |
566 // First check whether length is a positive Smi and args is an | 637 // First check whether length is a positive Smi and args is an |
567 // array. This is the fast case. If this fails, we do the slow case | 638 // array. This is the fast case. If this fails, we do the slow case |
568 // that takes care of more eventualities. | 639 // that takes care of more eventualities. |
569 if (IS_ARRAY(args)) { | 640 if (IS_ARRAY(args)) { |
570 length = args.length; | 641 length = args.length; |
571 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && | 642 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && |
572 IS_SPEC_FUNCTION(this)) { | 643 IS_SPEC_FUNCTION(this)) { |
573 return length; | 644 return length; |
574 } | 645 } |
575 } | 646 } |
576 | 647 |
577 if (!IS_SPEC_FUNCTION(this)) { | 648 if (!IS_SPEC_FUNCTION(this)) { |
578 throw %MakeTypeError(kCalledNonCallable, %ToString(this)); | 649 throw %MakeTypeError(kCalledNonCallable, %$toString(this)); |
579 } | 650 } |
580 | 651 |
581 if (!IS_SPEC_OBJECT(args)) { | 652 if (!IS_SPEC_OBJECT(args)) { |
582 throw %MakeTypeError(kWrongArgs, "Reflect.apply"); | 653 throw %MakeTypeError(kWrongArgs, "Reflect.apply"); |
583 } | 654 } |
584 | 655 |
585 length = %ToLength(args.length); | 656 length = %$toLength(args.length); |
586 | 657 |
587 // We can handle any number of apply arguments if the stack is | 658 // We can handle any number of apply arguments if the stack is |
588 // big enough, but sanity check the value to avoid overflow when | 659 // big enough, but sanity check the value to avoid overflow when |
589 // multiplying with pointer size. | 660 // multiplying with pointer size. |
590 if (length > kSafeArgumentsLength) throw %MakeRangeError(kStackOverflow); | 661 if (length > kSafeArgumentsLength) throw %MakeRangeError(kStackOverflow); |
591 | 662 |
592 // Return the length which is the number of arguments to copy to the | 663 // Return the length which is the number of arguments to copy to the |
593 // stack. It is guaranteed to be a small integer at this point. | 664 // stack. It is guaranteed to be a small integer at this point. |
594 return length; | 665 return length; |
595 } | 666 } |
596 | 667 |
597 | 668 |
598 function REFLECT_CONSTRUCT_PREPARE(args, newTarget) { | 669 REFLECT_CONSTRUCT_PREPARE = function REFLECT_CONSTRUCT_PREPARE( |
| 670 args, newTarget) { |
599 var length; | 671 var length; |
600 var ctorOk = IS_SPEC_FUNCTION(this) && %IsConstructor(this); | 672 var ctorOk = IS_SPEC_FUNCTION(this) && %IsConstructor(this); |
601 var newTargetOk = IS_SPEC_FUNCTION(newTarget) && %IsConstructor(newTarget); | 673 var newTargetOk = IS_SPEC_FUNCTION(newTarget) && %IsConstructor(newTarget); |
602 | 674 |
603 // First check whether length is a positive Smi and args is an | 675 // First check whether length is a positive Smi and args is an |
604 // array. This is the fast case. If this fails, we do the slow case | 676 // array. This is the fast case. If this fails, we do the slow case |
605 // that takes care of more eventualities. | 677 // that takes care of more eventualities. |
606 if (IS_ARRAY(args)) { | 678 if (IS_ARRAY(args)) { |
607 length = args.length; | 679 length = args.length; |
608 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && | 680 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && |
609 ctorOk && newTargetOk) { | 681 ctorOk && newTargetOk) { |
610 return length; | 682 return length; |
611 } | 683 } |
612 } | 684 } |
613 | 685 |
614 if (!ctorOk) { | 686 if (!ctorOk) { |
615 if (!IS_SPEC_FUNCTION(this)) { | 687 if (!IS_SPEC_FUNCTION(this)) { |
616 throw %MakeTypeError(kCalledNonCallable, %ToString(this)); | 688 throw %MakeTypeError(kCalledNonCallable, %$toString(this)); |
617 } else { | 689 } else { |
618 throw %MakeTypeError(kNotConstructor, %ToString(this)); | 690 throw %MakeTypeError(kNotConstructor, %$toString(this)); |
619 } | 691 } |
620 } | 692 } |
621 | 693 |
622 if (!newTargetOk) { | 694 if (!newTargetOk) { |
623 if (!IS_SPEC_FUNCTION(newTarget)) { | 695 if (!IS_SPEC_FUNCTION(newTarget)) { |
624 throw %MakeTypeError(kCalledNonCallable, %ToString(newTarget)); | 696 throw %MakeTypeError(kCalledNonCallable, %$toString(newTarget)); |
625 } else { | 697 } else { |
626 throw %MakeTypeError(kNotConstructor, %ToString(newTarget)); | 698 throw %MakeTypeError(kNotConstructor, %$toString(newTarget)); |
627 } | 699 } |
628 } | 700 } |
629 | 701 |
630 if (!IS_SPEC_OBJECT(args)) { | 702 if (!IS_SPEC_OBJECT(args)) { |
631 throw %MakeTypeError(kWrongArgs, "Reflect.construct"); | 703 throw %MakeTypeError(kWrongArgs, "Reflect.construct"); |
632 } | 704 } |
633 | 705 |
634 length = %ToLength(args.length); | 706 length = %$toLength(args.length); |
635 | 707 |
636 // We can handle any number of apply arguments if the stack is | 708 // We can handle any number of apply arguments if the stack is |
637 // big enough, but sanity check the value to avoid overflow when | 709 // big enough, but sanity check the value to avoid overflow when |
638 // multiplying with pointer size. | 710 // multiplying with pointer size. |
639 if (length > kSafeArgumentsLength) throw %MakeRangeError(kStackOverflow); | 711 if (length > kSafeArgumentsLength) throw %MakeRangeError(kStackOverflow); |
640 | 712 |
641 // Return the length which is the number of arguments to copy to the | 713 // Return the length which is the number of arguments to copy to the |
642 // stack. It is guaranteed to be a small integer at this point. | 714 // stack. It is guaranteed to be a small integer at this point. |
643 return length; | 715 return length; |
644 } | 716 } |
645 | 717 |
646 | 718 |
647 function STACK_OVERFLOW(length) { | 719 STACK_OVERFLOW = function STACK_OVERFLOW(length) { |
648 throw %MakeRangeError(kStackOverflow); | 720 throw %MakeRangeError(kStackOverflow); |
649 } | 721 } |
650 | 722 |
651 | 723 |
652 // Convert the receiver to an object - forward to ToObject. | 724 // Convert the receiver to an object - forward to ToObject. |
653 function TO_OBJECT() { | 725 TO_OBJECT = function TO_OBJECT() { |
654 return %ToObject(this); | 726 return %$toObject(this); |
655 } | 727 } |
656 | 728 |
657 | 729 |
658 // Convert the receiver to a number - forward to ToNumber. | 730 // Convert the receiver to a number - forward to ToNumber. |
659 function TO_NUMBER() { | 731 TO_NUMBER = function TO_NUMBER() { |
660 return %ToNumber(this); | 732 return %$toNumber(this); |
661 } | 733 } |
662 | 734 |
663 | 735 |
664 // Convert the receiver to a string - forward to ToString. | 736 // Convert the receiver to a string - forward to ToString. |
665 function TO_STRING() { | 737 TO_STRING = function TO_STRING() { |
666 return %ToString(this); | 738 return %$toString(this); |
667 } | 739 } |
668 | 740 |
669 | 741 |
670 // Convert the receiver to a string or symbol - forward to ToName. | 742 // Convert the receiver to a string or symbol - forward to ToName. |
671 function TO_NAME() { | 743 TO_NAME = function TO_NAME() { |
672 return %ToName(this); | 744 return %$toName(this); |
673 } | 745 } |
674 | 746 |
675 | 747 |
| 748 /* ----------------------------------------------- |
| 749 - - - J a v a S c r i p t S t u b s - - - |
| 750 ----------------------------------------------- |
| 751 */ |
| 752 |
| 753 STRING_LENGTH_STUB = function STRING_LENGTH_STUB(name) { |
| 754 var receiver = this; // implicit first parameter |
| 755 return %_StringGetLength(%_JSValueGetValue(receiver)); |
| 756 } |
| 757 |
| 758 |
676 /* ------------------------------------- | 759 /* ------------------------------------- |
677 - - - C o n v e r s i o n s - - - | 760 - - - C o n v e r s i o n s - - - |
678 ------------------------------------- | 761 ------------------------------------- |
679 */ | 762 */ |
680 | 763 |
681 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, | 764 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, |
682 // (1) for number hint, and (2) for string hint. | 765 // (1) for number hint, and (2) for string hint. |
683 function ToPrimitive(x, hint) { | 766 function ToPrimitive(x, hint) { |
684 // Fast case check. | 767 // Fast case check. |
685 if (IS_STRING(x)) return x; | 768 if (IS_STRING(x)) return x; |
686 // Normal behavior. | 769 // Normal behavior. |
687 if (!IS_SPEC_OBJECT(x)) return x; | 770 if (!IS_SPEC_OBJECT(x)) return x; |
688 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError(kSymbolToPrimitive); | 771 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError(kSymbolToPrimitive); |
689 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; | 772 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; |
690 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x); | 773 return (hint == NUMBER_HINT) ? DefaultNumber(x) : DefaultString(x); |
691 } | 774 } |
692 | 775 |
693 | 776 |
694 // ECMA-262, section 9.2, page 30 | 777 // ECMA-262, section 9.2, page 30 |
695 function ToBoolean(x) { | 778 function ToBoolean(x) { |
696 if (IS_BOOLEAN(x)) return x; | 779 if (IS_BOOLEAN(x)) return x; |
697 if (IS_STRING(x)) return x.length != 0; | 780 if (IS_STRING(x)) return x.length != 0; |
698 if (x == null) return false; | 781 if (x == null) return false; |
699 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); | 782 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); |
700 return true; | 783 return true; |
701 } | 784 } |
702 | 785 |
703 | 786 |
704 // ECMA-262, section 9.3, page 31. | 787 // ECMA-262, section 9.3, page 31. |
705 function ToNumber(x) { | 788 function ToNumber(x) { |
706 if (IS_NUMBER(x)) return x; | 789 if (IS_NUMBER(x)) return x; |
707 if (IS_STRING(x)) { | 790 if (IS_STRING(x)) { |
708 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | 791 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
709 : %StringToNumber(x); | 792 : %StringToNumber(x); |
710 } | 793 } |
711 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 794 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
712 if (IS_UNDEFINED(x)) return NAN; | 795 if (IS_UNDEFINED(x)) return NAN; |
713 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); | 796 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); |
714 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x)); | 797 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); |
715 } | 798 } |
716 | 799 |
717 function NonNumberToNumber(x) { | 800 function NonNumberToNumber(x) { |
718 if (IS_STRING(x)) { | 801 if (IS_STRING(x)) { |
719 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | 802 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
720 : %StringToNumber(x); | 803 : %StringToNumber(x); |
721 } | 804 } |
722 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 805 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
723 if (IS_UNDEFINED(x)) return NAN; | 806 if (IS_UNDEFINED(x)) return NAN; |
724 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); | 807 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToNumber); |
725 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x)); | 808 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); |
726 } | 809 } |
727 | 810 |
728 | 811 |
729 // ECMA-262, section 9.8, page 35. | 812 // ECMA-262, section 9.8, page 35. |
730 function ToString(x) { | 813 function ToString(x) { |
731 if (IS_STRING(x)) return x; | 814 if (IS_STRING(x)) return x; |
732 if (IS_NUMBER(x)) return %_NumberToString(x); | 815 if (IS_NUMBER(x)) return %_NumberToString(x); |
733 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 816 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
734 if (IS_UNDEFINED(x)) return 'undefined'; | 817 if (IS_UNDEFINED(x)) return 'undefined'; |
735 if (IS_SYMBOL(x)) throw %MakeTypeError(kSymbolToString); | 818 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToString); |
736 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); | 819 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x)); |
737 } | 820 } |
738 | 821 |
739 function NonStringToString(x) { | 822 function NonStringToString(x) { |
740 if (IS_NUMBER(x)) return %_NumberToString(x); | 823 if (IS_NUMBER(x)) return %_NumberToString(x); |
741 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 824 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
742 if (IS_UNDEFINED(x)) return 'undefined'; | 825 if (IS_UNDEFINED(x)) return 'undefined'; |
743 if (IS_SYMBOL(x)) throw %MakeTypeError(kSymbolToString); | 826 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToString); |
744 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); | 827 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x)); |
745 } | 828 } |
746 | 829 |
747 | 830 |
748 // ES6 symbols | 831 // ES6 symbols |
749 function ToName(x) { | 832 function ToName(x) { |
750 return IS_SYMBOL(x) ? x : %ToString(x); | 833 return IS_SYMBOL(x) ? x : ToString(x); |
751 } | 834 } |
752 | 835 |
753 | 836 |
754 // ECMA-262, section 9.9, page 36. | 837 // ECMA-262, section 9.9, page 36. |
755 function ToObject(x) { | 838 function ToObject(x) { |
756 if (IS_STRING(x)) return new $String(x); | 839 if (IS_STRING(x)) return new GlobalString(x); |
757 if (IS_NUMBER(x)) return new $Number(x); | 840 if (IS_NUMBER(x)) return new GlobalNumber(x); |
758 if (IS_BOOLEAN(x)) return new $Boolean(x); | 841 if (IS_BOOLEAN(x)) return new GlobalBoolean(x); |
759 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); | 842 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); |
760 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { | 843 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { |
761 throw %MakeTypeError(kUndefinedOrNullToObject); | 844 throw MakeTypeError(kUndefinedOrNullToObject); |
762 } | 845 } |
763 return x; | 846 return x; |
764 } | 847 } |
765 | 848 |
766 | 849 |
767 // ECMA-262, section 9.4, page 34. | 850 // ECMA-262, section 9.4, page 34. |
768 function ToInteger(x) { | 851 function ToInteger(x) { |
769 if (%_IsSmi(x)) return x; | 852 if (%_IsSmi(x)) return x; |
770 return %NumberToInteger(ToNumber(x)); | 853 return %NumberToInteger(ToNumber(x)); |
771 } | 854 } |
772 | 855 |
773 | 856 |
774 // ES6, draft 08-24-14, section 7.1.15 | 857 // ES6, draft 08-24-14, section 7.1.15 |
775 function ToLength(arg) { | 858 function ToLength(arg) { |
776 arg = ToInteger(arg); | 859 arg = ToInteger(arg); |
777 if (arg < 0) return 0; | 860 if (arg < 0) return 0; |
778 return arg < $Number.MAX_SAFE_INTEGER ? arg : $Number.MAX_SAFE_INTEGER; | 861 return arg < GlobalNumber.MAX_SAFE_INTEGER ? arg |
| 862 : GlobalNumber.MAX_SAFE_INTEGER; |
779 } | 863 } |
780 | 864 |
781 | 865 |
782 // ECMA-262, section 9.6, page 34. | 866 // ECMA-262, section 9.6, page 34. |
783 function ToUint32(x) { | 867 function ToUint32(x) { |
784 if (%_IsSmi(x) && x >= 0) return x; | 868 if (%_IsSmi(x) && x >= 0) return x; |
785 return %NumberToJSUint32(ToNumber(x)); | 869 return %NumberToJSUint32(ToNumber(x)); |
786 } | 870 } |
787 | 871 |
788 | 872 |
(...skipping 10 matching lines...) Expand all Loading... |
799 if (IS_NUMBER(x)) { | 883 if (IS_NUMBER(x)) { |
800 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; | 884 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; |
801 // x is +0 and y is -0 or vice versa. | 885 // x is +0 and y is -0 or vice versa. |
802 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { | 886 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { |
803 return false; | 887 return false; |
804 } | 888 } |
805 } | 889 } |
806 return x === y; | 890 return x === y; |
807 } | 891 } |
808 | 892 |
| 893 |
809 // ES6, section 7.2.4 | 894 // ES6, section 7.2.4 |
810 function SameValueZero(x, y) { | 895 function SameValueZero(x, y) { |
811 if (typeof x != typeof y) return false; | 896 if (typeof x != typeof y) return false; |
812 if (IS_NUMBER(x)) { | 897 if (IS_NUMBER(x)) { |
813 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; | 898 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; |
814 } | 899 } |
815 return x === y; | 900 return x === y; |
816 } | 901 } |
817 | 902 |
818 | 903 |
(...skipping 20 matching lines...) Expand all Loading... |
839 return ToBoolean(spreadable); | 924 return ToBoolean(spreadable); |
840 } | 925 } |
841 | 926 |
842 | 927 |
843 // ECMA-262, section 8.6.2.6, page 28. | 928 // ECMA-262, section 8.6.2.6, page 28. |
844 function DefaultNumber(x) { | 929 function DefaultNumber(x) { |
845 if (!IS_SYMBOL_WRAPPER(x)) { | 930 if (!IS_SYMBOL_WRAPPER(x)) { |
846 var valueOf = x.valueOf; | 931 var valueOf = x.valueOf; |
847 if (IS_SPEC_FUNCTION(valueOf)) { | 932 if (IS_SPEC_FUNCTION(valueOf)) { |
848 var v = %_CallFunction(x, valueOf); | 933 var v = %_CallFunction(x, valueOf); |
849 if (%IsPrimitive(v)) return v; | 934 if (IsPrimitive(v)) return v; |
850 } | 935 } |
851 | 936 |
852 var toString = x.toString; | 937 var toString = x.toString; |
853 if (IS_SPEC_FUNCTION(toString)) { | 938 if (IS_SPEC_FUNCTION(toString)) { |
854 var s = %_CallFunction(x, toString); | 939 var s = %_CallFunction(x, toString); |
855 if (%IsPrimitive(s)) return s; | 940 if (IsPrimitive(s)) return s; |
856 } | 941 } |
857 } | 942 } |
858 throw %MakeTypeError(kCannotConvertToPrimitive); | 943 throw MakeTypeError(kCannotConvertToPrimitive); |
859 } | 944 } |
860 | 945 |
861 // ECMA-262, section 8.6.2.6, page 28. | 946 // ECMA-262, section 8.6.2.6, page 28. |
862 function DefaultString(x) { | 947 function DefaultString(x) { |
863 if (!IS_SYMBOL_WRAPPER(x)) { | 948 if (!IS_SYMBOL_WRAPPER(x)) { |
864 var toString = x.toString; | 949 var toString = x.toString; |
865 if (IS_SPEC_FUNCTION(toString)) { | 950 if (IS_SPEC_FUNCTION(toString)) { |
866 var s = %_CallFunction(x, toString); | 951 var s = %_CallFunction(x, toString); |
867 if (%IsPrimitive(s)) return s; | 952 if (IsPrimitive(s)) return s; |
868 } | 953 } |
869 | 954 |
870 var valueOf = x.valueOf; | 955 var valueOf = x.valueOf; |
871 if (IS_SPEC_FUNCTION(valueOf)) { | 956 if (IS_SPEC_FUNCTION(valueOf)) { |
872 var v = %_CallFunction(x, valueOf); | 957 var v = %_CallFunction(x, valueOf); |
873 if (%IsPrimitive(v)) return v; | 958 if (IsPrimitive(v)) return v; |
874 } | 959 } |
875 } | 960 } |
876 throw %MakeTypeError(kCannotConvertToPrimitive); | 961 throw MakeTypeError(kCannotConvertToPrimitive); |
877 } | 962 } |
878 | 963 |
879 function ToPositiveInteger(x, rangeErrorIndex) { | 964 function ToPositiveInteger(x, rangeErrorIndex) { |
880 var i = TO_INTEGER_MAP_MINUS_ZERO(x); | 965 var i = TO_INTEGER_MAP_MINUS_ZERO(x); |
881 if (i < 0) throw MakeRangeError(rangeErrorIndex); | 966 if (i < 0) throw MakeRangeError(rangeErrorIndex); |
882 return i; | 967 return i; |
883 } | 968 } |
884 | 969 |
| 970 //---------------------------------------------------------------------------- |
885 | 971 |
886 // NOTE: Setting the prototype for Array must take place as early as | 972 // NOTE: Setting the prototype for Array must take place as early as |
887 // possible due to code generation for array literals. When | 973 // possible due to code generation for array literals. When |
888 // generating code for a array literal a boilerplate array is created | 974 // generating code for a array literal a boilerplate array is created |
889 // that is cloned when running the code. It is essential that the | 975 // that is cloned when running the code. It is essential that the |
890 // boilerplate gets the right prototype. | 976 // boilerplate gets the right prototype. |
891 %FunctionSetPrototype($Array, new $Array(0)); | 977 %FunctionSetPrototype(GlobalArray, new GlobalArray(0)); |
892 | 978 |
| 979 //---------------------------------------------------------------------------- |
893 | 980 |
894 /* ----------------------------------------------- | 981 $defaultNumber = DefaultNumber; |
895 - - - J a v a S c r i p t S t u b s - - - | 982 $defaultString = DefaultString; |
896 ----------------------------------------------- | 983 $NaN = %GetRootNaN(); |
897 */ | 984 $nonNumberToNumber = NonNumberToNumber; |
| 985 $nonStringToString = NonStringToString; |
| 986 $sameValue = SameValue; |
| 987 $sameValueZero = SameValueZero; |
| 988 $toBoolean = ToBoolean; |
| 989 $toInt32 = ToInt32; |
| 990 $toInteger = ToInteger; |
| 991 $toLength = ToLength; |
| 992 $toName = ToName; |
| 993 $toNumber = ToNumber; |
| 994 $toObject = ToObject; |
| 995 $toPositiveInteger = ToPositiveInteger; |
| 996 $toPrimitive = ToPrimitive; |
| 997 $toString = ToString; |
| 998 $toUint32 = ToUint32; |
898 | 999 |
899 function STRING_LENGTH_STUB(name) { | 1000 })(); |
900 var receiver = this; // implicit first parameter | |
901 return %_StringGetLength(%_JSValueGetValue(receiver)); | |
902 } | |
OLD | NEW |