runtime.js 18.5 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

// This files contains runtime support implemented in JavaScript.

// CAUTION: Some of the functions specified in this file are called
// directly from compiled code. These are the functions with names in
// ALL CAPS. The compiled code passes the first argument in 'this' and
// it does not push the function onto the stack. This means that you
// cannot use contexts in all these functions.


/* -----------------------------------
   - - -   C o m p a r i s o n   - - -
   -----------------------------------
*/

19 20 21 22 23 24 25 26
// The following declarations are shared with other native JS files.
// They are all declared at this one spot to avoid redeclaration errors.
var $Object = global.Object;
var $Array = global.Array;
var $String = global.String;
var $Number = global.Number;
var $Function = global.Function;
var $Boolean = global.Boolean;
27
var $NaN = %GetRootNaN();
28

29
// ECMA-262 Section 11.9.3.
30
function EQUALS(y) {
31
  if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
32 33 34 35
  var x = this;

  while (true) {
    if (IS_NUMBER(x)) {
36 37 38
      while (true) {
        if (IS_NUMBER(y)) return %NumberEquals(x, y);
        if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
39
        if (IS_SYMBOL(y)) return 1;  // not equal
40 41 42 43 44 45
        if (!IS_SPEC_OBJECT(y)) {
          // String or boolean.
          return %NumberEquals(x, %ToNumber(y));
        }
        y = %ToPrimitive(y, NO_HINT);
      }
46
    } else if (IS_STRING(x)) {
47 48
      while (true) {
        if (IS_STRING(y)) return %StringEquals(x, y);
49
        if (IS_SYMBOL(y)) return 1;  // not equal
50 51 52 53 54
        if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
        if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
        if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
        y = %ToPrimitive(y, NO_HINT);
      }
55
    } else if (IS_SYMBOL(x)) {
56 57
      if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
      return 1; // not equal
58 59 60
    } else if (IS_BOOLEAN(x)) {
      if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
      if (IS_NULL_OR_UNDEFINED(y)) return 1;
61
      if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
62
      if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
63
      if (IS_SYMBOL(y)) return 1;  // not equal
64 65
      // y is object.
      x = %ToNumber(x);
66
      y = %ToPrimitive(y, NO_HINT);
67 68
    } else if (IS_NULL_OR_UNDEFINED(x)) {
      return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
69
    } else {
70
      // x is an object.
71
      if (IS_SPEC_OBJECT(y)) {
72 73
        return %_ObjectEquals(x, y) ? 0 : 1;
      }
74
      if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
75
      if (IS_SYMBOL(y)) return 1;  // not equal
76
      if (IS_BOOLEAN(y)) y = %ToNumber(y);
77 78 79
      x = %ToPrimitive(x, NO_HINT);
    }
  }
80
}
81 82 83 84 85 86

// ECMA-262, section 11.9.4, page 56.
function STRICT_EQUALS(x) {
  if (IS_STRING(this)) {
    if (!IS_STRING(x)) return 1;  // not equal
    return %StringEquals(this, x);
87
  }
88

89 90 91
  if (IS_NUMBER(this)) {
    if (!IS_NUMBER(x)) return 1;  // not equal
    return %NumberEquals(this, x);
92
  }
93

94 95 96
  // If anything else gets here, we just do simple identity check.
  // Objects (including functions), null, undefined and booleans were
  // checked in the CompareStub, so there should be nothing left.
97
  return %_ObjectEquals(this, x) ? 0 : 1;
98
}
99 100 101 102 103


// ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
// the result when either (or both) the operands are NaN.
function COMPARE(x, ncr) {
104
  var left;
105
  var right;
106 107
  // Fast cases for string, numbers and undefined compares.
  if (IS_STRING(this)) {
108
    if (IS_STRING(x)) return %_StringCompare(this, x);
109 110 111 112 113 114 115
    if (IS_UNDEFINED(x)) return ncr;
    left = this;
  } else if (IS_NUMBER(this)) {
    if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
    if (IS_UNDEFINED(x)) return ncr;
    left = this;
  } else if (IS_UNDEFINED(this)) {
116 117 118 119 120 121
    if (!IS_UNDEFINED(x)) {
      %ToPrimitive(x, NUMBER_HINT);
    }
    return ncr;
  } else if (IS_UNDEFINED(x)) {
    %ToPrimitive(this, NUMBER_HINT);
122
    return ncr;
123 124
  } else {
    left = %ToPrimitive(this, NUMBER_HINT);
125 126
  }

127
  right = %ToPrimitive(x, NUMBER_HINT);
128
  if (IS_STRING(left) && IS_STRING(right)) {
129
    return %_StringCompare(left, right);
130
  } else {
131 132 133 134
    var left_number = %ToNumber(left);
    var right_number = %ToNumber(right);
    if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
    return %NumberCompare(left_number, right_number, ncr);
135
  }
136
}
137 138 139 140 141 142 143 144 145 146 147



/* -----------------------------------
   - - -   A r i t h m e t i c   - - -
   -----------------------------------
*/

// ECMA-262, section 11.6.1, page 50.
function ADD(x) {
  // Fast case: Check for number operands and do the addition.
148
  if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
149
  if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
150

151
  // Default implementation.
152 153
  var a = %ToPrimitive(this, NO_HINT);
  var b = %ToPrimitive(x, NO_HINT);
154

155
  if (IS_STRING(a)) {
156
    return %_StringAdd(a, %ToString(b));
157
  } else if (IS_STRING(b)) {
158
    return %_StringAdd(%NonStringToString(a), b);
159 160 161
  } else {
    return %NumberAdd(%ToNumber(a), %ToNumber(b));
  }
162
}
163 164


165
// Left operand (this) is already a string.
166
function STRING_ADD_LEFT(y) {
167
  if (!IS_STRING(y)) {
168
    if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
169 170 171
      y = %_ValueOf(y);
    } else {
      y = IS_NUMBER(y)
172
          ? %_NumberToString(y)
173 174 175
          : %ToString(%ToPrimitive(y, NO_HINT));
    }
  }
176
  return %_StringAdd(this, y);
177 178 179
}


180 181
// Right operand (y) is already a string.
function STRING_ADD_RIGHT(y) {
182 183
  var x = this;
  if (!IS_STRING(x)) {
184
    if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
185 186 187
      x = %_ValueOf(x);
    } else {
      x = IS_NUMBER(x)
188
          ? %_NumberToString(x)
189 190 191
          : %ToString(%ToPrimitive(x, NO_HINT));
    }
  }
192
  return %_StringAdd(x, y);
193 194 195
}


196
// ECMA-262, section 11.6.2, page 50.
197
function SUB(y) {
198 199
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
200
  return %NumberSub(x, y);
201
}
202 203 204


// ECMA-262, section 11.5.1, page 48.
205
function MUL(y) {
206 207
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
208
  return %NumberMul(x, y);
209
}
210 211 212


// ECMA-262, section 11.5.2, page 49.
213
function DIV(y) {
214 215
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
216
  return %NumberDiv(x, y);
217
}
218 219 220


// ECMA-262, section 11.5.3, page 49.
221
function MOD(y) {
222 223
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
224
  return %NumberMod(x, y);
225
}
226 227 228 229 230 231 232 233 234



/* -------------------------------------------
   - - -   B i t   o p e r a t i o n s   - - -
   -------------------------------------------
*/

// ECMA-262, section 11.10, page 57.
235
function BIT_OR(y) {
236 237
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
238
  return %NumberOr(x, y);
239
}
240 241 242


// ECMA-262, section 11.10, page 57.
243 244 245 246
function BIT_AND(y) {
  var x;
  if (IS_NUMBER(this)) {
    x = this;
247
    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
248
  } else {
249
    x = %NonNumberToNumber(this);
250 251 252 253
    // Make sure to convert the right operand to a number before
    // bailing out in the fast case, but after converting the
    // left operand. This ensures that valueOf methods on the right
    // operand are always executed.
254
    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
255 256 257 258 259 260
    // Optimize for the case where we end up AND'ing a value
    // that doesn't convert to a number. This is common in
    // certain benchmarks.
    if (NUMBER_IS_NAN(x)) return 0;
  }
  return %NumberAnd(x, y);
261
}
262 263 264


// ECMA-262, section 11.10, page 57.
265
function BIT_XOR(y) {
266 267
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
268
  return %NumberXor(x, y);
269
}
270 271 272


// ECMA-262, section 11.7.1, page 51.
273
function SHL(y) {
274 275
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
276
  return %NumberShl(x, y);
277
}
278 279 280


// ECMA-262, section 11.7.2, page 51.
281 282 283 284
function SAR(y) {
  var x;
  if (IS_NUMBER(this)) {
    x = this;
285
    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
286
  } else {
287
    x = %NonNumberToNumber(this);
288 289 290 291
    // Make sure to convert the right operand to a number before
    // bailing out in the fast case, but after converting the
    // left operand. This ensures that valueOf methods on the right
    // operand are always executed.
292
    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
293 294 295 296 297 298
    // Optimize for the case where we end up shifting a value
    // that doesn't convert to a number. This is common in
    // certain benchmarks.
    if (NUMBER_IS_NAN(x)) return 0;
  }
  return %NumberSar(x, y);
299
}
300 301 302


// ECMA-262, section 11.7.3, page 52.
303
function SHR(y) {
304 305
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
306
  return %NumberShr(x, y);
307
}
308 309 310 311 312 313 314 315 316



/* -----------------------------
   - - -   H e l p e r s   - - -
   -----------------------------
*/

// ECMA-262, section 11.4.1, page 46.
317
function DELETE(key, strict) {
318
  return %DeleteProperty(%ToObject(this), %ToName(key), strict);
319
}
320 321 322 323


// ECMA-262, section 11.8.7, page 54.
function IN(x) {
324
  if (!IS_SPEC_OBJECT(x)) {
325 326
    throw %MakeTypeError('invalid_in_operator_use', [this, x]);
  }
327
  return %_IsNonNegativeSmi(this) ?
328
    %HasElement(x, this) : %HasProperty(x, %ToName(this));
329
}
330 331


332
// ECMA-262, section 11.8.6, page 54. To make the implementation more
333
// efficient, the return value should be zero if the 'this' is an
334 335
// instance of F, and non-zero if not. This makes it possible to avoid
// an expensive ToBoolean conversion in the generated code.
336 337
function INSTANCE_OF(F) {
  var V = this;
338
  if (!IS_SPEC_FUNCTION(F)) {
339
    throw %MakeTypeError('instanceof_function_expected', [F]);
340 341 342
  }

  // If V is not an object, return false.
343
  if (!IS_SPEC_OBJECT(V)) {
344
    return 1;
345 346
  }

347 348 349 350 351 352
  // Check if function is bound, if so, get [[BoundFunction]] from it
  // and use that instead of F.
  var bindings = %BoundFunctionGetBindings(F);
  if (bindings) {
    F = bindings[kBoundFunctionIndex];  // Always a non-bound function.
  }
353 354
  // Get the prototype of F; if it is not an object, throw an error.
  var O = F.prototype;
355
  if (!IS_SPEC_OBJECT(O)) {
356 357 358 359
    throw %MakeTypeError('instanceof_nonobject_proto', [O]);
  }

  // Return whether or not O is in the prototype chain of V.
360
  return %IsInPrototypeChain(O, V) ? 0 : 1;
361
}
362 363 364 365


// Filter a given key against an object by checking if the object
// has a property with the given key; return the key as a string if
366
// it has. Otherwise returns 0 (smi). Used in for-in statements.
367
function FILTER_KEY(key) {
368
  var string = %ToName(key);
369
  if (%HasProperty(this, string)) return string;
370
  return 0;
371
}
372 373 374


function CALL_NON_FUNCTION() {
375
  var delegate = %GetFunctionDelegate(this);
376
  if (!IS_FUNCTION(delegate)) {
377
    throw %MakeTypeError('called_non_callable', [typeof this]);
378
  }
379
  return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
380 381 382 383
}


function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
384
  var delegate = %GetConstructorDelegate(this);
385
  if (!IS_FUNCTION(delegate)) {
386
    throw %MakeTypeError('called_non_callable', [typeof this]);
387
  }
388
  return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
389
}
390 391


392 393 394 395 396 397 398 399
function CALL_FUNCTION_PROXY() {
  var arity = %_ArgumentsLength() - 1;
  var proxy = %_Arguments(arity);  // The proxy comes in as an additional arg.
  var trap = %GetCallTrap(proxy);
  return %Apply(trap, this, arguments, 0, arity);
}


400 401
function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
  var proxy = this;
402
  var trap = %GetConstructTrap(proxy);
403
  return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
404 405 406
}


407 408
function APPLY_PREPARE(args) {
  var length;
409 410 411
  // First check whether length is a positive Smi and args is an
  // array. This is the fast case. If this fails, we do the slow case
  // that takes care of more eventualities.
412
  if (IS_ARRAY(args)) {
413
    length = args.length;
414 415
    if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
        IS_SPEC_FUNCTION(this)) {
416 417 418 419
      return length;
    }
  }

420
  length = (args == null) ? 0 : %ToUint32(args.length);
421 422 423 424 425

  // We can handle any number of apply arguments if the stack is
  // big enough, but sanity check the value to avoid overflow when
  // multiplying with pointer size.
  if (length > 0x800000) {
426
    throw %MakeRangeError('stack_overflow', []);
427 428
  }

429
  if (!IS_SPEC_FUNCTION(this)) {
430 431
    throw %MakeTypeError('apply_non_function',
                         [ %ToString(this), typeof this ]);
432 433 434
  }

  // Make sure the arguments list has the right type.
435
  if (args != null && !IS_SPEC_OBJECT(args)) {
436 437
    throw %MakeTypeError('apply_wrong_args', []);
  }
438

439 440 441
  // Return the length which is the number of arguments to copy to the
  // stack. It is guaranteed to be a small integer at this point.
  return length;
442
}
443 444


445
function STACK_OVERFLOW(length) {
446
  throw %MakeRangeError('stack_overflow', []);
447
}
448 449 450 451 452


// Convert the receiver to an object - forward to ToObject.
function TO_OBJECT() {
  return %ToObject(this);
453
}
454 455 456 457 458


// Convert the receiver to a number - forward to ToNumber.
function TO_NUMBER() {
  return %ToNumber(this);
459
}
460 461 462 463 464


// Convert the receiver to a string - forward to ToString.
function TO_STRING() {
  return %ToString(this);
465
}
466 467 468 469 470 471 472 473 474 475


/* -------------------------------------
   - - -   C o n v e r s i o n s   - - -
   -------------------------------------
*/

// ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
// (1) for number hint, and (2) for string hint.
function ToPrimitive(x, hint) {
476 477
  // Fast case check.
  if (IS_STRING(x)) return x;
478
  // Normal behavior.
479
  if (!IS_SPEC_OBJECT(x)) return x;
480
  if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
481 482
  if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
  return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
483
}
484 485


486 487 488 489 490 491 492 493 494 495
// ECMA-262, section 9.2, page 30
function ToBoolean(x) {
  if (IS_BOOLEAN(x)) return x;
  if (IS_STRING(x)) return x.length != 0;
  if (x == null) return false;
  if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
  return true;
}


496 497 498
// ECMA-262, section 9.3, page 31.
function ToNumber(x) {
  if (IS_NUMBER(x)) return x;
499 500 501 502
  if (IS_STRING(x)) {
    return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
                                    : %StringToNumber(x);
  }
503
  if (IS_BOOLEAN(x)) return x ? 1 : 0;
504
  if (IS_UNDEFINED(x)) return NAN;
505
  if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
506
  return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
507
}
508

509 510 511 512 513 514
function NonNumberToNumber(x) {
  if (IS_STRING(x)) {
    return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
                                    : %StringToNumber(x);
  }
  if (IS_BOOLEAN(x)) return x ? 1 : 0;
515
  if (IS_UNDEFINED(x)) return NAN;
516
  if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
517 518 519
  return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
}

520 521 522 523

// ECMA-262, section 9.8, page 35.
function ToString(x) {
  if (IS_STRING(x)) return x;
524
  if (IS_NUMBER(x)) return %_NumberToString(x);
525 526
  if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
  if (IS_UNDEFINED(x)) return 'undefined';
527
  if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
528
  return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
529
}
530

531
function NonStringToString(x) {
532
  if (IS_NUMBER(x)) return %_NumberToString(x);
533 534
  if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
  if (IS_UNDEFINED(x)) return 'undefined';
535
  if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
536 537 538
  return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
}

539

540 541 542 543 544 545
// ES6 symbols
function ToName(x) {
  return IS_SYMBOL(x) ? x : %ToString(x);
}


546 547 548 549 550
// ECMA-262, section 9.9, page 36.
function ToObject(x) {
  if (IS_STRING(x)) return new $String(x);
  if (IS_NUMBER(x)) return new $Number(x);
  if (IS_BOOLEAN(x)) return new $Boolean(x);
551
  if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
552
  if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
553
    throw %MakeTypeError('undefined_or_null_to_object', []);
554
  }
555
  return x;
556
}
557 558 559 560 561 562


// ECMA-262, section 9.4, page 34.
function ToInteger(x) {
  if (%_IsSmi(x)) return x;
  return %NumberToInteger(ToNumber(x));
563
}
564 565 566 567


// ECMA-262, section 9.6, page 34.
function ToUint32(x) {
568
  if (%_IsSmi(x) && x >= 0) return x;
569
  return %NumberToJSUint32(ToNumber(x));
570
}
571 572 573 574 575 576


// ECMA-262, section 9.5, page 34
function ToInt32(x) {
  if (%_IsSmi(x)) return x;
  return %NumberToJSInt32(ToNumber(x));
577
}
578 579


580 581 582 583 584
// ES5, section 9.12
function SameValue(x, y) {
  if (typeof x != typeof y) return false;
  if (IS_NUMBER(x)) {
    if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
585
    // x is +0 and y is -0 or vice versa.
586 587 588
    if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
      return false;
    }
589
  }
590
  return x === y;
591 592
}

593 594 595 596 597 598 599 600 601

/* ---------------------------------
   - - -   U t i l i t i e s   - - -
   ---------------------------------
*/

// Returns if the given x is a primitive value - not an object or a
// function.
function IsPrimitive(x) {
602 603 604 605
  // Even though the type of null is "object", null is still
  // considered a primitive value. IS_SPEC_OBJECT handles this correctly
  // (i.e., it will return false if x is null).
  return !IS_SPEC_OBJECT(x);
606
}
607 608 609 610


// ECMA-262, section 8.6.2.6, page 28.
function DefaultNumber(x) {
611 612 613 614 615 616
  if (!IS_SYMBOL_WRAPPER(x)) {
    var valueOf = x.valueOf;
    if (IS_SPEC_FUNCTION(valueOf)) {
      var v = %_CallFunction(x, valueOf);
      if (%IsPrimitive(v)) return v;
    }
617

618 619 620 621 622
    var toString = x.toString;
    if (IS_SPEC_FUNCTION(toString)) {
      var s = %_CallFunction(x, toString);
      if (%IsPrimitive(s)) return s;
    }
623 624
  }
  throw %MakeTypeError('cannot_convert_to_primitive', []);
625
}
626 627 628

// ECMA-262, section 8.6.2.6, page 28.
function DefaultString(x) {
629 630 631 632 633 634
  if (!IS_SYMBOL_WRAPPER(x)) {
    var toString = x.toString;
    if (IS_SPEC_FUNCTION(toString)) {
      var s = %_CallFunction(x, toString);
      if (%IsPrimitive(s)) return s;
    }
635

636 637 638 639 640
    var valueOf = x.valueOf;
    if (IS_SPEC_FUNCTION(valueOf)) {
      var v = %_CallFunction(x, valueOf);
      if (%IsPrimitive(v)) return v;
    }
641 642
  }
  throw %MakeTypeError('cannot_convert_to_primitive', []);
643
}
644

645 646
function ToPositiveInteger(x, rangeErrorName) {
  var i = TO_INTEGER(x);
647
  if (i < 0) throw MakeRangeError(rangeErrorName);
648 649 650
  return i;
}

651 652 653 654

// NOTE: Setting the prototype for Array must take place as early as
// possible due to code generation for array literals.  When
// generating code for a array literal a boilerplate array is created
655
// that is cloned when running the code.  It is essential that the
656 657
// boilerplate gets the right prototype.
%FunctionSetPrototype($Array, new $Array(0));