runtime.js 19.7 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// 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   - - -
   -----------------------------------
*/

42 43 44 45 46 47 48 49
// 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;
50
var $NaN = %GetRootNaN();
51
var builtins = this;
52

53
// ECMA-262 Section 11.9.3.
54
function EQUALS(y) {
55
  if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
56 57 58 59
  var x = this;

  while (true) {
    if (IS_NUMBER(x)) {
60 61 62 63 64 65 66 67 68
      while (true) {
        if (IS_NUMBER(y)) return %NumberEquals(x, y);
        if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
        if (!IS_SPEC_OBJECT(y)) {
          // String or boolean.
          return %NumberEquals(x, %ToNumber(y));
        }
        y = %ToPrimitive(y, NO_HINT);
      }
69
    } else if (IS_STRING(x)) {
70 71
      while (true) {
        if (IS_STRING(y)) return %StringEquals(x, y);
72
        if (IS_SYMBOL(y)) return 1;  // not equal
73 74 75 76 77
        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);
      }
78 79 80 81 82 83
    } else if (IS_SYMBOL(x)) {
      while (true) {
        if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
        if (!IS_SPEC_OBJECT(y)) return 1;  // not equal
        y = %ToPrimitive(y, NO_HINT);
      }
84 85 86
    } else if (IS_BOOLEAN(x)) {
      if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
      if (IS_NULL_OR_UNDEFINED(y)) return 1;
87
      if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
88
      if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
89
      if (IS_SYMBOL(y)) return 1;  // not equal
90 91
      // y is object.
      x = %ToNumber(x);
92
      y = %ToPrimitive(y, NO_HINT);
93 94
    } else if (IS_NULL_OR_UNDEFINED(x)) {
      return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
95
    } else {
96
      // x is an object.
97
      if (IS_SPEC_OBJECT(y)) {
98 99
        return %_ObjectEquals(x, y) ? 0 : 1;
      }
100 101
      if (IS_NULL_OR_UNDEFINED(y)) return 1;  // not equal
      if (IS_BOOLEAN(y)) y = %ToNumber(y);
102 103 104
      x = %ToPrimitive(x, NO_HINT);
    }
  }
105
}
106 107 108 109 110 111

// 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);
112
  }
113

114 115 116
  if (IS_NUMBER(this)) {
    if (!IS_NUMBER(x)) return 1;  // not equal
    return %NumberEquals(this, x);
117
  }
118

119 120 121
  // 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.
122
  return %_ObjectEquals(this, x) ? 0 : 1;
123
}
124 125 126 127 128


// 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) {
129
  var left;
130
  var right;
131 132
  // Fast cases for string, numbers and undefined compares.
  if (IS_STRING(this)) {
133
    if (IS_STRING(x)) return %_StringCompare(this, x);
134 135 136 137 138 139 140
    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)) {
141 142 143 144 145 146
    if (!IS_UNDEFINED(x)) {
      %ToPrimitive(x, NUMBER_HINT);
    }
    return ncr;
  } else if (IS_UNDEFINED(x)) {
    %ToPrimitive(this, NUMBER_HINT);
147
    return ncr;
148 149
  } else {
    left = %ToPrimitive(this, NUMBER_HINT);
150 151
  }

152
  right = %ToPrimitive(x, NUMBER_HINT);
153
  if (IS_STRING(left) && IS_STRING(right)) {
154
    return %_StringCompare(left, right);
155
  } else {
156 157 158 159
    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);
160
  }
161
}
162 163 164 165 166 167 168 169 170 171 172



/* -----------------------------------
   - - -   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.
173
  if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
174
  if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
175

176
  // Default implementation.
177 178
  var a = %ToPrimitive(this, NO_HINT);
  var b = %ToPrimitive(x, NO_HINT);
179

180
  if (IS_STRING(a)) {
181
    return %_StringAdd(a, %ToString(b));
182
  } else if (IS_STRING(b)) {
183
    return %_StringAdd(%NonStringToString(a), b);
184 185 186
  } else {
    return %NumberAdd(%ToNumber(a), %ToNumber(b));
  }
187
}
188 189


190
// Left operand (this) is already a string.
191
function STRING_ADD_LEFT(y) {
192
  if (!IS_STRING(y)) {
193
    if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
194 195 196
      y = %_ValueOf(y);
    } else {
      y = IS_NUMBER(y)
197
          ? %_NumberToString(y)
198 199 200
          : %ToString(%ToPrimitive(y, NO_HINT));
    }
  }
201
  return %_StringAdd(this, y);
202 203 204
}


205 206
// Right operand (y) is already a string.
function STRING_ADD_RIGHT(y) {
207 208
  var x = this;
  if (!IS_STRING(x)) {
209
    if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
210 211 212
      x = %_ValueOf(x);
    } else {
      x = IS_NUMBER(x)
213
          ? %_NumberToString(x)
214 215 216
          : %ToString(%ToPrimitive(x, NO_HINT));
    }
  }
217
  return %_StringAdd(x, y);
218 219 220
}


221
// ECMA-262, section 11.6.2, page 50.
222
function SUB(y) {
223 224
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
225
  return %NumberSub(x, y);
226
}
227 228 229


// ECMA-262, section 11.5.1, page 48.
230
function MUL(y) {
231 232
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
233
  return %NumberMul(x, y);
234
}
235 236 237


// ECMA-262, section 11.5.2, page 49.
238
function DIV(y) {
239 240
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
241
  return %NumberDiv(x, y);
242
}
243 244 245


// ECMA-262, section 11.5.3, page 49.
246
function MOD(y) {
247 248
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
249
  return %NumberMod(x, y);
250
}
251 252 253 254 255 256 257 258 259



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

// ECMA-262, section 11.10, page 57.
260
function BIT_OR(y) {
261 262
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
263
  return %NumberOr(x, y);
264
}
265 266 267


// ECMA-262, section 11.10, page 57.
268 269 270 271
function BIT_AND(y) {
  var x;
  if (IS_NUMBER(this)) {
    x = this;
272
    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
273
  } else {
274
    x = %NonNumberToNumber(this);
275 276 277 278
    // 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.
279
    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
280 281 282 283 284 285
    // 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);
286
}
287 288 289


// ECMA-262, section 11.10, page 57.
290
function BIT_XOR(y) {
291 292
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
293
  return %NumberXor(x, y);
294
}
295 296 297 298


// ECMA-262, section 11.4.7, page 47.
function UNARY_MINUS() {
299
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
300
  return %NumberUnaryMinus(x);
301
}
302 303 304 305


// ECMA-262, section 11.4.8, page 48.
function BIT_NOT() {
306
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
307
  return %NumberNot(x);
308
}
309 310 311


// ECMA-262, section 11.7.1, page 51.
312
function SHL(y) {
313 314
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
315
  return %NumberShl(x, y);
316
}
317 318 319


// ECMA-262, section 11.7.2, page 51.
320 321 322 323
function SAR(y) {
  var x;
  if (IS_NUMBER(this)) {
    x = this;
324
    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
325
  } else {
326
    x = %NonNumberToNumber(this);
327 328 329 330
    // 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.
331
    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
332 333 334 335 336 337
    // 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);
338
}
339 340 341


// ECMA-262, section 11.7.3, page 52.
342
function SHR(y) {
343 344
  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
345
  return %NumberShr(x, y);
346
}
347 348 349 350 351 352 353 354 355



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

// ECMA-262, section 11.4.1, page 46.
356
function DELETE(key, strict) {
357
  return %DeleteProperty(%ToObject(this), %ToName(key), strict);
358
}
359 360 361 362


// ECMA-262, section 11.8.7, page 54.
function IN(x) {
363
  if (!IS_SPEC_OBJECT(x)) {
364 365
    throw %MakeTypeError('invalid_in_operator_use', [this, x]);
  }
366
  return %_IsNonNegativeSmi(this) ?
367
    %HasElement(x, this) : %HasProperty(x, %ToName(this));
368
}
369 370


371
// ECMA-262, section 11.8.6, page 54. To make the implementation more
372
// efficient, the return value should be zero if the 'this' is an
373 374
// instance of F, and non-zero if not. This makes it possible to avoid
// an expensive ToBoolean conversion in the generated code.
375 376
function INSTANCE_OF(F) {
  var V = this;
377
  if (!IS_SPEC_FUNCTION(F)) {
yangguo@chromium.org's avatar
yangguo@chromium.org committed
378
    throw %MakeTypeError('instanceof_function_expected', [V]);
379 380 381
  }

  // If V is not an object, return false.
382
  if (!IS_SPEC_OBJECT(V)) {
383
    return 1;
384 385
  }

386 387 388 389 390 391
  // 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.
  }
392 393
  // Get the prototype of F; if it is not an object, throw an error.
  var O = F.prototype;
394
  if (!IS_SPEC_OBJECT(O)) {
395 396 397 398
    throw %MakeTypeError('instanceof_nonobject_proto', [O]);
  }

  // Return whether or not O is in the prototype chain of V.
399
  return %IsInPrototypeChain(O, V) ? 0 : 1;
400
}
401 402 403 404


// 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
405
// it has. Otherwise returns 0 (smi). Used in for-in statements.
406
function FILTER_KEY(key) {
407
  var string = %ToName(key);
408
  if (%HasProperty(this, string)) return string;
409
  return 0;
410
}
411 412 413


function CALL_NON_FUNCTION() {
414
  var delegate = %GetFunctionDelegate(this);
415
  if (!IS_FUNCTION(delegate)) {
416
    throw %MakeTypeError('called_non_callable', [typeof this]);
417
  }
418
  return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
419 420 421 422
}


function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
423
  var delegate = %GetConstructorDelegate(this);
424
  if (!IS_FUNCTION(delegate)) {
425
    throw %MakeTypeError('called_non_callable', [typeof this]);
426
  }
427
  return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
428
}
429 430


431 432 433 434 435 436 437 438
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);
}


439 440
function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
  var proxy = this;
441
  var trap = %GetConstructTrap(proxy);
442
  return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
443 444 445
}


446 447
function APPLY_PREPARE(args) {
  var length;
448 449 450
  // 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.
451
  if (IS_ARRAY(args)) {
452
    length = args.length;
453 454
    if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
        IS_SPEC_FUNCTION(this)) {
455 456 457 458
      return length;
    }
  }

459
  length = (args == null) ? 0 : %ToUint32(args.length);
460 461 462 463 464

  // 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) {
465
    throw %MakeRangeError('stack_overflow', []);
466 467
  }

468
  if (!IS_SPEC_FUNCTION(this)) {
469 470
    throw %MakeTypeError('apply_non_function',
                         [ %ToString(this), typeof this ]);
471 472 473
  }

  // Make sure the arguments list has the right type.
474
  if (args != null && !IS_SPEC_OBJECT(args)) {
475 476
    throw %MakeTypeError('apply_wrong_args', []);
  }
477

478 479 480
  // 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;
481
}
482 483 484


function APPLY_OVERFLOW(length) {
485
  throw %MakeRangeError('stack_overflow', []);
486
}
487 488 489 490 491


// Convert the receiver to an object - forward to ToObject.
function TO_OBJECT() {
  return %ToObject(this);
492
}
493 494 495 496 497


// Convert the receiver to a number - forward to ToNumber.
function TO_NUMBER() {
  return %ToNumber(this);
498
}
499 500 501 502 503


// Convert the receiver to a string - forward to ToString.
function TO_STRING() {
  return %ToString(this);
504
}
505 506 507 508 509 510 511 512 513 514


/* -------------------------------------
   - - -   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) {
515 516
  // Fast case check.
  if (IS_STRING(x)) return x;
517
  // Normal behavior.
518
  if (!IS_SPEC_OBJECT(x)) return x;
519
  if (IS_SYMBOL_WRAPPER(x)) return %_ValueOf(x);
520 521
  if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
  return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
522
}
523 524


525 526 527 528 529 530 531 532 533 534
// 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;
}


535 536 537
// ECMA-262, section 9.3, page 31.
function ToNumber(x) {
  if (IS_NUMBER(x)) return x;
538 539 540 541
  if (IS_STRING(x)) {
    return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
                                    : %StringToNumber(x);
  }
542
  if (IS_BOOLEAN(x)) return x ? 1 : 0;
543
  if (IS_UNDEFINED(x)) return $NaN;
544
  if (IS_SYMBOL(x)) return $NaN;
545
  return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
546
}
547

548 549 550 551 552 553 554
function NonNumberToNumber(x) {
  if (IS_STRING(x)) {
    return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
                                    : %StringToNumber(x);
  }
  if (IS_BOOLEAN(x)) return x ? 1 : 0;
  if (IS_UNDEFINED(x)) return $NaN;
555
  if (IS_SYMBOL(x)) return $NaN;
556 557 558
  return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
}

559 560 561 562

// ECMA-262, section 9.8, page 35.
function ToString(x) {
  if (IS_STRING(x)) return x;
563
  if (IS_NUMBER(x)) return %_NumberToString(x);
564 565 566
  if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
  if (IS_UNDEFINED(x)) return 'undefined';
  return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
567
}
568

569
function NonStringToString(x) {
570
  if (IS_NUMBER(x)) return %_NumberToString(x);
571 572 573 574 575
  if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
  if (IS_UNDEFINED(x)) return 'undefined';
  return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
}

576

577 578 579 580 581 582
// ES6 symbols
function ToName(x) {
  return IS_SYMBOL(x) ? x : %ToString(x);
}


583 584 585
// ECMA-262, section 9.9, page 36.
function ToObject(x) {
  if (IS_STRING(x)) return new $String(x);
586
  if (IS_SYMBOL(x)) return new $Symbol(x);
587 588
  if (IS_NUMBER(x)) return new $Number(x);
  if (IS_BOOLEAN(x)) return new $Boolean(x);
589 590 591
  if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
    throw %MakeTypeError('null_to_object', []);
  }
592
  return x;
593
}
594 595 596 597 598 599


// ECMA-262, section 9.4, page 34.
function ToInteger(x) {
  if (%_IsSmi(x)) return x;
  return %NumberToInteger(ToNumber(x));
600
}
601 602 603 604


// ECMA-262, section 9.6, page 34.
function ToUint32(x) {
605
  if (%_IsSmi(x) && x >= 0) return x;
606
  return %NumberToJSUint32(ToNumber(x));
607
}
608 609 610 611 612 613


// ECMA-262, section 9.5, page 34
function ToInt32(x) {
  if (%_IsSmi(x)) return x;
  return %NumberToJSInt32(ToNumber(x));
614
}
615 616


617 618 619 620 621
// 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;
622
    // x is +0 and y is -0 or vice versa.
623
    if (x === 0 && y === 0 && (1 / x) != (1 / y)) return false;
624
  }
625
  return x === y;
626 627
}

628 629 630 631 632 633 634 635 636

/* ---------------------------------
   - - -   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) {
637 638 639 640
  // 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);
641
}
642 643 644 645


// ECMA-262, section 8.6.2.6, page 28.
function DefaultNumber(x) {
646
  var valueOf = x.valueOf;
647
  if (IS_SPEC_FUNCTION(valueOf)) {
648
    var v = %_CallFunction(x, valueOf);
649 650 651
    if (%IsPrimitive(v)) return v;
  }

652
  var toString = x.toString;
653
  if (IS_SPEC_FUNCTION(toString)) {
654
    var s = %_CallFunction(x, toString);
655 656 657 658
    if (%IsPrimitive(s)) return s;
  }

  throw %MakeTypeError('cannot_convert_to_primitive', []);
659
}
660 661 662 663


// ECMA-262, section 8.6.2.6, page 28.
function DefaultString(x) {
664
  var toString = x.toString;
665
  if (IS_SPEC_FUNCTION(toString)) {
666
    var s = %_CallFunction(x, toString);
667 668 669
    if (%IsPrimitive(s)) return s;
  }

670
  var valueOf = x.valueOf;
671
  if (IS_SPEC_FUNCTION(valueOf)) {
672
    var v = %_CallFunction(x, valueOf);
673 674 675 676
    if (%IsPrimitive(v)) return v;
  }

  throw %MakeTypeError('cannot_convert_to_primitive', []);
677
}
678 679 680 681 682


// 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
683
// that is cloned when running the code.  It is essential that the
684 685
// boilerplate gets the right prototype.
%FunctionSetPrototype($Array, new $Array(0));