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.
5 // This files contains runtime support implemented in JavaScript.
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'.
12 // The following declarations are shared with other native JS files.
13 // They are all declared at this one spot to avoid redeclaration errors.
16 var $nonNumberToNumber;
17 var $nonStringToString;
23 var $toPositiveInteger;
27 (function(global, utils) {
29 %CheckIsBootstrapping();
31 var GlobalArray = global.Array;
32 var GlobalBoolean = global.Boolean;
33 var GlobalString = global.String;
34 var GlobalNumber = global.Number;
35 var isConcatSpreadableSymbol =
36 utils.ImportNow("is_concat_spreadable_symbol");
38 // ----------------------------------------------------------------------------
40 /* -----------------------------------
41 - - - C o m p a r i s o n - - -
42 -----------------------------------
45 // ECMA-262 Section 11.9.3.
47 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
53 if (IS_NUMBER(y)) return %NumberEquals(x, y);
54 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
55 if (!IS_SPEC_OBJECT(y)) {
56 if (IS_SYMBOL(y) || IS_SIMD_VALUE(y)) return 1; // not equal
58 return %NumberEquals(x, %to_number_fun(y));
60 y = %to_primitive(y, NO_HINT);
62 } else if (IS_STRING(x)) {
64 if (IS_STRING(y)) return %StringEquals(x, y);
65 if (IS_NUMBER(y)) return %NumberEquals(%to_number_fun(x), y);
67 return %NumberEquals(%to_number_fun(x), %to_number_fun(y));
69 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
70 if (IS_SYMBOL(y) || IS_SIMD_VALUE(y)) return 1; // not equal
71 y = %to_primitive(y, NO_HINT);
73 } else if (IS_SYMBOL(x)) {
74 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
75 return 1; // not equal
76 } else if (IS_BOOLEAN(x)) {
77 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
78 if (IS_NULL_OR_UNDEFINED(y)) return 1;
79 if (IS_NUMBER(y)) return %NumberEquals(%to_number_fun(x), y);
81 return %NumberEquals(%to_number_fun(x), %to_number_fun(y));
83 if (IS_SYMBOL(y) || IS_SIMD_VALUE(y)) return 1; // not equal
85 x = %to_number_fun(x);
86 y = %to_primitive(y, NO_HINT);
87 } else if (IS_NULL_OR_UNDEFINED(x)) {
88 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
89 } else if (IS_SIMD_VALUE(x)) {
90 if (!IS_SIMD_VALUE(y)) return 1; // not equal
91 return %SimdEquals(x, y);
94 if (IS_SPEC_OBJECT(y)) return %_ObjectEquals(x, y) ? 0 : 1;
95 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
97 y = %to_number_fun(y);
98 } else if (IS_SYMBOL(y) || IS_SIMD_VALUE(y)) {
99 return 1; // not equal
101 x = %to_primitive(x, NO_HINT);
107 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
108 // the result when either (or both) the operands are NaN.
109 function COMPARE(x, ncr) {
112 // Fast cases for string, numbers and undefined compares.
113 if (IS_STRING(this)) {
114 if (IS_STRING(x)) return %_StringCompare(this, x);
115 if (IS_UNDEFINED(x)) return ncr;
117 } else if (IS_NUMBER(this)) {
118 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
119 if (IS_UNDEFINED(x)) return ncr;
121 } else if (IS_UNDEFINED(this)) {
122 if (!IS_UNDEFINED(x)) {
123 %to_primitive(x, NUMBER_HINT);
126 } else if (IS_UNDEFINED(x)) {
127 %to_primitive(this, NUMBER_HINT);
130 left = %to_primitive(this, NUMBER_HINT);
133 right = %to_primitive(x, NUMBER_HINT);
134 if (IS_STRING(left) && IS_STRING(right)) {
135 return %_StringCompare(left, right);
137 var left_number = %to_number_fun(left);
138 var right_number = %to_number_fun(right);
139 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
140 return %NumberCompare(left_number, right_number, ncr);
144 // Strong mode COMPARE throws if an implicit conversion would be performed
145 function COMPARE_STRONG(x, ncr) {
146 if (IS_STRING(this) && IS_STRING(x)) return %_StringCompare(this, x);
147 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
149 throw %make_type_error(kStrongImplicitConversion);
154 /* -----------------------------------
155 - - - A r i t h m e t i c - - -
156 -----------------------------------
159 // ECMA-262, section 11.6.1, page 50.
161 // Fast case: Check for number operands and do the addition.
162 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
163 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
165 // Default implementation.
166 var a = %to_primitive(this, NO_HINT);
167 var b = %to_primitive(x, NO_HINT);
170 return %_StringAdd(a, %to_string_fun(b));
171 } else if (IS_STRING(b)) {
172 return %_StringAdd(%non_string_to_string(a), b);
174 return %NumberAdd(%to_number_fun(a), %to_number_fun(b));
179 // Strong mode ADD throws if an implicit conversion would be performed
180 function ADD_STRONG(x) {
181 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
182 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
184 throw %make_type_error(kStrongImplicitConversion);
188 // Left operand (this) is already a string.
189 function STRING_ADD_LEFT(y) {
191 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
195 ? %_NumberToString(y)
196 : %to_string_fun(%to_primitive(y, NO_HINT));
199 return %_StringAdd(this, y);
203 // Right operand (y) is already a string.
204 function STRING_ADD_RIGHT(y) {
207 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
211 ? %_NumberToString(x)
212 : %to_string_fun(%to_primitive(x, NO_HINT));
215 return %_StringAdd(x, y);
219 // ECMA-262, section 11.6.2, page 50.
221 var x = IS_NUMBER(this) ? this : %non_number_to_number(this);
222 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
223 return %NumberSub(x, y);
227 // Strong mode SUB throws if an implicit conversion would be performed
228 function SUB_STRONG(y) {
229 if (IS_NUMBER(this) && IS_NUMBER(y)) {
230 return %NumberSub(this, y);
232 throw %make_type_error(kStrongImplicitConversion);
236 // ECMA-262, section 11.5.1, page 48.
238 var x = IS_NUMBER(this) ? this : %non_number_to_number(this);
239 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
240 return %NumberMul(x, y);
244 // Strong mode MUL throws if an implicit conversion would be performed
245 function MUL_STRONG(y) {
246 if (IS_NUMBER(this) && IS_NUMBER(y)) {
247 return %NumberMul(this, y);
249 throw %make_type_error(kStrongImplicitConversion);
253 // ECMA-262, section 11.5.2, page 49.
255 var x = IS_NUMBER(this) ? this : %non_number_to_number(this);
256 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
257 return %NumberDiv(x, y);
261 // Strong mode DIV throws if an implicit conversion would be performed
262 function DIV_STRONG(y) {
263 if (IS_NUMBER(this) && IS_NUMBER(y)) {
264 return %NumberDiv(this, y);
266 throw %make_type_error(kStrongImplicitConversion);
270 // ECMA-262, section 11.5.3, page 49.
272 var x = IS_NUMBER(this) ? this : %non_number_to_number(this);
273 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
274 return %NumberMod(x, y);
278 // Strong mode MOD throws if an implicit conversion would be performed
279 function MOD_STRONG(y) {
280 if (IS_NUMBER(this) && IS_NUMBER(y)) {
281 return %NumberMod(this, y);
283 throw %make_type_error(kStrongImplicitConversion);
287 /* -------------------------------------------
288 - - - B i t o p e r a t i o n s - - -
289 -------------------------------------------
292 // ECMA-262, section 11.10, page 57.
294 var x = IS_NUMBER(this) ? this : %non_number_to_number(this);
295 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
296 return %NumberOr(x, y);
300 // Strong mode BIT_OR throws if an implicit conversion would be performed
301 function BIT_OR_STRONG(y) {
302 if (IS_NUMBER(this) && IS_NUMBER(y)) {
303 return %NumberOr(this, y);
305 throw %make_type_error(kStrongImplicitConversion);
309 // ECMA-262, section 11.10, page 57.
310 function BIT_AND(y) {
312 if (IS_NUMBER(this)) {
314 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
316 x = %non_number_to_number(this);
317 // Make sure to convert the right operand to a number before
318 // bailing out in the fast case, but after converting the
319 // left operand. This ensures that valueOf methods on the right
320 // operand are always executed.
321 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
322 // Optimize for the case where we end up AND'ing a value
323 // that doesn't convert to a number. This is common in
324 // certain benchmarks.
325 if (NUMBER_IS_NAN(x)) return 0;
327 return %NumberAnd(x, y);
331 // Strong mode BIT_AND throws if an implicit conversion would be performed
332 function BIT_AND_STRONG(y) {
333 if (IS_NUMBER(this) && IS_NUMBER(y)) {
334 return %NumberAnd(this, y);
336 throw %make_type_error(kStrongImplicitConversion);
340 // ECMA-262, section 11.10, page 57.
341 function BIT_XOR(y) {
342 var x = IS_NUMBER(this) ? this : %non_number_to_number(this);
343 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
344 return %NumberXor(x, y);
348 // Strong mode BIT_XOR throws if an implicit conversion would be performed
349 function BIT_XOR_STRONG(y) {
350 if (IS_NUMBER(this) && IS_NUMBER(y)) {
351 return %NumberXor(this, y);
353 throw %make_type_error(kStrongImplicitConversion);
357 // ECMA-262, section 11.7.1, page 51.
359 var x = IS_NUMBER(this) ? this : %non_number_to_number(this);
360 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
361 return %NumberShl(x, y);
365 // Strong mode SHL throws if an implicit conversion would be performed
366 function SHL_STRONG(y) {
367 if (IS_NUMBER(this) && IS_NUMBER(y)) {
368 return %NumberShl(this, y);
370 throw %make_type_error(kStrongImplicitConversion);
374 // ECMA-262, section 11.7.2, page 51.
377 if (IS_NUMBER(this)) {
379 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
381 x = %non_number_to_number(this);
382 // Make sure to convert the right operand to a number before
383 // bailing out in the fast case, but after converting the
384 // left operand. This ensures that valueOf methods on the right
385 // operand are always executed.
386 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
387 // Optimize for the case where we end up shifting a value
388 // that doesn't convert to a number. This is common in
389 // certain benchmarks.
390 if (NUMBER_IS_NAN(x)) return 0;
392 return %NumberSar(x, y);
396 // Strong mode SAR throws if an implicit conversion would be performed
397 function SAR_STRONG(y) {
398 if (IS_NUMBER(this) && IS_NUMBER(y)) {
399 return %NumberSar(this, y);
401 throw %make_type_error(kStrongImplicitConversion);
405 // ECMA-262, section 11.7.3, page 52.
407 var x = IS_NUMBER(this) ? this : %non_number_to_number(this);
408 if (!IS_NUMBER(y)) y = %non_number_to_number(y);
409 return %NumberShr(x, y);
413 // Strong mode SHR throws if an implicit conversion would be performed
414 function SHR_STRONG(y) {
415 if (IS_NUMBER(this) && IS_NUMBER(y)) {
416 return %NumberShr(this, y);
418 throw %make_type_error(kStrongImplicitConversion);
422 /* -----------------------------
423 - - - H e l p e r s - - -
424 -----------------------------
427 // ECMA-262, section 11.8.7, page 54.
429 if (!IS_SPEC_OBJECT(x)) {
430 throw %make_type_error(kInvalidInOperatorUse, this, x);
432 if (%_IsNonNegativeSmi(this)) {
433 if (IS_ARRAY(x) && %_HasFastPackedElements(x)) {
434 return this < x.length;
436 return %HasElement(x, this);
438 return %HasProperty(x, this);
442 function CALL_NON_FUNCTION() {
443 var delegate = %GetFunctionDelegate(this);
444 if (!IS_FUNCTION(delegate)) {
445 var callsite = %RenderCallSite();
446 if (callsite == "") callsite = typeof this;
447 throw %make_type_error(kCalledNonCallable, callsite);
449 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
453 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
454 var delegate = %GetConstructorDelegate(this);
455 if (!IS_FUNCTION(delegate)) {
456 var callsite = %RenderCallSite();
457 if (callsite == "") callsite = typeof this;
458 throw %make_type_error(kCalledNonCallable, callsite);
460 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
464 function CALL_FUNCTION_PROXY() {
465 var arity = %_ArgumentsLength() - 1;
466 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
467 var trap = %GetCallTrap(proxy);
468 return %Apply(trap, this, arguments, 0, arity);
472 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR () {
474 var trap = %GetConstructTrap(proxy);
475 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
479 function APPLY_PREPARE(args) {
481 // First check whether length is a positive Smi and args is an
482 // array. This is the fast case. If this fails, we do the slow case
483 // that takes care of more eventualities.
484 if (IS_ARRAY(args)) {
485 length = args.length;
486 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
487 IS_SPEC_FUNCTION(this)) {
492 length = (args == null) ? 0 : TO_UINT32(args.length);
494 // We can handle any number of apply arguments if the stack is
495 // big enough, but sanity check the value to avoid overflow when
496 // multiplying with pointer size.
497 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow);
499 if (!IS_SPEC_FUNCTION(this)) {
500 throw %make_type_error(kApplyNonFunction, %to_string_fun(this), typeof this);
503 // Make sure the arguments list has the right type.
504 if (args != null && !IS_SPEC_OBJECT(args)) {
505 throw %make_type_error(kWrongArgs, "Function.prototype.apply");
508 // Return the length which is the number of arguments to copy to the
509 // stack. It is guaranteed to be a small integer at this point.
514 function REFLECT_APPLY_PREPARE(args) {
516 // First check whether length is a positive Smi and args is an
517 // array. This is the fast case. If this fails, we do the slow case
518 // that takes care of more eventualities.
519 if (IS_ARRAY(args)) {
520 length = args.length;
521 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
522 IS_SPEC_FUNCTION(this)) {
527 if (!IS_SPEC_FUNCTION(this)) {
528 throw %make_type_error(kCalledNonCallable, %to_string_fun(this));
531 if (!IS_SPEC_OBJECT(args)) {
532 throw %make_type_error(kWrongArgs, "Reflect.apply");
535 length = %to_length_fun(args.length);
537 // We can handle any number of apply arguments if the stack is
538 // big enough, but sanity check the value to avoid overflow when
539 // multiplying with pointer size.
540 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow);
542 // Return the length which is the number of arguments to copy to the
543 // stack. It is guaranteed to be a small integer at this point.
548 function REFLECT_CONSTRUCT_PREPARE(
551 var ctorOk = IS_SPEC_FUNCTION(this) && %IsConstructor(this);
552 var newTargetOk = IS_SPEC_FUNCTION(newTarget) && %IsConstructor(newTarget);
554 // First check whether length is a positive Smi and args is an
555 // array. This is the fast case. If this fails, we do the slow case
556 // that takes care of more eventualities.
557 if (IS_ARRAY(args)) {
558 length = args.length;
559 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
560 ctorOk && newTargetOk) {
566 if (!IS_SPEC_FUNCTION(this)) {
567 throw %make_type_error(kCalledNonCallable, %to_string_fun(this));
569 throw %make_type_error(kNotConstructor, %to_string_fun(this));
574 if (!IS_SPEC_FUNCTION(newTarget)) {
575 throw %make_type_error(kCalledNonCallable, %to_string_fun(newTarget));
577 throw %make_type_error(kNotConstructor, %to_string_fun(newTarget));
581 if (!IS_SPEC_OBJECT(args)) {
582 throw %make_type_error(kWrongArgs, "Reflect.construct");
585 length = %to_length_fun(args.length);
587 // We can handle any number of apply arguments if the stack is
588 // big enough, but sanity check the value to avoid overflow when
589 // multiplying with pointer size.
590 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow);
592 // Return the length which is the number of arguments to copy to the
593 // stack. It is guaranteed to be a small integer at this point.
598 function CONCAT_ITERABLE_TO_ARRAY(iterable) {
599 return %concat_iterable_to_array(this, iterable);
603 function STACK_OVERFLOW(length) {
604 throw %make_range_error(kStackOverflow);
608 // Convert the receiver to a number - forward to ToNumber.
609 function TO_NUMBER() {
610 return %to_number_fun(this);
614 // Convert the receiver to a string - forward to ToString.
615 function TO_STRING() {
616 return %to_string_fun(this);
620 /* -------------------------------------
621 - - - C o n v e r s i o n s - - -
622 -------------------------------------
625 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
626 // (1) for number hint, and (2) for string hint.
627 function ToPrimitive(x, hint) {
628 if (!IS_SPEC_OBJECT(x)) return x;
629 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
630 return (hint == NUMBER_HINT) ? DefaultNumber(x) : DefaultString(x);
634 // ECMA-262, section 9.2, page 30
635 function ToBoolean(x) {
636 if (IS_BOOLEAN(x)) return x;
637 if (IS_STRING(x)) return x.length != 0;
638 if (x == null) return false;
639 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
644 // ECMA-262, section 9.3, page 31.
645 function ToNumber(x) {
646 if (IS_NUMBER(x)) return x;
648 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
649 : %StringToNumber(x);
651 if (IS_BOOLEAN(x)) return x ? 1 : 0;
652 if (IS_UNDEFINED(x)) return NAN;
653 // Types that can't be converted to number are caught in DefaultNumber.
654 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x));
657 function NonNumberToNumber(x) {
659 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
660 : %StringToNumber(x);
662 if (IS_BOOLEAN(x)) return x ? 1 : 0;
663 if (IS_UNDEFINED(x)) return NAN;
664 // Types that can't be converted to number are caught in DefaultNumber.
665 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x));
669 // ECMA-262, section 9.8, page 35.
670 function ToString(x) {
671 if (IS_STRING(x)) return x;
672 if (IS_NUMBER(x)) return %_NumberToString(x);
673 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
674 if (IS_UNDEFINED(x)) return 'undefined';
675 // Types that can't be converted to string are caught in DefaultString.
676 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x));
679 function NonStringToString(x) {
680 if (IS_NUMBER(x)) return %_NumberToString(x);
681 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
682 if (IS_UNDEFINED(x)) return 'undefined';
683 // Types that can't be converted to string are caught in DefaultString.
684 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x));
688 // ECMA-262, section 9.4, page 34.
689 function ToInteger(x) {
690 if (%_IsSmi(x)) return x;
691 return %NumberToInteger(ToNumber(x));
695 // ES6, draft 08-24-14, section 7.1.15
696 function ToLength(arg) {
697 arg = ToInteger(arg);
698 if (arg < 0) return 0;
699 return arg < GlobalNumber.MAX_SAFE_INTEGER ? arg
700 : GlobalNumber.MAX_SAFE_INTEGER;
705 function SameValue(x, y) {
706 if (typeof x != typeof y) return false;
708 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
709 // x is +0 and y is -0 or vice versa.
710 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
714 if (IS_SIMD_VALUE(x)) return %SimdSameValue(x, y);
719 // ES6, section 7.2.4
720 function SameValueZero(x, y) {
721 if (typeof x != typeof y) return false;
723 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
725 if (IS_SIMD_VALUE(x)) return %SimdSameValueZero(x, y);
730 function ConcatIterableToArray(target, iterable) {
731 var index = target.length;
732 for (var element of iterable) {
733 %AddElement(target, index++, element);
739 /* ---------------------------------
740 - - - U t i l i t i e s - - -
741 ---------------------------------
744 // Returns if the given x is a primitive value - not an object or a
746 function IsPrimitive(x) {
747 // Even though the type of null is "object", null is still
748 // considered a primitive value. IS_SPEC_OBJECT handles this correctly
749 // (i.e., it will return false if x is null).
750 return !IS_SPEC_OBJECT(x);
754 // ES6, draft 10-14-14, section 22.1.3.1.1
755 function IsConcatSpreadable(O) {
756 if (!IS_SPEC_OBJECT(O)) return false;
757 var spreadable = O[isConcatSpreadableSymbol];
758 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O);
759 return ToBoolean(spreadable);
763 // ECMA-262, section 8.6.2.6, page 28.
764 function DefaultNumber(x) {
765 var valueOf = x.valueOf;
766 if (IS_SPEC_FUNCTION(valueOf)) {
767 var v = %_CallFunction(x, valueOf);
768 if (IS_SYMBOL(v)) throw MakeTypeError(kSymbolToNumber);
769 if (IS_SIMD_VALUE(x)) throw MakeTypeError(kSimdToNumber);
770 if (IsPrimitive(v)) return v;
772 var toString = x.toString;
773 if (IS_SPEC_FUNCTION(toString)) {
774 var s = %_CallFunction(x, toString);
775 if (IsPrimitive(s)) return s;
777 throw MakeTypeError(kCannotConvertToPrimitive);
780 // ECMA-262, section 8.6.2.6, page 28.
781 function DefaultString(x) {
782 if (!IS_SYMBOL_WRAPPER(x)) {
783 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToString);
784 var toString = x.toString;
785 if (IS_SPEC_FUNCTION(toString)) {
786 var s = %_CallFunction(x, toString);
787 if (IsPrimitive(s)) return s;
790 var valueOf = x.valueOf;
791 if (IS_SPEC_FUNCTION(valueOf)) {
792 var v = %_CallFunction(x, valueOf);
793 if (IsPrimitive(v)) return v;
796 throw MakeTypeError(kCannotConvertToPrimitive);
799 function ToPositiveInteger(x, rangeErrorIndex) {
800 var i = TO_INTEGER_MAP_MINUS_ZERO(x);
801 if (i < 0) throw MakeRangeError(rangeErrorIndex);
805 //----------------------------------------------------------------------------
807 // NOTE: Setting the prototype for Array must take place as early as
808 // possible due to code generation for array literals. When
809 // generating code for a array literal a boilerplate array is created
810 // that is cloned when running the code. It is essential that the
811 // boilerplate gets the right prototype.
812 %FunctionSetPrototype(GlobalArray, new GlobalArray(0));
814 // ----------------------------------------------------------------------------
817 $defaultString = DefaultString;
818 $NaN = %GetRootNaN();
819 $nonNumberToNumber = NonNumberToNumber;
820 $nonStringToString = NonStringToString;
821 $sameValue = SameValue;
822 $sameValueZero = SameValueZero;
823 $toInteger = ToInteger;
824 $toLength = ToLength;
825 $toNumber = ToNumber;
826 $toPositiveInteger = ToPositiveInteger;
827 $toPrimitive = ToPrimitive;
828 $toString = ToString;
832 "add_strong_builtin", ADD_STRONG,
833 "apply_prepare_builtin", APPLY_PREPARE,
834 "bit_and_builtin", BIT_AND,
835 "bit_and_strong_builtin", BIT_AND_STRONG,
836 "bit_or_builtin", BIT_OR,
837 "bit_or_strong_builtin", BIT_OR_STRONG,
838 "bit_xor_builtin", BIT_XOR,
839 "bit_xor_strong_builtin", BIT_XOR_STRONG,
840 "call_function_proxy_as_constructor_builtin", CALL_FUNCTION_PROXY_AS_CONSTRUCTOR,
841 "call_function_proxy_builtin", CALL_FUNCTION_PROXY,
842 "call_non_function_as_constructor_builtin", CALL_NON_FUNCTION_AS_CONSTRUCTOR,
843 "call_non_function_builtin", CALL_NON_FUNCTION,
844 "compare_builtin", COMPARE,
845 "compare_strong_builtin", COMPARE_STRONG,
846 "concat_iterable_to_array_builtin", CONCAT_ITERABLE_TO_ARRAY,
848 "div_strong_builtin", DIV_STRONG,
849 "equals_builtin", EQUALS,
852 "mod_strong_builtin", MOD_STRONG,
854 "mul_strong_builtin", MUL_STRONG,
855 "reflect_apply_prepare_builtin", REFLECT_APPLY_PREPARE,
856 "reflect_construct_prepare_builtin", REFLECT_CONSTRUCT_PREPARE,
858 "sar_strong_builtin", SAR_STRONG,
860 "shl_strong_builtin", SHL_STRONG,
862 "shr_strong_builtin", SHR_STRONG,
863 "stack_overflow_builtin", STACK_OVERFLOW,
864 "string_add_left_builtin", STRING_ADD_LEFT,
865 "string_add_right_builtin", STRING_ADD_RIGHT,
867 "sub_strong_builtin", SUB_STRONG,
871 "concat_iterable_to_array", ConcatIterableToArray,
872 "non_number_to_number", NonNumberToNumber,
873 "non_string_to_string", NonStringToString,
874 "to_integer_fun", ToInteger,
875 "to_length_fun", ToLength,
876 "to_number_fun", ToNumber,
877 "to_primitive", ToPrimitive,
878 "to_string_fun", ToString,
881 utils.Export(function(to) {
882 to.ToBoolean = ToBoolean;
883 to.ToLength = ToLength;
884 to.ToNumber = ToNumber;
885 to.ToPrimitive = ToPrimitive;
886 to.ToString = ToString;