Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / v8 / src / runtime.js
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
3 // found in the LICENSE file.
4
5 // This files contains runtime support implemented in JavaScript.
6
7 // CAUTION: Some of the functions specified in this file are called
8 // directly from compiled code. These are the functions with names in
9 // ALL CAPS. The compiled code passes the first argument in 'this' and
10 // it does not push the function onto the stack. This means that you
11 // cannot use contexts in all these functions.
12
13
14 /* -----------------------------------
15    - - -   C o m p a r i s o n   - - -
16    -----------------------------------
17 */
18
19 // The following declarations are shared with other native JS files.
20 // They are all declared at this one spot to avoid redeclaration errors.
21 var $Object = global.Object;
22 var $Array = global.Array;
23 var $String = global.String;
24 var $Number = global.Number;
25 var $Function = global.Function;
26 var $Boolean = global.Boolean;
27 var $NaN = %GetRootNaN();
28
29 // ECMA-262 Section 11.9.3.
30 function EQUALS(y) {
31   if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
32   var x = this;
33
34   while (true) {
35     if (IS_NUMBER(x)) {
36       while (true) {
37         if (IS_NUMBER(y)) return %NumberEquals(x, y);
38         if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
39         if (IS_SYMBOL(y)) return 1;  // not equal
40         if (!IS_SPEC_OBJECT(y)) {
41           // String or boolean.
42           return %NumberEquals(x, %ToNumber(y));
43         }
44         y = %ToPrimitive(y, NO_HINT);
45       }
46     } else if (IS_STRING(x)) {
47       while (true) {
48         if (IS_STRING(y)) return %StringEquals(x, y);
49         if (IS_SYMBOL(y)) return 1;  // not equal
50         if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
51         if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
52         if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
53         if (IsFloat32x4(y) || IsFloat64x2(y) || IsInt32x4(y)) {
54           return %StringEquals(x, %ToString(y));
55         }
56         y = %ToPrimitive(y, NO_HINT);
57       }
58     } else if (IS_SYMBOL(x)) {
59       if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
60       return 1; // not equal
61     } else if (IsFloat32x4(x)) {
62       while (true) {
63         if (IsFloat32x4(y) || IsInt32x4(y)) {
64           return (x.x == y.x && x.y == y.y && x.z == y.z && x.w == y.w) ? 0 : 1;
65         }
66         if (IS_STRING(y)) return %StringEquals(%ToString(x), y);
67         if (IS_NUMBER(y)) return 1;  // not equal
68         if (IS_SYMBOL(y)) return 1;  // not equal
69         if (IS_BOOLEAN(y)) return y ? 0 : 1;
70         if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
71         y = %ToPrimitive(y, NO_HINT);
72       }
73     } else if (IsFloat64x2(x)) {
74       while (true) {
75         if (IsFloat64x2(y)) {
76           return (x.x == y.x && x.y == y.y) ? 0 : 1;
77         }
78         if (IS_STRING(y)) return %StringEquals(%ToString(x), y);
79         if (IS_NUMBER(y)) return 1;  // not equal
80         if (IS_SYMBOL(y)) return 1;  // not equal
81         if (IS_BOOLEAN(y)) return y ? 0 : 1;
82         if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
83         y = %ToPrimitive(y, NO_HINT);
84       }
85     } else if (IsInt32x4(x)) {
86       while (true) {
87         if (IsFloat32x4(y) || IsInt32x4(y)) {
88           return (x.x == y.x && x.y == y.y && x.z == y.z && x.w == y.w) ? 0 : 1;
89         }
90         if (IS_STRING(y)) return %StringEquals(%ToString(x), y);
91         if (IS_NUMBER(y)) return 1;  // not equal
92         if (IS_SYMBOL(y)) return 1;  // not equal
93         if (IS_BOOLEAN(y)) return y ? 0 : 1;
94         if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
95         y = %ToPrimitive(y, NO_HINT);
96       }
97     } else if (IS_BOOLEAN(x)) {
98       if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
99       if (IS_NULL_OR_UNDEFINED(y)) return 1;
100       if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
101       if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
102       if (IS_SYMBOL(y)) return 1;  // not equal
103       if (IsFloat32x4(y) || IsFloat64x2(y) || IsInt32x4(y)) return x ? 0 : 1;
104       // y is object.
105       x = %ToNumber(x);
106       y = %ToPrimitive(y, NO_HINT);
107     } else if (IS_NULL_OR_UNDEFINED(x)) {
108       return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
109     } else {
110       // x is an object.
111       if (IS_SPEC_OBJECT(y)) {
112         return %_ObjectEquals(x, y) ? 0 : 1;
113       }
114       if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
115       if (IS_SYMBOL(y)) return 1;  // not equal
116       if (IS_BOOLEAN(y)) y = %ToNumber(y);
117       x = %ToPrimitive(x, NO_HINT);
118     }
119   }
120 }
121
122 // ECMA-262, section 11.9.4, page 56.
123 function STRICT_EQUALS(x) {
124   if (IS_STRING(this)) {
125     if (!IS_STRING(x)) return 1;  // not equal
126     return %StringEquals(this, x);
127   }
128
129   if (IS_NUMBER(this)) {
130     if (!IS_NUMBER(x)) return 1;  // not equal
131     return %NumberEquals(this, x);
132   }
133
134   if (IsFloat32x4(this)) {
135     if (!IsFloat32x4(x)) return 1;  // not equal
136     return (this.x == x.x && this.y == x.y &&
137             this.z == x.z && this.w == x.w) ? 0 : 1;
138   }
139
140   if (IsFloat64x2(this)) {
141     if (!IsFloat64x2(x)) return 1;  // not equal
142     return (this.x == x.x && this.y == x.y) ? 0 : 1;
143   }
144
145   if (IsInt32x4(this)) {
146     if (!IsInt32x4(x)) return 1;  // not equal
147     return (this.x == x.x && this.y == x.y &&
148             this.z == x.z && this.w == x.w) ? 0 : 1;
149   }
150
151   // If anything else gets here, we just do simple identity check.
152   // Objects (including functions), null, undefined and booleans were
153   // checked in the CompareStub, so there should be nothing left.
154   return %_ObjectEquals(this, x) ? 0 : 1;
155 }
156
157
158 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
159 // the result when either (or both) the operands are NaN.
160 function COMPARE(x, ncr) {
161   var left;
162   var right;
163   // Fast cases for string, numbers and undefined compares.
164   if (IS_STRING(this)) {
165     if (IS_STRING(x)) return %_StringCompare(this, x);
166     if (IS_UNDEFINED(x)) return ncr;
167     left = this;
168   } else if (IS_NUMBER(this)) {
169     if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
170     if (IS_UNDEFINED(x)) return ncr;
171     left = this;
172   } else if (IS_UNDEFINED(this)) {
173     if (!IS_UNDEFINED(x)) {
174       %ToPrimitive(x, NUMBER_HINT);
175     }
176     return ncr;
177   } else if (IS_UNDEFINED(x)) {
178     %ToPrimitive(this, NUMBER_HINT);
179     return ncr;
180   } else {
181     left = %ToPrimitive(this, NUMBER_HINT);
182   }
183
184   right = %ToPrimitive(x, NUMBER_HINT);
185   if (IS_STRING(left) && IS_STRING(right)) {
186     return %_StringCompare(left, right);
187   } else if ((IsFloat32x4(left)  || IsInt32x4(left)) &&
188              (IsFloat32x4(right) || IsInt32x4(right))) {
189     if ((left.x == right.x) && (left.y == right.y) &&
190         (left.z == right.z) && (left.w == right.w)) {
191       return 0;  // equal
192     }
193     if ((left.x < right.x) && (left.y < right.y) &&
194         (left.z < right.z) && (left.w < right.w)) {
195       return -1;  // less
196     }
197     if ((left.x > right.x) && (left.y > right.y) &&
198         (left.z > right.z) && (left.w > right.w)) {
199       return 1;  // great
200     }
201   } else if (IsFloat64x2(left) && IsFloat64x2(right)) {
202     if ((left.x == right.x) && (left.y == right.y)) {
203       return 0;  // equal
204     }
205     if ((left.x < right.x) && (left.y < right.y)) {
206       return -1;  // less
207     }
208     if ((left.x > right.x) && (left.y > right.y)) {
209       return 1;  // great
210     }
211   } else {
212     var left_number = %ToNumber(left);
213     var right_number = %ToNumber(right);
214     if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
215     return %NumberCompare(left_number, right_number, ncr);
216   }
217 }
218
219
220
221 /* -----------------------------------
222    - - -   A r i t h m e t i c   - - -
223    -----------------------------------
224 */
225
226 // ECMA-262, section 11.6.1, page 50.
227 function ADD(x) {
228   // Fast case: Check for number operands and do the addition.
229   if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
230   if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
231
232   // Default implementation.
233   var a = %ToPrimitive(this, NO_HINT);
234   var b = %ToPrimitive(x, NO_HINT);
235
236   if (IS_STRING(a)) {
237     return %_StringAdd(a, %ToString(b));
238   } else if (IS_STRING(b)) {
239     return %_StringAdd(%NonStringToString(a), b);
240   } else {
241     return %NumberAdd(%ToNumber(a), %ToNumber(b));
242   }
243 }
244
245
246 // Left operand (this) is already a string.
247 function STRING_ADD_LEFT(y) {
248   if (!IS_STRING(y)) {
249     if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
250       y = %_ValueOf(y);
251     } else {
252       y = IS_NUMBER(y)
253           ? %_NumberToString(y)
254           : %ToString(%ToPrimitive(y, NO_HINT));
255     }
256   }
257   return %_StringAdd(this, y);
258 }
259
260
261 // Right operand (y) is already a string.
262 function STRING_ADD_RIGHT(y) {
263   var x = this;
264   if (!IS_STRING(x)) {
265     if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
266       x = %_ValueOf(x);
267     } else {
268       x = IS_NUMBER(x)
269           ? %_NumberToString(x)
270           : %ToString(%ToPrimitive(x, NO_HINT));
271     }
272   }
273   return %_StringAdd(x, y);
274 }
275
276
277 // ECMA-262, section 11.6.2, page 50.
278 function SUB(y) {
279   var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
280   if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
281   return %NumberSub(x, y);
282 }
283
284
285 // ECMA-262, section 11.5.1, page 48.
286 function MUL(y) {
287   var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
288   if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
289   return %NumberMul(x, y);
290 }
291
292
293 // ECMA-262, section 11.5.2, page 49.
294 function DIV(y) {
295   var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
296   if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
297   return %NumberDiv(x, y);
298 }
299
300
301 // ECMA-262, section 11.5.3, page 49.
302 function MOD(y) {
303   var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
304   if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
305   return %NumberMod(x, y);
306 }
307
308
309
310 /* -------------------------------------------
311    - - -   B i t   o p e r a t i o n s   - - -
312    -------------------------------------------
313 */
314
315 // ECMA-262, section 11.10, page 57.
316 function BIT_OR(y) {
317   var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
318   if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
319   return %NumberOr(x, y);
320 }
321
322
323 // ECMA-262, section 11.10, page 57.
324 function BIT_AND(y) {
325   var x;
326   if (IS_NUMBER(this)) {
327     x = this;
328     if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
329   } else {
330     x = %NonNumberToNumber(this);
331     // Make sure to convert the right operand to a number before
332     // bailing out in the fast case, but after converting the
333     // left operand. This ensures that valueOf methods on the right
334     // operand are always executed.
335     if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
336     // Optimize for the case where we end up AND'ing a value
337     // that doesn't convert to a number. This is common in
338     // certain benchmarks.
339     if (NUMBER_IS_NAN(x)) return 0;
340   }
341   return %NumberAnd(x, y);
342 }
343
344
345 // ECMA-262, section 11.10, page 57.
346 function BIT_XOR(y) {
347   var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
348   if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
349   return %NumberXor(x, y);
350 }
351
352
353 // ECMA-262, section 11.7.1, page 51.
354 function SHL(y) {
355   var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
356   if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
357   return %NumberShl(x, y);
358 }
359
360
361 // ECMA-262, section 11.7.2, page 51.
362 function SAR(y) {
363   var x;
364   if (IS_NUMBER(this)) {
365     x = this;
366     if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
367   } else {
368     x = %NonNumberToNumber(this);
369     // Make sure to convert the right operand to a number before
370     // bailing out in the fast case, but after converting the
371     // left operand. This ensures that valueOf methods on the right
372     // operand are always executed.
373     if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
374     // Optimize for the case where we end up shifting a value
375     // that doesn't convert to a number. This is common in
376     // certain benchmarks.
377     if (NUMBER_IS_NAN(x)) return 0;
378   }
379   return %NumberSar(x, y);
380 }
381
382
383 // ECMA-262, section 11.7.3, page 52.
384 function SHR(y) {
385   var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
386   if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
387   return %NumberShr(x, y);
388 }
389
390
391
392 /* -----------------------------
393    - - -   H e l p e r s   - - -
394    -----------------------------
395 */
396
397 // ECMA-262, section 11.4.1, page 46.
398 function DELETE(key, strict) {
399   return %DeleteProperty(%ToObject(this), %ToName(key), strict);
400 }
401
402
403 // ECMA-262, section 11.8.7, page 54.
404 function IN(x) {
405   if (!IS_SPEC_OBJECT(x)) {
406     throw %MakeTypeError('invalid_in_operator_use', [this, x]);
407   }
408   return %_IsNonNegativeSmi(this) ?
409     %HasElement(x, this) : %HasProperty(x, %ToName(this));
410 }
411
412
413 // ECMA-262, section 11.8.6, page 54. To make the implementation more
414 // efficient, the return value should be zero if the 'this' is an
415 // instance of F, and non-zero if not. This makes it possible to avoid
416 // an expensive ToBoolean conversion in the generated code.
417 function INSTANCE_OF(F) {
418   var V = this;
419   if (!IS_SPEC_FUNCTION(F)) {
420     throw %MakeTypeError('instanceof_function_expected', [F]);
421   }
422
423   // If V is not an object, return false.
424   if (!IS_SPEC_OBJECT(V)) {
425     return 1;
426   }
427
428   // Check if function is bound, if so, get [[BoundFunction]] from it
429   // and use that instead of F.
430   var bindings = %BoundFunctionGetBindings(F);
431   if (bindings) {
432     F = bindings[kBoundFunctionIndex];  // Always a non-bound function.
433   }
434   // Get the prototype of F; if it is not an object, throw an error.
435   var O = F.prototype;
436   if (!IS_SPEC_OBJECT(O)) {
437     throw %MakeTypeError('instanceof_nonobject_proto', [O]);
438   }
439
440   // Return whether or not O is in the prototype chain of V.
441   return %IsInPrototypeChain(O, V) ? 0 : 1;
442 }
443
444
445 // Filter a given key against an object by checking if the object
446 // has a property with the given key; return the key as a string if
447 // it has. Otherwise returns 0 (smi). Used in for-in statements.
448 function FILTER_KEY(key) {
449   var string = %ToName(key);
450   if (%HasProperty(this, string)) return string;
451   return 0;
452 }
453
454
455 function CALL_NON_FUNCTION() {
456   var delegate = %GetFunctionDelegate(this);
457   if (!IS_FUNCTION(delegate)) {
458     throw %MakeTypeError('called_non_callable', [typeof this]);
459   }
460   return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
461 }
462
463
464 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
465   var delegate = %GetConstructorDelegate(this);
466   if (!IS_FUNCTION(delegate)) {
467     throw %MakeTypeError('called_non_callable', [typeof this]);
468   }
469   return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
470 }
471
472
473 function CALL_FUNCTION_PROXY() {
474   var arity = %_ArgumentsLength() - 1;
475   var proxy = %_Arguments(arity);  // The proxy comes in as an additional arg.
476   var trap = %GetCallTrap(proxy);
477   return %Apply(trap, this, arguments, 0, arity);
478 }
479
480
481 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
482   var proxy = this;
483   var trap = %GetConstructTrap(proxy);
484   return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
485 }
486
487
488 function APPLY_PREPARE(args) {
489   var length;
490   // First check whether length is a positive Smi and args is an
491   // array. This is the fast case. If this fails, we do the slow case
492   // that takes care of more eventualities.
493   if (IS_ARRAY(args)) {
494     length = args.length;
495     if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
496         IS_SPEC_FUNCTION(this)) {
497       return length;
498     }
499   }
500
501   length = (args == null) ? 0 : %ToUint32(args.length);
502
503   // We can handle any number of apply arguments if the stack is
504   // big enough, but sanity check the value to avoid overflow when
505   // multiplying with pointer size.
506   if (length > 0x800000) {
507     throw %MakeRangeError('stack_overflow', []);
508   }
509
510   if (!IS_SPEC_FUNCTION(this)) {
511     throw %MakeTypeError('apply_non_function',
512                          [ %ToString(this), typeof this ]);
513   }
514
515   // Make sure the arguments list has the right type.
516   if (args != null && !IS_SPEC_OBJECT(args)) {
517     throw %MakeTypeError('apply_wrong_args', []);
518   }
519
520   // Return the length which is the number of arguments to copy to the
521   // stack. It is guaranteed to be a small integer at this point.
522   return length;
523 }
524
525
526 function STACK_OVERFLOW(length) {
527   throw %MakeRangeError('stack_overflow', []);
528 }
529
530
531 // Convert the receiver to an object - forward to ToObject.
532 function TO_OBJECT() {
533   return %ToObject(this);
534 }
535
536
537 // Convert the receiver to a number - forward to ToNumber.
538 function TO_NUMBER() {
539   return %ToNumber(this);
540 }
541
542
543 // Convert the receiver to a string - forward to ToString.
544 function TO_STRING() {
545   return %ToString(this);
546 }
547
548
549 /* -------------------------------------
550    - - -   C o n v e r s i o n s   - - -
551    -------------------------------------
552 */
553
554 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
555 // (1) for number hint, and (2) for string hint.
556 function ToPrimitive(x, hint) {
557   // Fast case check.
558   if (IS_STRING(x)) return x;
559   // Normal behavior.
560   if (!IS_SPEC_OBJECT(x)) return x;
561   if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
562   if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
563   return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
564 }
565
566
567 // ECMA-262, section 9.2, page 30
568 function ToBoolean(x) {
569   if (IS_BOOLEAN(x)) return x;
570   if (IS_STRING(x)) return x.length != 0;
571   if (x == null) return false;
572   if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
573   return true;
574 }
575
576
577 // ECMA-262, section 9.3, page 31.
578 function ToNumber(x) {
579   if (IS_NUMBER(x)) return x;
580   if (IS_STRING(x)) {
581     return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
582                                     : %StringToNumber(x);
583   }
584   if (IS_BOOLEAN(x)) return x ? 1 : 0;
585   if (IS_UNDEFINED(x)) return NAN;
586   if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
587   if (IsFloat32x4(x)) return NAN;
588   if (IsFloat64x2(x)) return NAN;
589   if (IsInt32x4(x)) return NAN;
590   return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
591 }
592
593 function NonNumberToNumber(x) {
594   if (IS_STRING(x)) {
595     return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
596                                     : %StringToNumber(x);
597   }
598   if (IS_BOOLEAN(x)) return x ? 1 : 0;
599   if (IS_UNDEFINED(x)) return NAN;
600   if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
601   if (IsFloat32x4(x)) return NAN;
602   if (IsFloat64x2(x)) return NAN;
603   if (IsInt32x4(x)) return NAN;
604   return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
605 }
606
607
608 // ECMA-262, section 9.8, page 35.
609 function ToString(x) {
610   if (IS_STRING(x)) return x;
611   if (IS_NUMBER(x)) return %_NumberToString(x);
612   if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
613   if (IS_UNDEFINED(x)) return 'undefined';
614   if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
615   return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
616 }
617
618 function NonStringToString(x) {
619   if (IS_NUMBER(x)) return %_NumberToString(x);
620   if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
621   if (IS_UNDEFINED(x)) return 'undefined';
622   if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
623   return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
624 }
625
626
627 // ES6 symbols
628 function ToName(x) {
629   return IS_SYMBOL(x) ? x : %ToString(x);
630 }
631
632
633 // ECMA-262, section 9.9, page 36.
634 function ToObject(x) {
635   if (IS_STRING(x)) return new $String(x);
636   if (IS_NUMBER(x)) return new $Number(x);
637   if (IS_BOOLEAN(x)) return new $Boolean(x);
638   if (IsFloat32x4(x)) return new $Float32x4(x.x, x.y, x.z, x.w);
639   if (IsFloat64x2(x)) return new $Float64x2(x.x, x.y);
640   if (IsInt32x4(x)) return new $Int32x4(x.x, x.y, x.z, x.w);
641   if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
642   if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
643     throw %MakeTypeError('undefined_or_null_to_object', []);
644   }
645   return x;
646 }
647
648
649 // ECMA-262, section 9.4, page 34.
650 function ToInteger(x) {
651   if (%_IsSmi(x)) return x;
652   return %NumberToInteger(ToNumber(x));
653 }
654
655
656 // ECMA-262, section 9.6, page 34.
657 function ToUint32(x) {
658   if (%_IsSmi(x) && x >= 0) return x;
659   return %NumberToJSUint32(ToNumber(x));
660 }
661
662
663 // ECMA-262, section 9.5, page 34
664 function ToInt32(x) {
665   if (%_IsSmi(x)) return x;
666   return %NumberToJSInt32(ToNumber(x));
667 }
668
669
670 // ES5, section 9.12
671 function SameValue(x, y) {
672   if (typeof x != typeof y) return false;
673   if (IS_NUMBER(x)) {
674     if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
675     // x is +0 and y is -0 or vice versa.
676     if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
677       return false;
678     }
679   }
680   return x === y;
681 }
682
683
684 /* ---------------------------------
685    - - -   U t i l i t i e s   - - -
686    ---------------------------------
687 */
688
689 // Returns if the given x is a primitive value - not an object or a
690 // function.
691 function IsPrimitive(x) {
692   // Even though the type of null is "object", null is still
693   // considered a primitive value. IS_SPEC_OBJECT handles this correctly
694   // (i.e., it will return false if x is null).
695   return !IS_SPEC_OBJECT(x);
696 }
697
698
699 // ECMA-262, section 8.6.2.6, page 28.
700 function DefaultNumber(x) {
701   if (!IS_SYMBOL_WRAPPER(x)) {
702     var valueOf = x.valueOf;
703     if (IS_SPEC_FUNCTION(valueOf)) {
704       var v = %_CallFunction(x, valueOf);
705       if (%IsPrimitive(v)) return v;
706     }
707
708     var toString = x.toString;
709     if (IS_SPEC_FUNCTION(toString)) {
710       var s = %_CallFunction(x, toString);
711       if (%IsPrimitive(s)) return s;
712     }
713   }
714   throw %MakeTypeError('cannot_convert_to_primitive', []);
715 }
716
717 // ECMA-262, section 8.6.2.6, page 28.
718 function DefaultString(x) {
719   if (!IS_SYMBOL_WRAPPER(x)) {
720     var toString = x.toString;
721     if (IS_SPEC_FUNCTION(toString)) {
722       var s = %_CallFunction(x, toString);
723       if (%IsPrimitive(s)) return s;
724     }
725
726     var valueOf = x.valueOf;
727     if (IS_SPEC_FUNCTION(valueOf)) {
728       var v = %_CallFunction(x, valueOf);
729       if (%IsPrimitive(v)) return v;
730     }
731   }
732   throw %MakeTypeError('cannot_convert_to_primitive', []);
733 }
734
735 function ToPositiveInteger(x, rangeErrorName) {
736   var i = TO_INTEGER(x);
737   if (i < 0) throw MakeRangeError(rangeErrorName);
738   return i;
739 }
740
741
742 // NOTE: Setting the prototype for Array must take place as early as
743 // possible due to code generation for array literals.  When
744 // generating code for a array literal a boilerplate array is created
745 // that is cloned when running the code.  It is essential that the
746 // boilerplate gets the right prototype.
747 %FunctionSetPrototype($Array, new $Array(0));