deps: update v8 to 4.3.61.21
[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 < kSafeArgumentsLength &&
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 > kSafeArgumentsLength) {
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 REFLECT_APPLY_PREPARE(args) {
453   var length;
454   // First check whether length is a positive Smi and args is an
455   // array. This is the fast case. If this fails, we do the slow case
456   // that takes care of more eventualities.
457   if (IS_ARRAY(args)) {
458     length = args.length;
459     if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
460         IS_SPEC_FUNCTION(this)) {
461       return length;
462     }
463   }
464
465   if (!IS_SPEC_FUNCTION(this)) {
466     throw %MakeTypeError('called_non_callable', [ %ToString(this) ]);
467   }
468
469   if (!IS_SPEC_OBJECT(args)) {
470     throw %MakeTypeError('reflect_apply_wrong_args', [ ]);
471   }
472
473   length = %ToLength(args.length);
474
475   // We can handle any number of apply arguments if the stack is
476   // big enough, but sanity check the value to avoid overflow when
477   // multiplying with pointer size.
478   if (length > kSafeArgumentsLength) {
479     throw %MakeRangeError('stack_overflow', []);
480   }
481
482   // Return the length which is the number of arguments to copy to the
483   // stack. It is guaranteed to be a small integer at this point.
484   return length;
485 }
486
487
488 function REFLECT_CONSTRUCT_PREPARE(args, newTarget) {
489   var length;
490   var ctorOk = IS_SPEC_FUNCTION(this) && %IsConstructor(this);
491   var newTargetOk = IS_SPEC_FUNCTION(newTarget) && %IsConstructor(newTarget);
492
493   // First check whether length is a positive Smi and args is an
494   // array. This is the fast case. If this fails, we do the slow case
495   // that takes care of more eventualities.
496   if (IS_ARRAY(args)) {
497     length = args.length;
498     if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
499         ctorOk && newTargetOk) {
500       return length;
501     }
502   }
503
504   if (!ctorOk) {
505     if (!IS_SPEC_FUNCTION(this)) {
506       throw %MakeTypeError('called_non_callable', [ %ToString(this) ]);
507     } else {
508       throw %MakeTypeError('not_constructor', [ %ToString(this) ]);
509     }
510   }
511
512   if (!newTargetOk) {
513     if (!IS_SPEC_FUNCTION(newTarget)) {
514       throw %MakeTypeError('called_non_callable', [ %ToString(newTarget) ]);
515     } else {
516       throw %MakeTypeError('not_constructor', [ %ToString(newTarget) ]);
517     }
518   }
519
520   if (!IS_SPEC_OBJECT(args)) {
521     throw %MakeTypeError('reflect_construct_wrong_args', [ ]);
522   }
523
524   length = %ToLength(args.length);
525
526   // We can handle any number of apply arguments if the stack is
527   // big enough, but sanity check the value to avoid overflow when
528   // multiplying with pointer size.
529   if (length > kSafeArgumentsLength) {
530     throw %MakeRangeError('stack_overflow', []);
531   }
532
533   // Return the length which is the number of arguments to copy to the
534   // stack. It is guaranteed to be a small integer at this point.
535   return length;
536 }
537
538
539 function STACK_OVERFLOW(length) {
540   throw %MakeRangeError('stack_overflow', []);
541 }
542
543
544 // Convert the receiver to an object - forward to ToObject.
545 function TO_OBJECT() {
546   return %ToObject(this);
547 }
548
549
550 // Convert the receiver to a number - forward to ToNumber.
551 function TO_NUMBER() {
552   return %ToNumber(this);
553 }
554
555
556 // Convert the receiver to a string - forward to ToString.
557 function TO_STRING() {
558   return %ToString(this);
559 }
560
561
562 // Convert the receiver to a string or symbol - forward to ToName.
563 function TO_NAME() {
564   return %ToName(this);
565 }
566
567
568 /* -------------------------------------
569    - - -   C o n v e r s i o n s   - - -
570    -------------------------------------
571 */
572
573 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
574 // (1) for number hint, and (2) for string hint.
575 function ToPrimitive(x, hint) {
576   // Fast case check.
577   if (IS_STRING(x)) return x;
578   // Normal behavior.
579   if (!IS_SPEC_OBJECT(x)) return x;
580   if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
581   if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
582   return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
583 }
584
585
586 // ECMA-262, section 9.2, page 30
587 function ToBoolean(x) {
588   if (IS_BOOLEAN(x)) return x;
589   if (IS_STRING(x)) return x.length != 0;
590   if (x == null) return false;
591   if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
592   return true;
593 }
594
595
596 // ECMA-262, section 9.3, page 31.
597 function ToNumber(x) {
598   if (IS_NUMBER(x)) return x;
599   if (IS_STRING(x)) {
600     return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
601                                     : %StringToNumber(x);
602   }
603   if (IS_BOOLEAN(x)) return x ? 1 : 0;
604   if (IS_UNDEFINED(x)) return NAN;
605   if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
606   return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
607 }
608
609 function NonNumberToNumber(x) {
610   if (IS_STRING(x)) {
611     return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
612                                     : %StringToNumber(x);
613   }
614   if (IS_BOOLEAN(x)) return x ? 1 : 0;
615   if (IS_UNDEFINED(x)) return NAN;
616   if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
617   return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
618 }
619
620
621 // ECMA-262, section 9.8, page 35.
622 function ToString(x) {
623   if (IS_STRING(x)) return x;
624   if (IS_NUMBER(x)) return %_NumberToString(x);
625   if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
626   if (IS_UNDEFINED(x)) return 'undefined';
627   if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
628   return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
629 }
630
631 function NonStringToString(x) {
632   if (IS_NUMBER(x)) return %_NumberToString(x);
633   if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
634   if (IS_UNDEFINED(x)) return 'undefined';
635   if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
636   return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
637 }
638
639
640 // ES6 symbols
641 function ToName(x) {
642   return IS_SYMBOL(x) ? x : %ToString(x);
643 }
644
645
646 // ECMA-262, section 9.9, page 36.
647 function ToObject(x) {
648   if (IS_STRING(x)) return new $String(x);
649   if (IS_NUMBER(x)) return new $Number(x);
650   if (IS_BOOLEAN(x)) return new $Boolean(x);
651   if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
652   if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
653     throw %MakeTypeError('undefined_or_null_to_object', []);
654   }
655   return x;
656 }
657
658
659 // ECMA-262, section 9.4, page 34.
660 function ToInteger(x) {
661   if (%_IsSmi(x)) return x;
662   return %NumberToInteger(ToNumber(x));
663 }
664
665
666 // ES6, draft 08-24-14, section 7.1.15
667 function ToLength(arg) {
668   arg = ToInteger(arg);
669   if (arg < 0) return 0;
670   return arg < $Number.MAX_SAFE_INTEGER ? arg : $Number.MAX_SAFE_INTEGER;
671 }
672
673
674 // ECMA-262, section 9.6, page 34.
675 function ToUint32(x) {
676   if (%_IsSmi(x) && x >= 0) return x;
677   return %NumberToJSUint32(ToNumber(x));
678 }
679
680
681 // ECMA-262, section 9.5, page 34
682 function ToInt32(x) {
683   if (%_IsSmi(x)) return x;
684   return %NumberToJSInt32(ToNumber(x));
685 }
686
687
688 // ES5, section 9.12
689 function SameValue(x, y) {
690   if (typeof x != typeof y) return false;
691   if (IS_NUMBER(x)) {
692     if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
693     // x is +0 and y is -0 or vice versa.
694     if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
695       return false;
696     }
697   }
698   return x === y;
699 }
700
701 // ES6, section 7.2.4
702 function SameValueZero(x, y) {
703   if (typeof x != typeof y) return false;
704   if (IS_NUMBER(x)) {
705     if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
706   }
707   return x === y;
708 }
709
710
711 /* ---------------------------------
712    - - -   U t i l i t i e s   - - -
713    ---------------------------------
714 */
715
716 // Returns if the given x is a primitive value - not an object or a
717 // function.
718 function IsPrimitive(x) {
719   // Even though the type of null is "object", null is still
720   // considered a primitive value. IS_SPEC_OBJECT handles this correctly
721   // (i.e., it will return false if x is null).
722   return !IS_SPEC_OBJECT(x);
723 }
724
725
726 // ES6, draft 10-14-14, section 22.1.3.1.1
727 function IsConcatSpreadable(O) {
728   if (!IS_SPEC_OBJECT(O)) return false;
729   var spreadable = O[symbolIsConcatSpreadable];
730   if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O);
731   return ToBoolean(spreadable);
732 }
733
734
735 // ECMA-262, section 8.6.2.6, page 28.
736 function DefaultNumber(x) {
737   if (!IS_SYMBOL_WRAPPER(x)) {
738     var valueOf = x.valueOf;
739     if (IS_SPEC_FUNCTION(valueOf)) {
740       var v = %_CallFunction(x, valueOf);
741       if (%IsPrimitive(v)) return v;
742     }
743
744     var toString = x.toString;
745     if (IS_SPEC_FUNCTION(toString)) {
746       var s = %_CallFunction(x, toString);
747       if (%IsPrimitive(s)) return s;
748     }
749   }
750   throw %MakeTypeError('cannot_convert_to_primitive', []);
751 }
752
753 // ECMA-262, section 8.6.2.6, page 28.
754 function DefaultString(x) {
755   if (!IS_SYMBOL_WRAPPER(x)) {
756     var toString = x.toString;
757     if (IS_SPEC_FUNCTION(toString)) {
758       var s = %_CallFunction(x, toString);
759       if (%IsPrimitive(s)) return s;
760     }
761
762     var valueOf = x.valueOf;
763     if (IS_SPEC_FUNCTION(valueOf)) {
764       var v = %_CallFunction(x, valueOf);
765       if (%IsPrimitive(v)) return v;
766     }
767   }
768   throw %MakeTypeError('cannot_convert_to_primitive', []);
769 }
770
771 function ToPositiveInteger(x, rangeErrorName) {
772   var i = TO_INTEGER_MAP_MINUS_ZERO(x);
773   if (i < 0) throw MakeRangeError(rangeErrorName);
774   return i;
775 }
776
777
778 // NOTE: Setting the prototype for Array must take place as early as
779 // possible due to code generation for array literals.  When
780 // generating code for a array literal a boilerplate array is created
781 // that is cloned when running the code.  It is essential that the
782 // boilerplate gets the right prototype.
783 %FunctionSetPrototype($Array, new $Array(0));
784
785
786 /* -----------------------------------------------
787    - - -   J a v a S c r i p t   S t u b s   - - -
788    -----------------------------------------------
789 */
790
791 function STRING_LENGTH_STUB(name) {
792   var receiver = this;  // implicit first parameter
793   return %_StringGetLength(%_JSValueGetValue(receiver));
794 }