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