1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // This files contains runtime support implemented in JavaScript.
30 // CAUTION: Some of the functions specified in this file are called
31 // directly from compiled code. These are the functions with names in
32 // ALL CAPS. The compiled code passes the first argument in 'this' and
33 // it does not push the function onto the stack. This means that you
34 // cannot use contexts in all these functions.
37 /* -----------------------------------
38 - - - C o m p a r i s o n - - -
39 -----------------------------------
42 // The following declarations are shared with other native JS files.
43 // They are all declared at this one spot to avoid redeclaration errors.
44 var $Object = global.Object;
45 var $Array = global.Array;
46 var $String = global.String;
47 var $Number = global.Number;
48 var $Function = global.Function;
49 var $Boolean = global.Boolean;
50 var $NaN = %GetRootNaN();
52 // ECMA-262 Section 11.9.3.
54 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
60 if (IS_NUMBER(y)) return %NumberEquals(x, y);
61 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
62 if (!IS_SPEC_OBJECT(y)) {
64 return %NumberEquals(x, %ToNumber(y));
66 y = %ToPrimitive(y, NO_HINT);
68 } else if (IS_STRING(x)) {
70 if (IS_STRING(y)) return %StringEquals(x, y);
71 if (IS_SYMBOL(y)) return 1; // not equal
72 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
73 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
74 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
75 if (IsFloat32x4(y) || IsInt32x4(y)) {
76 return %StringEquals(x, %ToString(y));
78 y = %ToPrimitive(y, NO_HINT);
80 } else if (IS_SYMBOL(x)) {
81 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
82 return 1; // not equal
83 } else if (IsFloat32x4(x)) {
85 if (IsFloat32x4(y) || IsInt32x4(y)) {
86 return (x.x == y.x && x.y == y.y && x.z == y.z && x.w == y.w) ? 0 : 1;
88 if (IS_STRING(y)) return %StringEquals(%ToString(x), y);
89 if (IS_NUMBER(y)) return 1; // not equal
90 if (IS_SYMBOL(y)) return 1; // not equal
91 if (IS_BOOLEAN(y)) return y ? 0 : 1;
92 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
93 y = %ToPrimitive(y, NO_HINT);
95 } else if (IsInt32x4(x)) {
97 if (IsFloat32x4(y) || IsInt32x4(y)) {
98 return (x.x == y.x && x.y == y.y && x.z == y.z && x.w == y.w) ? 0 : 1;
100 if (IS_STRING(y)) return %StringEquals(%ToString(x), y);
101 if (IS_NUMBER(y)) return 1; // not equal
102 if (IS_SYMBOL(y)) return 1; // not equal
103 if (IS_BOOLEAN(y)) return y ? 0 : 1;
104 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
105 y = %ToPrimitive(y, NO_HINT);
107 } else if (IS_BOOLEAN(x)) {
108 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
109 if (IS_NULL_OR_UNDEFINED(y)) return 1;
110 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
111 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
112 if (IS_SYMBOL(y)) return 1; // not equal
113 if (IsFloat32x4(y) || IsInt32x4(y)) return x ? 0 : 1;
116 y = %ToPrimitive(y, NO_HINT);
117 } else if (IS_NULL_OR_UNDEFINED(x)) {
118 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
121 if (IS_SPEC_OBJECT(y)) {
122 return %_ObjectEquals(x, y) ? 0 : 1;
124 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
125 if (IS_SYMBOL(y)) return 1; // not equal
126 if (IS_BOOLEAN(y)) y = %ToNumber(y);
127 x = %ToPrimitive(x, NO_HINT);
132 // ECMA-262, section 11.9.4, page 56.
133 function STRICT_EQUALS(x) {
134 if (IS_STRING(this)) {
135 if (!IS_STRING(x)) return 1; // not equal
136 return %StringEquals(this, x);
139 if (IS_NUMBER(this)) {
140 if (!IS_NUMBER(x)) return 1; // not equal
141 return %NumberEquals(this, x);
144 if (IsFloat32x4(this)) {
145 if (!IsFloat32x4(x)) return 1; // not equal
146 return (this.x == x.x && this.y == x.y &&
147 this.z == x.z && this.w == x.w) ? 0 : 1;
150 if (IsInt32x4(this)) {
151 if (!IsInt32x4(x)) return 1; // not equal
152 return (this.x == x.x && this.y == x.y &&
153 this.z == x.z && this.w == x.w) ? 0 : 1;
156 // If anything else gets here, we just do simple identity check.
157 // Objects (including functions), null, undefined and booleans were
158 // checked in the CompareStub, so there should be nothing left.
159 return %_ObjectEquals(this, x) ? 0 : 1;
163 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
164 // the result when either (or both) the operands are NaN.
165 function COMPARE(x, ncr) {
168 // Fast cases for string, numbers and undefined compares.
169 if (IS_STRING(this)) {
170 if (IS_STRING(x)) return %_StringCompare(this, x);
171 if (IS_UNDEFINED(x)) return ncr;
173 } else if (IS_NUMBER(this)) {
174 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
175 if (IS_UNDEFINED(x)) return ncr;
177 } else if (IS_UNDEFINED(this)) {
178 if (!IS_UNDEFINED(x)) {
179 %ToPrimitive(x, NUMBER_HINT);
182 } else if (IS_UNDEFINED(x)) {
183 %ToPrimitive(this, NUMBER_HINT);
186 left = %ToPrimitive(this, NUMBER_HINT);
189 right = %ToPrimitive(x, NUMBER_HINT);
190 if (IS_STRING(left) && IS_STRING(right)) {
191 return %_StringCompare(left, right);
192 } else if ((IsFloat32x4(left) || IsInt32x4(left)) &&
193 (IsFloat32x4(right) || IsInt32x4(right))) {
194 if ((left.x == right.x) && (left.y == right.y) &&
195 (left.z == right.z) && (left.w == right.w)) {
198 if ((left.x < right.x) && (left.y < right.y) &&
199 (left.z < right.z) && (left.w < right.w)) {
202 if ((left.x > right.x) && (left.y > right.y) &&
203 (left.z > right.z) && (left.w > right.w)) {
207 var left_number = %ToNumber(left);
208 var right_number = %ToNumber(right);
209 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
210 return %NumberCompare(left_number, right_number, ncr);
216 /* -----------------------------------
217 - - - A r i t h m e t i c - - -
218 -----------------------------------
221 // ECMA-262, section 11.6.1, page 50.
223 // Fast case: Check for number operands and do the addition.
224 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
225 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
227 // Default implementation.
228 var a = %ToPrimitive(this, NO_HINT);
229 var b = %ToPrimitive(x, NO_HINT);
232 return %_StringAdd(a, %ToString(b));
233 } else if (IS_STRING(b)) {
234 return %_StringAdd(%NonStringToString(a), b);
236 return %NumberAdd(%ToNumber(a), %ToNumber(b));
241 // Left operand (this) is already a string.
242 function STRING_ADD_LEFT(y) {
244 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
248 ? %_NumberToString(y)
249 : %ToString(%ToPrimitive(y, NO_HINT));
252 return %_StringAdd(this, y);
256 // Right operand (y) is already a string.
257 function STRING_ADD_RIGHT(y) {
260 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
264 ? %_NumberToString(x)
265 : %ToString(%ToPrimitive(x, NO_HINT));
268 return %_StringAdd(x, y);
272 // ECMA-262, section 11.6.2, page 50.
274 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
275 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
276 return %NumberSub(x, y);
280 // ECMA-262, section 11.5.1, page 48.
282 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
283 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
284 return %NumberMul(x, y);
288 // ECMA-262, section 11.5.2, page 49.
290 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
291 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
292 return %NumberDiv(x, y);
296 // ECMA-262, section 11.5.3, page 49.
298 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
299 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
300 return %NumberMod(x, y);
305 /* -------------------------------------------
306 - - - B i t o p e r a t i o n s - - -
307 -------------------------------------------
310 // ECMA-262, section 11.10, page 57.
312 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
313 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
314 return %NumberOr(x, y);
318 // ECMA-262, section 11.10, page 57.
319 function BIT_AND(y) {
321 if (IS_NUMBER(this)) {
323 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
325 x = %NonNumberToNumber(this);
326 // Make sure to convert the right operand to a number before
327 // bailing out in the fast case, but after converting the
328 // left operand. This ensures that valueOf methods on the right
329 // operand are always executed.
330 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
331 // Optimize for the case where we end up AND'ing a value
332 // that doesn't convert to a number. This is common in
333 // certain benchmarks.
334 if (NUMBER_IS_NAN(x)) return 0;
336 return %NumberAnd(x, y);
340 // ECMA-262, section 11.10, page 57.
341 function BIT_XOR(y) {
342 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
343 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
344 return %NumberXor(x, y);
348 // ECMA-262, section 11.7.1, page 51.
350 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
351 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
352 return %NumberShl(x, y);
356 // ECMA-262, section 11.7.2, page 51.
359 if (IS_NUMBER(this)) {
361 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
363 x = %NonNumberToNumber(this);
364 // Make sure to convert the right operand to a number before
365 // bailing out in the fast case, but after converting the
366 // left operand. This ensures that valueOf methods on the right
367 // operand are always executed.
368 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
369 // Optimize for the case where we end up shifting a value
370 // that doesn't convert to a number. This is common in
371 // certain benchmarks.
372 if (NUMBER_IS_NAN(x)) return 0;
374 return %NumberSar(x, y);
378 // ECMA-262, section 11.7.3, page 52.
380 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
381 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
382 return %NumberShr(x, y);
387 /* -----------------------------
388 - - - H e l p e r s - - -
389 -----------------------------
392 // ECMA-262, section 11.4.1, page 46.
393 function DELETE(key, strict) {
394 return %DeleteProperty(%ToObject(this), %ToName(key), strict);
398 // ECMA-262, section 11.8.7, page 54.
400 if (!IS_SPEC_OBJECT(x)) {
401 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
403 return %_IsNonNegativeSmi(this) ?
404 %HasElement(x, this) : %HasProperty(x, %ToName(this));
408 // ECMA-262, section 11.8.6, page 54. To make the implementation more
409 // efficient, the return value should be zero if the 'this' is an
410 // instance of F, and non-zero if not. This makes it possible to avoid
411 // an expensive ToBoolean conversion in the generated code.
412 function INSTANCE_OF(F) {
414 if (!IS_SPEC_FUNCTION(F)) {
415 throw %MakeTypeError('instanceof_function_expected', [F]);
418 // If V is not an object, return false.
419 if (!IS_SPEC_OBJECT(V)) {
423 // Check if function is bound, if so, get [[BoundFunction]] from it
424 // and use that instead of F.
425 var bindings = %BoundFunctionGetBindings(F);
427 F = bindings[kBoundFunctionIndex]; // Always a non-bound function.
429 // Get the prototype of F; if it is not an object, throw an error.
431 if (!IS_SPEC_OBJECT(O)) {
432 throw %MakeTypeError('instanceof_nonobject_proto', [O]);
435 // Return whether or not O is in the prototype chain of V.
436 return %IsInPrototypeChain(O, V) ? 0 : 1;
440 // Filter a given key against an object by checking if the object
441 // has a property with the given key; return the key as a string if
442 // it has. Otherwise returns 0 (smi). Used in for-in statements.
443 function FILTER_KEY(key) {
444 var string = %ToName(key);
445 if (%HasProperty(this, string)) return string;
450 function CALL_NON_FUNCTION() {
451 var delegate = %GetFunctionDelegate(this);
452 if (!IS_FUNCTION(delegate)) {
453 throw %MakeTypeError('called_non_callable', [typeof this]);
455 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
459 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
460 var delegate = %GetConstructorDelegate(this);
461 if (!IS_FUNCTION(delegate)) {
462 throw %MakeTypeError('called_non_callable', [typeof this]);
464 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
468 function CALL_FUNCTION_PROXY() {
469 var arity = %_ArgumentsLength() - 1;
470 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
471 var trap = %GetCallTrap(proxy);
472 return %Apply(trap, this, arguments, 0, arity);
476 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
478 var trap = %GetConstructTrap(proxy);
479 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
483 function APPLY_PREPARE(args) {
485 // First check whether length is a positive Smi and args is an
486 // array. This is the fast case. If this fails, we do the slow case
487 // that takes care of more eventualities.
488 if (IS_ARRAY(args)) {
489 length = args.length;
490 if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
491 IS_SPEC_FUNCTION(this)) {
496 length = (args == null) ? 0 : %ToUint32(args.length);
498 // We can handle any number of apply arguments if the stack is
499 // big enough, but sanity check the value to avoid overflow when
500 // multiplying with pointer size.
501 if (length > 0x800000) {
502 throw %MakeRangeError('stack_overflow', []);
505 if (!IS_SPEC_FUNCTION(this)) {
506 throw %MakeTypeError('apply_non_function',
507 [ %ToString(this), typeof this ]);
510 // Make sure the arguments list has the right type.
511 if (args != null && !IS_SPEC_OBJECT(args)) {
512 throw %MakeTypeError('apply_wrong_args', []);
515 // Return the length which is the number of arguments to copy to the
516 // stack. It is guaranteed to be a small integer at this point.
521 function APPLY_OVERFLOW(length) {
522 throw %MakeRangeError('stack_overflow', []);
526 // Convert the receiver to an object - forward to ToObject.
527 function TO_OBJECT() {
528 return %ToObject(this);
532 // Convert the receiver to a number - forward to ToNumber.
533 function TO_NUMBER() {
534 return %ToNumber(this);
538 // Convert the receiver to a string - forward to ToString.
539 function TO_STRING() {
540 return %ToString(this);
544 /* -------------------------------------
545 - - - C o n v e r s i o n s - - -
546 -------------------------------------
549 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
550 // (1) for number hint, and (2) for string hint.
551 function ToPrimitive(x, hint) {
553 if (IS_STRING(x)) return x;
555 if (!IS_SPEC_OBJECT(x)) return x;
556 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
557 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
558 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
562 // ECMA-262, section 9.2, page 30
563 function ToBoolean(x) {
564 if (IS_BOOLEAN(x)) return x;
565 if (IS_STRING(x)) return x.length != 0;
566 if (x == null) return false;
567 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
572 // ECMA-262, section 9.3, page 31.
573 function ToNumber(x) {
574 if (IS_NUMBER(x)) return x;
576 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
577 : %StringToNumber(x);
579 if (IS_BOOLEAN(x)) return x ? 1 : 0;
580 if (IS_UNDEFINED(x)) return NAN;
581 if (IS_SYMBOL(x)) return NAN;
582 if (IsFloat32x4(x)) return NAN;
583 if (IsInt32x4(x)) return NAN;
584 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
587 function NonNumberToNumber(x) {
589 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
590 : %StringToNumber(x);
592 if (IS_BOOLEAN(x)) return x ? 1 : 0;
593 if (IS_UNDEFINED(x)) return NAN;
594 if (IS_SYMBOL(x)) return NAN;
595 if (IsFloat32x4(x)) return NAN;
596 if (IsInt32x4(x)) return NAN;
597 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
601 // ECMA-262, section 9.8, page 35.
602 function ToString(x) {
603 if (IS_STRING(x)) return x;
604 if (IS_NUMBER(x)) return %_NumberToString(x);
605 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
606 if (IS_UNDEFINED(x)) return 'undefined';
607 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
608 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
611 function NonStringToString(x) {
612 if (IS_NUMBER(x)) return %_NumberToString(x);
613 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
614 if (IS_UNDEFINED(x)) return 'undefined';
615 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
616 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
622 return IS_SYMBOL(x) ? x : %ToString(x);
626 // ECMA-262, section 9.9, page 36.
627 function ToObject(x) {
628 if (IS_STRING(x)) return new $String(x);
629 if (IS_NUMBER(x)) return new $Number(x);
630 if (IS_BOOLEAN(x)) return new $Boolean(x);
631 if (IsFloat32x4(x)) return new $Float32x4(x.x, x.y, x.z, x.w);
632 if (IsInt32x4(x)) return new $Int32x4(x.x, x.y, x.z, x.w);
633 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
634 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
635 throw %MakeTypeError('undefined_or_null_to_object', []);
641 // ECMA-262, section 9.4, page 34.
642 function ToInteger(x) {
643 if (%_IsSmi(x)) return x;
644 return %NumberToInteger(ToNumber(x));
648 // ECMA-262, section 9.6, page 34.
649 function ToUint32(x) {
650 if (%_IsSmi(x) && x >= 0) return x;
651 return %NumberToJSUint32(ToNumber(x));
655 // ECMA-262, section 9.5, page 34
656 function ToInt32(x) {
657 if (%_IsSmi(x)) return x;
658 return %NumberToJSInt32(ToNumber(x));
663 function SameValue(x, y) {
664 if (typeof x != typeof y) return false;
666 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
667 // x is +0 and y is -0 or vice versa.
668 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
676 /* ---------------------------------
677 - - - U t i l i t i e s - - -
678 ---------------------------------
681 // Returns if the given x is a primitive value - not an object or a
683 function IsPrimitive(x) {
684 // Even though the type of null is "object", null is still
685 // considered a primitive value. IS_SPEC_OBJECT handles this correctly
686 // (i.e., it will return false if x is null).
687 return !IS_SPEC_OBJECT(x);
691 // ECMA-262, section 8.6.2.6, page 28.
692 function DefaultNumber(x) {
693 var valueOf = x.valueOf;
694 if (IS_SPEC_FUNCTION(valueOf)) {
695 var v = %_CallFunction(x, valueOf);
696 if (%IsPrimitive(v)) return v;
699 var toString = x.toString;
700 if (IS_SPEC_FUNCTION(toString)) {
701 var s = %_CallFunction(x, toString);
702 if (%IsPrimitive(s)) return s;
705 throw %MakeTypeError('cannot_convert_to_primitive', []);
708 // ECMA-262, section 8.6.2.6, page 28.
709 function DefaultString(x) {
710 var toString = x.toString;
711 if (IS_SPEC_FUNCTION(toString)) {
712 var s = %_CallFunction(x, toString);
713 if (%IsPrimitive(s)) return s;
716 var valueOf = x.valueOf;
717 if (IS_SPEC_FUNCTION(valueOf)) {
718 var v = %_CallFunction(x, valueOf);
719 if (%IsPrimitive(v)) return v;
722 throw %MakeTypeError('cannot_convert_to_primitive', []);
725 function ToPositiveInteger(x, rangeErrorName) {
726 var i = TO_INTEGER(x);
727 if (i < 0) throw MakeRangeError(rangeErrorName);
732 // NOTE: Setting the prototype for Array must take place as early as
733 // possible due to code generation for array literals. When
734 // generating code for a array literal a boilerplate array is created
735 // that is cloned when running the code. It is essential that the
736 // boilerplate gets the right prototype.
737 %FunctionSetPrototype($Array, new $Array(0));