runtime.js 18.1 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 42 43 44 45 46 47 48 49
// 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   - - -
   -----------------------------------
*/

// The following const declarations are shared with other native JS files.
// They are all declared at this one spot to avoid const redeclaration errors.
const $Object = global.Object;
const $Array = global.Array;
const $String = global.String;
const $Number = global.Number;
const $Function = global.Function;
const $Boolean = global.Boolean;
50
const $NaN = 0/0;
51 52 53 54


// ECMA-262, section 11.9.1, page 55.
function EQUALS(y) {
55
  if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  var x = this;

  // NOTE: We use iteration instead of recursion, because it is
  // difficult to call EQUALS with the correct setting of 'this' in
  // an efficient way.
  while (true) {
    if (IS_NUMBER(x)) {
      if (y == null) return 1;  // not equal
      return %NumberEquals(x, %ToNumber(y));
    } else if (IS_STRING(x)) {
      if (IS_STRING(y)) return %StringEquals(x, y);
      if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
      if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
      if (y == null) return 1;  // not equal
      y = %ToPrimitive(y, NO_HINT);
    } else if (IS_BOOLEAN(x)) {
72 73 74
      if (IS_BOOLEAN(y)) {
        return %_ObjectEquals(x, y) ? 0 : 1;
      }
75 76 77 78 79 80
      if (y == null) return 1;  // not equal
      return %NumberEquals(%ToNumber(x), %ToNumber(y));
    } else if (x == null) {
      // NOTE: This checks for both null and undefined.
      return (y == null) ? 0 : 1;
    } else {
81 82
      // x is not a number, boolean, null or undefined.
      if (y == null) return 1;  // not equal
83 84 85 86 87 88
      if (IS_OBJECT(y)) {
        return %_ObjectEquals(x, y) ? 0 : 1;
      }
      if (IS_FUNCTION(y)) {
        return %_ObjectEquals(x, y) ? 0 : 1;
      }
89

90 91 92
      x = %ToPrimitive(x, NO_HINT);
    }
  }
93
}
94 95 96 97 98 99

// 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);
100
  }
101

102 103 104
  if (IS_NUMBER(this)) {
    if (!IS_NUMBER(x)) return 1;  // not equal
    return %NumberEquals(this, x);
105
  }
106

107 108 109
  // 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.
110
  return %_ObjectEquals(this, x) ? 0 : 1;
111
}
112 113 114 115 116


// 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) {
117
  var left;
118

119 120
  // Fast cases for string, numbers and undefined compares.
  if (IS_STRING(this)) {
121
    if (IS_STRING(x)) return %_StringCompare(this, x);
122 123 124 125 126 127 128
    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)) {
129
    return ncr;
130 131 132
  } else {
    if (IS_UNDEFINED(x)) return ncr;
    left = %ToPrimitive(this, NUMBER_HINT);
133 134
  }

135
  // Default implementation.
136 137
  var right = %ToPrimitive(x, NUMBER_HINT);
  if (IS_STRING(left) && IS_STRING(right)) {
138
    return %_StringCompare(left, right);
139
  } else {
140 141 142 143
    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);
144
  }
145
}
146 147 148 149 150 151 152 153 154 155 156



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

160
  // Default implementation.
161 162
  var a = %ToPrimitive(this, NO_HINT);
  var b = %ToPrimitive(x, NO_HINT);
163

164
  if (IS_STRING(a)) {
165
    return %_StringAdd(a, %ToString(b));
166
  } else if (IS_STRING(b)) {
167
    return %_StringAdd(%ToString(a), b);
168 169 170
  } else {
    return %NumberAdd(%ToNumber(a), %ToNumber(b));
  }
171
}
172 173


174
// Left operand (this) is already a string.
175
function STRING_ADD_LEFT(y) {
176 177 178 179 180
  if (!IS_STRING(y)) {
    if (IS_STRING_WRAPPER(y)) {
      y = %_ValueOf(y);
    } else {
      y = IS_NUMBER(y)
181
          ? %_NumberToString(y)
182 183 184
          : %ToString(%ToPrimitive(y, NO_HINT));
    }
  }
185
  return %_StringAdd(this, y);
186 187 188
}


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


205
// ECMA-262, section 11.6.2, page 50.
206 207 208 209
function SUB(y) {
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  if (!IS_NUMBER(y)) y = %ToNumber(y);
  return %NumberSub(x, y);
210
}
211 212 213


// ECMA-262, section 11.5.1, page 48.
214 215 216 217
function MUL(y) {
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  if (!IS_NUMBER(y)) y = %ToNumber(y);
  return %NumberMul(x, y);
218
}
219 220 221


// ECMA-262, section 11.5.2, page 49.
222 223 224 225
function DIV(y) {
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  if (!IS_NUMBER(y)) y = %ToNumber(y);
  return %NumberDiv(x, y);
226
}
227 228 229


// ECMA-262, section 11.5.3, page 49.
230 231 232 233
function MOD(y) {
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  if (!IS_NUMBER(y)) y = %ToNumber(y);
  return %NumberMod(x, y);
234
}
235 236 237 238 239 240 241 242 243



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

// ECMA-262, section 11.10, page 57.
244 245 246 247
function BIT_OR(y) {
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  if (!IS_NUMBER(y)) y = %ToNumber(y);
  return %NumberOr(x, y);
248
}
249 250 251


// ECMA-262, section 11.10, page 57.
252 253 254 255
function BIT_AND(y) {
  var x;
  if (IS_NUMBER(this)) {
    x = this;
256
    if (!IS_NUMBER(y)) y = %ToNumber(y);
257 258
  } else {
    x = %ToNumber(this);
259 260 261 262 263
    // 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.
    if (!IS_NUMBER(y)) y = %ToNumber(y);
264 265 266 267 268 269
    // 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);
270
}
271 272 273


// ECMA-262, section 11.10, page 57.
274 275 276 277
function BIT_XOR(y) {
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  if (!IS_NUMBER(y)) y = %ToNumber(y);
  return %NumberXor(x, y);
278
}
279 280 281 282


// ECMA-262, section 11.4.7, page 47.
function UNARY_MINUS() {
283 284
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  return %NumberUnaryMinus(x);
285
}
286 287 288 289


// ECMA-262, section 11.4.8, page 48.
function BIT_NOT() {
290 291
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  return %NumberNot(x);
292
}
293 294 295


// ECMA-262, section 11.7.1, page 51.
296 297 298 299
function SHL(y) {
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  if (!IS_NUMBER(y)) y = %ToNumber(y);
  return %NumberShl(x, y);
300
}
301 302 303


// ECMA-262, section 11.7.2, page 51.
304 305 306 307
function SAR(y) {
  var x;
  if (IS_NUMBER(this)) {
    x = this;
308
    if (!IS_NUMBER(y)) y = %ToNumber(y);
309 310
  } else {
    x = %ToNumber(this);
311 312 313 314 315
    // 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.
    if (!IS_NUMBER(y)) y = %ToNumber(y);
316 317 318 319 320 321
    // 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);
322
}
323 324 325


// ECMA-262, section 11.7.3, page 52.
326 327 328 329
function SHR(y) {
  var x = IS_NUMBER(this) ? this : %ToNumber(this);
  if (!IS_NUMBER(y)) y = %ToNumber(y);
  return %NumberShr(x, y);
330
}
331 332 333 334 335 336 337 338 339 340 341



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

// ECMA-262, section 11.4.1, page 46.
function DELETE(key) {
  return %DeleteProperty(%ToObject(this), %ToString(key));
342
}
343 344 345 346 347 348 349


// ECMA-262, section 11.8.7, page 54.
function IN(x) {
  if (x == null || (!IS_OBJECT(x) && !IS_FUNCTION(x))) {
    throw %MakeTypeError('invalid_in_operator_use', [this, x]);
  }
350
  return %_IsNonNegativeSmi(this) ? %HasElement(x, this) : %HasProperty(x, %ToString(this));
351
}
352 353


354
// ECMA-262, section 11.8.6, page 54. To make the implementation more
355
// efficient, the return value should be zero if the 'this' is an
356 357
// instance of F, and non-zero if not. This makes it possible to avoid
// an expensive ToBoolean conversion in the generated code.
358 359 360 361 362 363 364 365
function INSTANCE_OF(F) {
  var V = this;
  if (!IS_FUNCTION(F)) {
    throw %MakeTypeError('instanceof_function_expected', [V]);
  }

  // If V is not an object, return false.
  if (IS_NULL(V) || (!IS_OBJECT(V) && !IS_FUNCTION(V))) {
366
    return 1;
367 368 369 370 371 372 373 374 375
  }

  // Get the prototype of F; if it is not an object, throw an error.
  var O = F.prototype;
  if (IS_NULL(O) || (!IS_OBJECT(O) && !IS_FUNCTION(O))) {
    throw %MakeTypeError('instanceof_nonobject_proto', [O]);
  }

  // Return whether or not O is in the prototype chain of V.
376
  return %IsInPrototypeChain(O, V) ? 0 : 1;
377
}
378 379 380 381 382 383


// Get an array of property keys for the given object. Used in
// for-in statements.
function GET_KEYS() {
  return %GetPropertyNames(this);
384
}
385 386 387 388 389 390 391 392 393


// 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
// it has. Otherwise returns null. Used in for-in statements.
function FILTER_KEY(key) {
  var string = %ToString(key);
  if (%HasProperty(this, string)) return string;
  return null;
394
}
395 396 397


function CALL_NON_FUNCTION() {
398
  var delegate = %GetFunctionDelegate(this);
399
  if (!IS_FUNCTION(delegate)) {
400
    throw %MakeTypeError('called_non_callable', [typeof this]);
401
  }
402
  return delegate.apply(this, arguments);
403 404 405 406
}


function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
407
  var delegate = %GetConstructorDelegate(this);
408
  if (!IS_FUNCTION(delegate)) {
409
    throw %MakeTypeError('called_non_callable', [typeof this]);
410
  }
411
  return delegate.apply(this, arguments);
412
}
413 414 415 416


function APPLY_PREPARE(args) {
  var length;
417 418 419
  // 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.
420
  if (IS_ARRAY(args)) {
421 422 423 424 425 426
    length = args.length;
    if (%_IsSmi(length) && length >= 0 && length < 0x800000 && IS_FUNCTION(this)) {
      return length;
    }
  }

427
  length = (args == null) ? 0 : %ToUint32(args.length);
428 429 430 431 432 433 434 435 436 437 438 439 440

  // 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) {
    throw %MakeRangeError('apply_overflow', [length]);
  }

  if (!IS_FUNCTION(this)) {
    throw %MakeTypeError('apply_non_function', [ %ToString(this), typeof this ]);
  }

  // Make sure the arguments list has the right type.
441
  if (args != null && !IS_ARRAY(args) && !IS_ARGUMENTS(args)) {
442 443
    throw %MakeTypeError('apply_wrong_args', []);
  }
444

445 446 447
  // 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;
448
}
449 450 451 452


function APPLY_OVERFLOW(length) {
  throw %MakeRangeError('apply_overflow', [length]);
453
}
454 455 456 457 458


// Convert the receiver to an object - forward to ToObject.
function TO_OBJECT() {
  return %ToObject(this);
459
}
460 461 462 463 464


// Convert the receiver to a number - forward to ToNumber.
function TO_NUMBER() {
  return %ToNumber(this);
465
}
466 467 468 469 470


// Convert the receiver to a string - forward to ToString.
function TO_STRING() {
  return %ToString(this);
471
}
472 473


474 475 476 477 478 479 480
// Specialized version of String.charAt. It assumes string as
// the receiver type and that the index is a number.
function STRING_CHAR_AT(pos) {
  var char_code = %_FastCharCodeAt(this, pos);
  if (!%_IsSmi(char_code)) {
    return %StringCharAt(this, pos);
  }
481
  return %_CharFromCode(char_code);
482 483 484
}


485 486 487 488 489 490 491 492
/* -------------------------------------
   - - -   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) {
493 494
  // Fast case check.
  if (IS_STRING(x)) return x;
495
  // Normal behavior.
496 497 498 499
  if (!IS_OBJECT(x) && !IS_FUNCTION(x)) return x;
  if (x == null) return x;  // check for null, undefined
  if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
  return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
500
}
501 502


503 504 505 506 507 508 509 510 511 512
// 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;
}


513 514 515 516 517
// ECMA-262, section 9.3, page 31.
function ToNumber(x) {
  if (IS_NUMBER(x)) return x;
  if (IS_STRING(x)) return %StringToNumber(x);
  if (IS_BOOLEAN(x)) return x ? 1 : 0;
518
  if (IS_UNDEFINED(x)) return $NaN;
519
  return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
520
}
521 522 523 524 525


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

532 533 534 535 536 537 538
function NonStringToString(x) {
  if (IS_NUMBER(x)) return %NumberToString(x);
  if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
  if (IS_UNDEFINED(x)) return 'undefined';
  return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
}

539 540 541 542 543 544

// 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);
545 546 547
  if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
    throw %MakeTypeError('null_to_object', []);
  }
548
  return x;
549
}
550 551 552 553 554 555


// ECMA-262, section 9.4, page 34.
function ToInteger(x) {
  if (%_IsSmi(x)) return x;
  return %NumberToInteger(ToNumber(x));
556
}
557 558 559 560


// ECMA-262, section 9.6, page 34.
function ToUint32(x) {
561
  if (%_IsSmi(x) && x >= 0) return x;
562
  return %NumberToJSUint32(ToNumber(x));
563
}
564 565 566 567 568 569


// ECMA-262, section 9.5, page 34
function ToInt32(x) {
  if (%_IsSmi(x)) return x;
  return %NumberToJSInt32(ToNumber(x));
570
}
571 572


573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
// ES5, section 9.12
function SameValue(x, y) {
  if (typeof x != typeof y) return false;
  if (IS_NULL_OR_UNDEFINED(x)) return true;
  if (IS_NUMBER(x)) {
    if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
    // x is +0 and y is -0 or vice versa
    if (x === 0 && y === 0 && !%_IsSmi(x) && !%_IsSmi(y) && 
        ((1 / x < 0 && 1 / y > 0) || (1 / x > 0 && 1 / y < 0))) {
      return false;
    }
    return x == y;    
  }
  if (IS_STRING(x)) return %StringEquals(x, y);
  if (IS_BOOLEAN(x))return %NumberEquals(%ToNumber(x),%ToNumber(y));

  return %_ObjectEquals(x, y);
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

/* ---------------------------------
   - - -   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) {
  if (!IS_OBJECT(x) && !IS_FUNCTION(x)) {
    return true;
  } else {
    // Even though the type of null is "object", null is still
    // considered a primitive value.
    return IS_NULL(x);
  }
608
}
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623


// ECMA-262, section 8.6.2.6, page 28.
function DefaultNumber(x) {
  if (IS_FUNCTION(x.valueOf)) {
    var v = x.valueOf();
    if (%IsPrimitive(v)) return v;
  }

  if (IS_FUNCTION(x.toString)) {
    var s = x.toString();
    if (%IsPrimitive(s)) return s;
  }

  throw %MakeTypeError('cannot_convert_to_primitive', []);
624
}
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639


// ECMA-262, section 8.6.2.6, page 28.
function DefaultString(x) {
  if (IS_FUNCTION(x.toString)) {
    var s = x.toString();
    if (%IsPrimitive(s)) return s;
  }

  if (IS_FUNCTION(x.valueOf)) {
    var v = x.valueOf();
    if (%IsPrimitive(v)) return v;
  }

  throw %MakeTypeError('cannot_convert_to_primitive', []);
640
}
641 642 643 644 645 646 647 648


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