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