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