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' and
10 // it does not push the function onto the stack. This means that you
11 // cannot use contexts in all these functions.
14 /* -----------------------------------
15 - - - C o m p a r i s o n - - -
16 -----------------------------------
19 // The following declarations are shared with other native JS files.
20 // They are all declared at this one spot to avoid redeclaration errors.
21 var $Object = global.Object;
22 var $Array = global.Array;
23 var $String = global.String;
24 var $Number = global.Number;
25 var $Function = global.Function;
26 var $Boolean = global.Boolean;
27 var $NaN = %GetRootNaN();
29 // ECMA-262 Section 11.9.3.
31 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
37 if (IS_NUMBER(y)) return %NumberEquals(x, y);
38 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
39 if (IS_SYMBOL(y)) return 1; // not equal
40 if (!IS_SPEC_OBJECT(y)) {
42 return %NumberEquals(x, %ToNumber(y));
44 y = %ToPrimitive(y, NO_HINT);
46 } else if (IS_STRING(x)) {
48 if (IS_STRING(y)) return %StringEquals(x, y);
49 if (IS_SYMBOL(y)) return 1; // not equal
50 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
51 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
52 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
53 if (IsFloat32x4(y) || IsFloat64x2(y) || IsInt32x4(y)) {
54 return %StringEquals(x, %ToString(y));
56 y = %ToPrimitive(y, NO_HINT);
58 } else if (IS_SYMBOL(x)) {
59 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
60 return 1; // not equal
61 } else if (IsFloat32x4(x)) {
63 if (IsFloat32x4(y) || IsInt32x4(y)) {
64 return (x.x == y.x && x.y == y.y && x.z == y.z && x.w == y.w) ? 0 : 1;
66 if (IS_STRING(y)) return %StringEquals(%ToString(x), y);
67 if (IS_NUMBER(y)) return 1; // not equal
68 if (IS_SYMBOL(y)) return 1; // not equal
69 if (IS_BOOLEAN(y)) return y ? 0 : 1;
70 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
71 y = %ToPrimitive(y, NO_HINT);
73 } else if (IsFloat64x2(x)) {
76 return (x.x == y.x && x.y == y.y) ? 0 : 1;
78 if (IS_STRING(y)) return %StringEquals(%ToString(x), y);
79 if (IS_NUMBER(y)) return 1; // not equal
80 if (IS_SYMBOL(y)) return 1; // not equal
81 if (IS_BOOLEAN(y)) return y ? 0 : 1;
82 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
83 y = %ToPrimitive(y, NO_HINT);
85 } else if (IsInt32x4(x)) {
87 if (IsFloat32x4(y) || IsInt32x4(y)) {
88 return (x.x == y.x && x.y == y.y && x.z == y.z && x.w == y.w) ? 0 : 1;
90 if (IS_STRING(y)) return %StringEquals(%ToString(x), y);
91 if (IS_NUMBER(y)) return 1; // not equal
92 if (IS_SYMBOL(y)) return 1; // not equal
93 if (IS_BOOLEAN(y)) return y ? 0 : 1;
94 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
95 y = %ToPrimitive(y, NO_HINT);
97 } else if (IS_BOOLEAN(x)) {
98 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
99 if (IS_NULL_OR_UNDEFINED(y)) return 1;
100 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
101 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
102 if (IS_SYMBOL(y)) return 1; // not equal
103 if (IsFloat32x4(y) || IsFloat64x2(y) || IsInt32x4(y)) return x ? 0 : 1;
106 y = %ToPrimitive(y, NO_HINT);
107 } else if (IS_NULL_OR_UNDEFINED(x)) {
108 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
111 if (IS_SPEC_OBJECT(y)) {
112 return %_ObjectEquals(x, y) ? 0 : 1;
114 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
115 if (IS_SYMBOL(y)) return 1; // not equal
116 if (IS_BOOLEAN(y)) y = %ToNumber(y);
117 x = %ToPrimitive(x, NO_HINT);
122 // ECMA-262, section 11.9.4, page 56.
123 function STRICT_EQUALS(x) {
124 if (IS_STRING(this)) {
125 if (!IS_STRING(x)) return 1; // not equal
126 return %StringEquals(this, x);
129 if (IS_NUMBER(this)) {
130 if (!IS_NUMBER(x)) return 1; // not equal
131 return %NumberEquals(this, x);
134 if (IsFloat32x4(this)) {
135 if (!IsFloat32x4(x)) return 1; // not equal
136 return (this.x == x.x && this.y == x.y &&
137 this.z == x.z && this.w == x.w) ? 0 : 1;
140 if (IsFloat64x2(this)) {
141 if (!IsFloat64x2(x)) return 1; // not equal
142 return (this.x == x.x && this.y == x.y) ? 0 : 1;
145 if (IsInt32x4(this)) {
146 if (!IsInt32x4(x)) return 1; // not equal
147 return (this.x == x.x && this.y == x.y &&
148 this.z == x.z && this.w == x.w) ? 0 : 1;
151 // If anything else gets here, we just do simple identity check.
152 // Objects (including functions), null, undefined and booleans were
153 // checked in the CompareStub, so there should be nothing left.
154 return %_ObjectEquals(this, x) ? 0 : 1;
158 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
159 // the result when either (or both) the operands are NaN.
160 function COMPARE(x, ncr) {
163 // Fast cases for string, numbers and undefined compares.
164 if (IS_STRING(this)) {
165 if (IS_STRING(x)) return %_StringCompare(this, x);
166 if (IS_UNDEFINED(x)) return ncr;
168 } else if (IS_NUMBER(this)) {
169 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
170 if (IS_UNDEFINED(x)) return ncr;
172 } else if (IS_UNDEFINED(this)) {
173 if (!IS_UNDEFINED(x)) {
174 %ToPrimitive(x, NUMBER_HINT);
177 } else if (IS_UNDEFINED(x)) {
178 %ToPrimitive(this, NUMBER_HINT);
181 left = %ToPrimitive(this, NUMBER_HINT);
184 right = %ToPrimitive(x, NUMBER_HINT);
185 if (IS_STRING(left) && IS_STRING(right)) {
186 return %_StringCompare(left, right);
187 } else if ((IsFloat32x4(left) || IsInt32x4(left)) &&
188 (IsFloat32x4(right) || IsInt32x4(right))) {
189 if ((left.x == right.x) && (left.y == right.y) &&
190 (left.z == right.z) && (left.w == right.w)) {
193 if ((left.x < right.x) && (left.y < right.y) &&
194 (left.z < right.z) && (left.w < right.w)) {
197 if ((left.x > right.x) && (left.y > right.y) &&
198 (left.z > right.z) && (left.w > right.w)) {
201 } else if (IsFloat64x2(left) && IsFloat64x2(right)) {
202 if ((left.x == right.x) && (left.y == right.y)) {
205 if ((left.x < right.x) && (left.y < right.y)) {
208 if ((left.x > right.x) && (left.y > right.y)) {
212 var left_number = %ToNumber(left);
213 var right_number = %ToNumber(right);
214 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
215 return %NumberCompare(left_number, right_number, ncr);
221 /* -----------------------------------
222 - - - A r i t h m e t i c - - -
223 -----------------------------------
226 // ECMA-262, section 11.6.1, page 50.
228 // Fast case: Check for number operands and do the addition.
229 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
230 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
232 // Default implementation.
233 var a = %ToPrimitive(this, NO_HINT);
234 var b = %ToPrimitive(x, NO_HINT);
237 return %_StringAdd(a, %ToString(b));
238 } else if (IS_STRING(b)) {
239 return %_StringAdd(%NonStringToString(a), b);
241 return %NumberAdd(%ToNumber(a), %ToNumber(b));
246 // Left operand (this) is already a string.
247 function STRING_ADD_LEFT(y) {
249 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
253 ? %_NumberToString(y)
254 : %ToString(%ToPrimitive(y, NO_HINT));
257 return %_StringAdd(this, y);
261 // Right operand (y) is already a string.
262 function STRING_ADD_RIGHT(y) {
265 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
269 ? %_NumberToString(x)
270 : %ToString(%ToPrimitive(x, NO_HINT));
273 return %_StringAdd(x, y);
277 // ECMA-262, section 11.6.2, page 50.
279 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
280 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
281 return %NumberSub(x, y);
285 // ECMA-262, section 11.5.1, page 48.
287 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
288 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
289 return %NumberMul(x, y);
293 // ECMA-262, section 11.5.2, page 49.
295 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
296 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
297 return %NumberDiv(x, y);
301 // ECMA-262, section 11.5.3, page 49.
303 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
304 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
305 return %NumberMod(x, y);
310 /* -------------------------------------------
311 - - - B i t o p e r a t i o n s - - -
312 -------------------------------------------
315 // ECMA-262, section 11.10, page 57.
317 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
318 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
319 return %NumberOr(x, y);
323 // ECMA-262, section 11.10, page 57.
324 function BIT_AND(y) {
326 if (IS_NUMBER(this)) {
328 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
330 x = %NonNumberToNumber(this);
331 // Make sure to convert the right operand to a number before
332 // bailing out in the fast case, but after converting the
333 // left operand. This ensures that valueOf methods on the right
334 // operand are always executed.
335 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
336 // Optimize for the case where we end up AND'ing a value
337 // that doesn't convert to a number. This is common in
338 // certain benchmarks.
339 if (NUMBER_IS_NAN(x)) return 0;
341 return %NumberAnd(x, y);
345 // ECMA-262, section 11.10, page 57.
346 function BIT_XOR(y) {
347 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
348 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
349 return %NumberXor(x, y);
353 // ECMA-262, section 11.7.1, page 51.
355 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
356 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
357 return %NumberShl(x, y);
361 // ECMA-262, section 11.7.2, page 51.
364 if (IS_NUMBER(this)) {
366 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
368 x = %NonNumberToNumber(this);
369 // Make sure to convert the right operand to a number before
370 // bailing out in the fast case, but after converting the
371 // left operand. This ensures that valueOf methods on the right
372 // operand are always executed.
373 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
374 // Optimize for the case where we end up shifting a value
375 // that doesn't convert to a number. This is common in
376 // certain benchmarks.
377 if (NUMBER_IS_NAN(x)) return 0;
379 return %NumberSar(x, y);
383 // ECMA-262, section 11.7.3, page 52.
385 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
386 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
387 return %NumberShr(x, y);
392 /* -----------------------------
393 - - - H e l p e r s - - -
394 -----------------------------
397 // ECMA-262, section 11.4.1, page 46.
398 function DELETE(key, strict) {
399 return %DeleteProperty(%ToObject(this), %ToName(key), strict);
403 // ECMA-262, section 11.8.7, page 54.
405 if (!IS_SPEC_OBJECT(x)) {
406 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
408 return %_IsNonNegativeSmi(this) ?
409 %HasElement(x, this) : %HasProperty(x, %ToName(this));
413 // ECMA-262, section 11.8.6, page 54. To make the implementation more
414 // efficient, the return value should be zero if the 'this' is an
415 // instance of F, and non-zero if not. This makes it possible to avoid
416 // an expensive ToBoolean conversion in the generated code.
417 function INSTANCE_OF(F) {
419 if (!IS_SPEC_FUNCTION(F)) {
420 throw %MakeTypeError('instanceof_function_expected', [F]);
423 // If V is not an object, return false.
424 if (!IS_SPEC_OBJECT(V)) {
428 // Check if function is bound, if so, get [[BoundFunction]] from it
429 // and use that instead of F.
430 var bindings = %BoundFunctionGetBindings(F);
432 F = bindings[kBoundFunctionIndex]; // Always a non-bound function.
434 // Get the prototype of F; if it is not an object, throw an error.
436 if (!IS_SPEC_OBJECT(O)) {
437 throw %MakeTypeError('instanceof_nonobject_proto', [O]);
440 // Return whether or not O is in the prototype chain of V.
441 return %IsInPrototypeChain(O, V) ? 0 : 1;
445 // Filter a given key against an object by checking if the object
446 // has a property with the given key; return the key as a string if
447 // it has. Otherwise returns 0 (smi). Used in for-in statements.
448 function FILTER_KEY(key) {
449 var string = %ToName(key);
450 if (%HasProperty(this, string)) return string;
455 function CALL_NON_FUNCTION() {
456 var delegate = %GetFunctionDelegate(this);
457 if (!IS_FUNCTION(delegate)) {
458 throw %MakeTypeError('called_non_callable', [typeof this]);
460 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
464 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
465 var delegate = %GetConstructorDelegate(this);
466 if (!IS_FUNCTION(delegate)) {
467 throw %MakeTypeError('called_non_callable', [typeof this]);
469 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
473 function CALL_FUNCTION_PROXY() {
474 var arity = %_ArgumentsLength() - 1;
475 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
476 var trap = %GetCallTrap(proxy);
477 return %Apply(trap, this, arguments, 0, arity);
481 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
483 var trap = %GetConstructTrap(proxy);
484 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
488 function APPLY_PREPARE(args) {
490 // First check whether length is a positive Smi and args is an
491 // array. This is the fast case. If this fails, we do the slow case
492 // that takes care of more eventualities.
493 if (IS_ARRAY(args)) {
494 length = args.length;
495 if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
496 IS_SPEC_FUNCTION(this)) {
501 length = (args == null) ? 0 : %ToUint32(args.length);
503 // We can handle any number of apply arguments if the stack is
504 // big enough, but sanity check the value to avoid overflow when
505 // multiplying with pointer size.
506 if (length > 0x800000) {
507 throw %MakeRangeError('stack_overflow', []);
510 if (!IS_SPEC_FUNCTION(this)) {
511 throw %MakeTypeError('apply_non_function',
512 [ %ToString(this), typeof this ]);
515 // Make sure the arguments list has the right type.
516 if (args != null && !IS_SPEC_OBJECT(args)) {
517 throw %MakeTypeError('apply_wrong_args', []);
520 // Return the length which is the number of arguments to copy to the
521 // stack. It is guaranteed to be a small integer at this point.
526 function STACK_OVERFLOW(length) {
527 throw %MakeRangeError('stack_overflow', []);
531 // Convert the receiver to an object - forward to ToObject.
532 function TO_OBJECT() {
533 return %ToObject(this);
537 // Convert the receiver to a number - forward to ToNumber.
538 function TO_NUMBER() {
539 return %ToNumber(this);
543 // Convert the receiver to a string - forward to ToString.
544 function TO_STRING() {
545 return %ToString(this);
549 /* -------------------------------------
550 - - - C o n v e r s i o n s - - -
551 -------------------------------------
554 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
555 // (1) for number hint, and (2) for string hint.
556 function ToPrimitive(x, hint) {
558 if (IS_STRING(x)) return x;
560 if (!IS_SPEC_OBJECT(x)) return x;
561 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
562 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
563 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
567 // ECMA-262, section 9.2, page 30
568 function ToBoolean(x) {
569 if (IS_BOOLEAN(x)) return x;
570 if (IS_STRING(x)) return x.length != 0;
571 if (x == null) return false;
572 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
577 // ECMA-262, section 9.3, page 31.
578 function ToNumber(x) {
579 if (IS_NUMBER(x)) return x;
581 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
582 : %StringToNumber(x);
584 if (IS_BOOLEAN(x)) return x ? 1 : 0;
585 if (IS_UNDEFINED(x)) return NAN;
586 if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
587 if (IsFloat32x4(x)) return NAN;
588 if (IsFloat64x2(x)) return NAN;
589 if (IsInt32x4(x)) return NAN;
590 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
593 function NonNumberToNumber(x) {
595 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
596 : %StringToNumber(x);
598 if (IS_BOOLEAN(x)) return x ? 1 : 0;
599 if (IS_UNDEFINED(x)) return NAN;
600 if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
601 if (IsFloat32x4(x)) return NAN;
602 if (IsFloat64x2(x)) return NAN;
603 if (IsInt32x4(x)) return NAN;
604 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
608 // ECMA-262, section 9.8, page 35.
609 function ToString(x) {
610 if (IS_STRING(x)) return x;
611 if (IS_NUMBER(x)) return %_NumberToString(x);
612 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
613 if (IS_UNDEFINED(x)) return 'undefined';
614 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
615 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
618 function NonStringToString(x) {
619 if (IS_NUMBER(x)) return %_NumberToString(x);
620 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
621 if (IS_UNDEFINED(x)) return 'undefined';
622 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
623 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
629 return IS_SYMBOL(x) ? x : %ToString(x);
633 // ECMA-262, section 9.9, page 36.
634 function ToObject(x) {
635 if (IS_STRING(x)) return new $String(x);
636 if (IS_NUMBER(x)) return new $Number(x);
637 if (IS_BOOLEAN(x)) return new $Boolean(x);
638 if (IsFloat32x4(x)) return new $Float32x4(x.x, x.y, x.z, x.w);
639 if (IsFloat64x2(x)) return new $Float64x2(x.x, x.y);
640 if (IsInt32x4(x)) return new $Int32x4(x.x, x.y, x.z, x.w);
641 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
642 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
643 throw %MakeTypeError('undefined_or_null_to_object', []);
649 // ECMA-262, section 9.4, page 34.
650 function ToInteger(x) {
651 if (%_IsSmi(x)) return x;
652 return %NumberToInteger(ToNumber(x));
656 // ECMA-262, section 9.6, page 34.
657 function ToUint32(x) {
658 if (%_IsSmi(x) && x >= 0) return x;
659 return %NumberToJSUint32(ToNumber(x));
663 // ECMA-262, section 9.5, page 34
664 function ToInt32(x) {
665 if (%_IsSmi(x)) return x;
666 return %NumberToJSInt32(ToNumber(x));
671 function SameValue(x, y) {
672 if (typeof x != typeof y) return false;
674 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
675 // x is +0 and y is -0 or vice versa.
676 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
684 /* ---------------------------------
685 - - - U t i l i t i e s - - -
686 ---------------------------------
689 // Returns if the given x is a primitive value - not an object or a
691 function IsPrimitive(x) {
692 // Even though the type of null is "object", null is still
693 // considered a primitive value. IS_SPEC_OBJECT handles this correctly
694 // (i.e., it will return false if x is null).
695 return !IS_SPEC_OBJECT(x);
699 // ECMA-262, section 8.6.2.6, page 28.
700 function DefaultNumber(x) {
701 if (!IS_SYMBOL_WRAPPER(x)) {
702 var valueOf = x.valueOf;
703 if (IS_SPEC_FUNCTION(valueOf)) {
704 var v = %_CallFunction(x, valueOf);
705 if (%IsPrimitive(v)) return v;
708 var toString = x.toString;
709 if (IS_SPEC_FUNCTION(toString)) {
710 var s = %_CallFunction(x, toString);
711 if (%IsPrimitive(s)) return s;
714 throw %MakeTypeError('cannot_convert_to_primitive', []);
717 // ECMA-262, section 8.6.2.6, page 28.
718 function DefaultString(x) {
719 if (!IS_SYMBOL_WRAPPER(x)) {
720 var toString = x.toString;
721 if (IS_SPEC_FUNCTION(toString)) {
722 var s = %_CallFunction(x, toString);
723 if (%IsPrimitive(s)) return s;
726 var valueOf = x.valueOf;
727 if (IS_SPEC_FUNCTION(valueOf)) {
728 var v = %_CallFunction(x, valueOf);
729 if (%IsPrimitive(v)) return v;
732 throw %MakeTypeError('cannot_convert_to_primitive', []);
735 function ToPositiveInteger(x, rangeErrorName) {
736 var i = TO_INTEGER(x);
737 if (i < 0) throw MakeRangeError(rangeErrorName);
742 // NOTE: Setting the prototype for Array must take place as early as
743 // possible due to code generation for array literals. When
744 // generating code for a array literal a boilerplate array is created
745 // that is cloned when running the code. It is essential that the
746 // boilerplate gets the right prototype.
747 %FunctionSetPrototype($Array, new $Array(0));