7d82dfa8463fac3305ca98f42dd9a5545444a40c
[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, language_mode) {
316   return %DeleteProperty(%ToObject(this), %ToName(key), language_mode);
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     var callsite = %RenderCallSite();
381     if (callsite == "") callsite = typeof this;
382     throw %MakeTypeError('called_non_callable', [callsite]);
383   }
384   return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
385 }
386
387
388 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
389   var delegate = %GetConstructorDelegate(this);
390   if (!IS_FUNCTION(delegate)) {
391     var callsite = %RenderCallSite();
392     if (callsite == "") callsite = typeof this;
393     throw %MakeTypeError('called_non_callable', [callsite]);
394   }
395   return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
396 }
397
398
399 function CALL_FUNCTION_PROXY() {
400   var arity = %_ArgumentsLength() - 1;
401   var proxy = %_Arguments(arity);  // The proxy comes in as an additional arg.
402   var trap = %GetCallTrap(proxy);
403   return %Apply(trap, this, arguments, 0, arity);
404 }
405
406
407 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
408   var proxy = this;
409   var trap = %GetConstructTrap(proxy);
410   return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
411 }
412
413
414 function APPLY_PREPARE(args) {
415   var length;
416   // First check whether length is a positive Smi and args is an
417   // array. This is the fast case. If this fails, we do the slow case
418   // that takes care of more eventualities.
419   if (IS_ARRAY(args)) {
420     length = args.length;
421     if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
422         IS_SPEC_FUNCTION(this)) {
423       return length;
424     }
425   }
426
427   length = (args == null) ? 0 : %ToUint32(args.length);
428
429   // We can handle any number of apply arguments if the stack is
430   // big enough, but sanity check the value to avoid overflow when
431   // multiplying with pointer size.
432   if (length > 0x800000) {
433     throw %MakeRangeError('stack_overflow', []);
434   }
435
436   if (!IS_SPEC_FUNCTION(this)) {
437     throw %MakeTypeError('apply_non_function',
438                          [ %ToString(this), typeof this ]);
439   }
440
441   // Make sure the arguments list has the right type.
442   if (args != null && !IS_SPEC_OBJECT(args)) {
443     throw %MakeTypeError('apply_wrong_args', []);
444   }
445
446   // Return the length which is the number of arguments to copy to the
447   // stack. It is guaranteed to be a small integer at this point.
448   return length;
449 }
450
451
452 function STACK_OVERFLOW(length) {
453   throw %MakeRangeError('stack_overflow', []);
454 }
455
456
457 // Convert the receiver to an object - forward to ToObject.
458 function TO_OBJECT() {
459   return %ToObject(this);
460 }
461
462
463 // Convert the receiver to a number - forward to ToNumber.
464 function TO_NUMBER() {
465   return %ToNumber(this);
466 }
467
468
469 // Convert the receiver to a string - forward to ToString.
470 function TO_STRING() {
471   return %ToString(this);
472 }
473
474
475 // Convert the receiver to a string or symbol - forward to ToName.
476 function TO_NAME() {
477   return %ToName(this);
478 }
479
480
481 /* -------------------------------------
482    - - -   C o n v e r s i o n s   - - -
483    -------------------------------------
484 */
485
486 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
487 // (1) for number hint, and (2) for string hint.
488 function ToPrimitive(x, hint) {
489   // Fast case check.
490   if (IS_STRING(x)) return x;
491   // Normal behavior.
492   if (!IS_SPEC_OBJECT(x)) return x;
493   if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
494   if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
495   return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
496 }
497
498
499 // ECMA-262, section 9.2, page 30
500 function ToBoolean(x) {
501   if (IS_BOOLEAN(x)) return x;
502   if (IS_STRING(x)) return x.length != 0;
503   if (x == null) return false;
504   if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
505   return true;
506 }
507
508
509 // ECMA-262, section 9.3, page 31.
510 function ToNumber(x) {
511   if (IS_NUMBER(x)) return x;
512   if (IS_STRING(x)) {
513     return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
514                                     : %StringToNumber(x);
515   }
516   if (IS_BOOLEAN(x)) return x ? 1 : 0;
517   if (IS_UNDEFINED(x)) return NAN;
518   if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
519   return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
520 }
521
522 function NonNumberToNumber(x) {
523   if (IS_STRING(x)) {
524     return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
525                                     : %StringToNumber(x);
526   }
527   if (IS_BOOLEAN(x)) return x ? 1 : 0;
528   if (IS_UNDEFINED(x)) return NAN;
529   if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
530   return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
531 }
532
533
534 // ECMA-262, section 9.8, page 35.
535 function ToString(x) {
536   if (IS_STRING(x)) return x;
537   if (IS_NUMBER(x)) return %_NumberToString(x);
538   if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
539   if (IS_UNDEFINED(x)) return 'undefined';
540   if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
541   return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
542 }
543
544 function NonStringToString(x) {
545   if (IS_NUMBER(x)) return %_NumberToString(x);
546   if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
547   if (IS_UNDEFINED(x)) return 'undefined';
548   if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
549   return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
550 }
551
552
553 // ES6 symbols
554 function ToName(x) {
555   return IS_SYMBOL(x) ? x : %ToString(x);
556 }
557
558
559 // ECMA-262, section 9.9, page 36.
560 function ToObject(x) {
561   if (IS_STRING(x)) return new $String(x);
562   if (IS_NUMBER(x)) return new $Number(x);
563   if (IS_BOOLEAN(x)) return new $Boolean(x);
564   if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
565   if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
566     throw %MakeTypeError('undefined_or_null_to_object', []);
567   }
568   return x;
569 }
570
571
572 // ECMA-262, section 9.4, page 34.
573 function ToInteger(x) {
574   if (%_IsSmi(x)) return x;
575   return %NumberToInteger(ToNumber(x));
576 }
577
578
579 // ES6, draft 08-24-14, section 7.1.15
580 function ToLength(arg) {
581   arg = ToInteger(arg);
582   if (arg < 0) return 0;
583   return arg < $Number.MAX_SAFE_INTEGER ? arg : $Number.MAX_SAFE_INTEGER;
584 }
585
586
587 // ECMA-262, section 9.6, page 34.
588 function ToUint32(x) {
589   if (%_IsSmi(x) && x >= 0) return x;
590   return %NumberToJSUint32(ToNumber(x));
591 }
592
593
594 // ECMA-262, section 9.5, page 34
595 function ToInt32(x) {
596   if (%_IsSmi(x)) return x;
597   return %NumberToJSInt32(ToNumber(x));
598 }
599
600
601 // ES5, section 9.12
602 function SameValue(x, y) {
603   if (typeof x != typeof y) return false;
604   if (IS_NUMBER(x)) {
605     if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
606     // x is +0 and y is -0 or vice versa.
607     if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
608       return false;
609     }
610   }
611   return x === y;
612 }
613
614 // ES6, section 7.2.4
615 function SameValueZero(x, y) {
616   if (typeof x != typeof y) return false;
617   if (IS_NUMBER(x)) {
618     if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
619   }
620   return x === y;
621 }
622
623
624 /* ---------------------------------
625    - - -   U t i l i t i e s   - - -
626    ---------------------------------
627 */
628
629 // Returns if the given x is a primitive value - not an object or a
630 // function.
631 function IsPrimitive(x) {
632   // Even though the type of null is "object", null is still
633   // considered a primitive value. IS_SPEC_OBJECT handles this correctly
634   // (i.e., it will return false if x is null).
635   return !IS_SPEC_OBJECT(x);
636 }
637
638
639 // ES6, draft 10-14-14, section 22.1.3.1.1
640 function IsConcatSpreadable(O) {
641   if (!IS_SPEC_OBJECT(O)) return false;
642   var spreadable = O[symbolIsConcatSpreadable];
643   if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O);
644   return ToBoolean(spreadable);
645 }
646
647
648 // ECMA-262, section 8.6.2.6, page 28.
649 function DefaultNumber(x) {
650   if (!IS_SYMBOL_WRAPPER(x)) {
651     var valueOf = x.valueOf;
652     if (IS_SPEC_FUNCTION(valueOf)) {
653       var v = %_CallFunction(x, valueOf);
654       if (%IsPrimitive(v)) return v;
655     }
656
657     var toString = x.toString;
658     if (IS_SPEC_FUNCTION(toString)) {
659       var s = %_CallFunction(x, toString);
660       if (%IsPrimitive(s)) return s;
661     }
662   }
663   throw %MakeTypeError('cannot_convert_to_primitive', []);
664 }
665
666 // ECMA-262, section 8.6.2.6, page 28.
667 function DefaultString(x) {
668   if (!IS_SYMBOL_WRAPPER(x)) {
669     var toString = x.toString;
670     if (IS_SPEC_FUNCTION(toString)) {
671       var s = %_CallFunction(x, toString);
672       if (%IsPrimitive(s)) return s;
673     }
674
675     var valueOf = x.valueOf;
676     if (IS_SPEC_FUNCTION(valueOf)) {
677       var v = %_CallFunction(x, valueOf);
678       if (%IsPrimitive(v)) return v;
679     }
680   }
681   throw %MakeTypeError('cannot_convert_to_primitive', []);
682 }
683
684 function ToPositiveInteger(x, rangeErrorName) {
685   var i = TO_INTEGER_MAP_MINUS_ZERO(x);
686   if (i < 0) throw MakeRangeError(rangeErrorName);
687   return i;
688 }
689
690
691 // NOTE: Setting the prototype for Array must take place as early as
692 // possible due to code generation for array literals.  When
693 // generating code for a array literal a boilerplate array is created
694 // that is cloned when running the code.  It is essential that the
695 // boilerplate gets the right prototype.
696 %FunctionSetPrototype($Array, new $Array(0));