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 4 // met: 5 // 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. 15 // 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. 27 28 // This files contains runtime support implemented in JavaScript. 29 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. 35 36 37 /* ----------------------------------- 38 - - - C o m p a r i s o n - - - 39 ----------------------------------- 40 */ 41 42 // The following const declarations are shared with other native JS files. 43 // They are all declared at this one spot to avoid const redeclaration errors. 44 const $Object = global.Object; 45 const $Array = global.Array; 46 const $String = global.String; 47 const $Number = global.Number; 48 const $Function = global.Function; 49 const $Boolean = global.Boolean; 50 const $NaN = 0/0; 51 52 53 // ECMA-262, section 11.9.1, page 55. 54 function EQUALS(y) { 55 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); 56 var x = this; 57 58 // NOTE: We use iteration instead of recursion, because it is 59 // difficult to call EQUALS with the correct setting of 'this' in 60 // an efficient way. 61 while (true) { 62 if (IS_NUMBER(x)) { 63 if (y == null) return 1; // not equal 64 return %NumberEquals(x, %ToNumber(y)); 65 } else if (IS_STRING(x)) { 66 if (IS_STRING(y)) return %StringEquals(x, y); 67 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); 68 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); 69 if (y == null) return 1; // not equal 70 y = %ToPrimitive(y, NO_HINT); 71 } else if (IS_BOOLEAN(x)) { 72 if (IS_BOOLEAN(y)) { 73 return %_ObjectEquals(x, y) ? 0 : 1; 74 } 75 if (y == null) return 1; // not equal 76 return %NumberEquals(%ToNumber(x), %ToNumber(y)); 77 } else if (x == null) { 78 // NOTE: This checks for both null and undefined. 79 return (y == null) ? 0 : 1; 80 } else { 81 // x is not a number, boolean, null or undefined. 82 if (y == null) return 1; // not equal 83 if (IS_OBJECT(y)) { 84 return %_ObjectEquals(x, y) ? 0 : 1; 85 } 86 if (IS_FUNCTION(y)) { 87 return %_ObjectEquals(x, y) ? 0 : 1; 88 } 89 90 x = %ToPrimitive(x, NO_HINT); 91 } 92 } 93 } 94 95 // ECMA-262, section 11.9.4, page 56. 96 function STRICT_EQUALS(x) { 97 if (IS_STRING(this)) { 98 if (!IS_STRING(x)) return 1; // not equal 99 return %StringEquals(this, x); 100 } 101 102 if (IS_NUMBER(this)) { 103 if (!IS_NUMBER(x)) return 1; // not equal 104 return %NumberEquals(this, x); 105 } 106 107 // If anything else gets here, we just do simple identity check. 108 // Objects (including functions), null, undefined and booleans were 109 // checked in the CompareStub, so there should be nothing left. 110 return %_ObjectEquals(this, x) ? 0 : 1; 111 } 112 113 114 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as 115 // the result when either (or both) the operands are NaN. 116 function COMPARE(x, ncr) { 117 var left; 118 119 // Fast cases for string, numbers and undefined compares. 120 if (IS_STRING(this)) { 121 if (IS_STRING(x)) return %_StringCompare(this, x); 122 if (IS_UNDEFINED(x)) return ncr; 123 left = this; 124 } else if (IS_NUMBER(this)) { 125 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr); 126 if (IS_UNDEFINED(x)) return ncr; 127 left = this; 128 } else if (IS_UNDEFINED(this)) { 129 return ncr; 130 } else { 131 if (IS_UNDEFINED(x)) return ncr; 132 left = %ToPrimitive(this, NUMBER_HINT); 133 } 134 135 // Default implementation. 136 var right = %ToPrimitive(x, NUMBER_HINT); 137 if (IS_STRING(left) && IS_STRING(right)) { 138 return %_StringCompare(left, right); 139 } else { 140 var left_number = %ToNumber(left); 141 var right_number = %ToNumber(right); 142 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr; 143 return %NumberCompare(left_number, right_number, ncr); 144 } 145 } 146 147 148 149 /* ----------------------------------- 150 - - - A r i t h m e t i c - - - 151 ----------------------------------- 152 */ 153 154 // ECMA-262, section 11.6.1, page 50. 155 function ADD(x) { 156 // 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 var a = %ToPrimitive(this, NO_HINT); 162 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 } else { 169 return %NumberAdd(%ToNumber(a), %ToNumber(b)); 170 } 171 } 172 173 174 // Left operand (this) is already a string. 175 function STRING_ADD_LEFT(y) { 176 if (!IS_STRING(y)) { 177 if (IS_STRING_WRAPPER(y)) { 178 y = %_ValueOf(y); 179 } else { 180 y = IS_NUMBER(y) 181 ? %_NumberToString(y) 182 : %ToString(%ToPrimitive(y, NO_HINT)); 183 } 184 } 185 return %_StringAdd(this, y); 186 } 187 188 189 // Right operand (y) is already a string. 190 function STRING_ADD_RIGHT(y) { 191 var x = this; 192 if (!IS_STRING(x)) { 193 if (IS_STRING_WRAPPER(x)) { 194 x = %_ValueOf(x); 195 } else { 196 x = IS_NUMBER(x) 197 ? %_NumberToString(x) 198 : %ToString(%ToPrimitive(x, NO_HINT)); 199 } 200 } 201 return %_StringAdd(x, y); 202 } 203 204 205 // ECMA-262, section 11.6.2, page 50. 206 function SUB(y) { 207 var x = IS_NUMBER(this) ? this : %ToNumber(this); 208 if (!IS_NUMBER(y)) y = %ToNumber(y); 209 return %NumberSub(x, y); 210 } 211 212 213 // ECMA-262, section 11.5.1, page 48. 214 function MUL(y) { 215 var x = IS_NUMBER(this) ? this : %ToNumber(this); 216 if (!IS_NUMBER(y)) y = %ToNumber(y); 217 return %NumberMul(x, y); 218 } 219 220 221 // ECMA-262, section 11.5.2, page 49. 222 function DIV(y) { 223 var x = IS_NUMBER(this) ? this : %ToNumber(this); 224 if (!IS_NUMBER(y)) y = %ToNumber(y); 225 return %NumberDiv(x, y); 226 } 227 228 229 // ECMA-262, section 11.5.3, page 49. 230 function MOD(y) { 231 var x = IS_NUMBER(this) ? this : %ToNumber(this); 232 if (!IS_NUMBER(y)) y = %ToNumber(y); 233 return %NumberMod(x, y); 234 } 235 236 237 238 /* ------------------------------------------- 239 - - - B i t o p e r a t i o n s - - - 240 ------------------------------------------- 241 */ 242 243 // ECMA-262, section 11.10, page 57. 244 function BIT_OR(y) { 245 var x = IS_NUMBER(this) ? this : %ToNumber(this); 246 if (!IS_NUMBER(y)) y = %ToNumber(y); 247 return %NumberOr(x, y); 248 } 249 250 251 // ECMA-262, section 11.10, page 57. 252 function BIT_AND(y) { 253 var x; 254 if (IS_NUMBER(this)) { 255 x = this; 256 if (!IS_NUMBER(y)) y = %ToNumber(y); 257 } else { 258 x = %ToNumber(this); 259 // Make sure to convert the right operand to a number before 260 // bailing out in the fast case, but after converting the 261 // left operand. This ensures that valueOf methods on the right 262 // operand are always executed. 263 if (!IS_NUMBER(y)) y = %ToNumber(y); 264 // Optimize for the case where we end up AND'ing a value 265 // that doesn't convert to a number. This is common in 266 // certain benchmarks. 267 if (NUMBER_IS_NAN(x)) return 0; 268 } 269 return %NumberAnd(x, y); 270 } 271 272 273 // ECMA-262, section 11.10, page 57. 274 function BIT_XOR(y) { 275 var x = IS_NUMBER(this) ? this : %ToNumber(this); 276 if (!IS_NUMBER(y)) y = %ToNumber(y); 277 return %NumberXor(x, y); 278 } 279 280 281 // ECMA-262, section 11.4.7, page 47. 282 function UNARY_MINUS() { 283 var x = IS_NUMBER(this) ? this : %ToNumber(this); 284 return %NumberUnaryMinus(x); 285 } 286 287 288 // ECMA-262, section 11.4.8, page 48. 289 function BIT_NOT() { 290 var x = IS_NUMBER(this) ? this : %ToNumber(this); 291 return %NumberNot(x); 292 } 293 294 295 // ECMA-262, section 11.7.1, page 51. 296 function SHL(y) { 297 var x = IS_NUMBER(this) ? this : %ToNumber(this); 298 if (!IS_NUMBER(y)) y = %ToNumber(y); 299 return %NumberShl(x, y); 300 } 301 302 303 // ECMA-262, section 11.7.2, page 51. 304 function SAR(y) { 305 var x; 306 if (IS_NUMBER(this)) { 307 x = this; 308 if (!IS_NUMBER(y)) y = %ToNumber(y); 309 } else { 310 x = %ToNumber(this); 311 // Make sure to convert the right operand to a number before 312 // bailing out in the fast case, but after converting the 313 // left operand. This ensures that valueOf methods on the right 314 // operand are always executed. 315 if (!IS_NUMBER(y)) y = %ToNumber(y); 316 // Optimize for the case where we end up shifting a value 317 // that doesn't convert to a number. This is common in 318 // certain benchmarks. 319 if (NUMBER_IS_NAN(x)) return 0; 320 } 321 return %NumberSar(x, y); 322 } 323 324 325 // ECMA-262, section 11.7.3, page 52. 326 function SHR(y) { 327 var x = IS_NUMBER(this) ? this : %ToNumber(this); 328 if (!IS_NUMBER(y)) y = %ToNumber(y); 329 return %NumberShr(x, y); 330 } 331 332 333 334 /* ----------------------------- 335 - - - H e l p e r s - - - 336 ----------------------------- 337 */ 338 339 // ECMA-262, section 11.4.1, page 46. 340 function DELETE(key) { 341 return %DeleteProperty(%ToObject(this), %ToString(key)); 342 } 343 344 345 // ECMA-262, section 11.8.7, page 54. 346 function IN(x) { 347 if (x == null || (!IS_OBJECT(x) && !IS_FUNCTION(x))) { 348 throw %MakeTypeError('invalid_in_operator_use', [this, x]); 349 } 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 // instance of F, and non-zero if not. This makes it possible to avoid 357 // an expensive ToBoolean conversion in the generated code. 358 function INSTANCE_OF(F) { 359 var V = this; 360 if (!IS_FUNCTION(F)) { 361 throw %MakeTypeError('instanceof_function_expected', [V]); 362 } 363 364 // If V is not an object, return false. 365 if (IS_NULL(V) || (!IS_OBJECT(V) && !IS_FUNCTION(V))) { 366 return 1; 367 } 368 369 // Get the prototype of F; if it is not an object, throw an error. 370 var O = F.prototype; 371 if (IS_NULL(O) || (!IS_OBJECT(O) && !IS_FUNCTION(O))) { 372 throw %MakeTypeError('instanceof_nonobject_proto', [O]); 373 } 374 375 // Return whether or not O is in the prototype chain of V. 376 return %IsInPrototypeChain(O, V) ? 0 : 1; 377 } 378 379 380 // Get an array of property keys for the given object. Used in 381 // for-in statements. 382 function GET_KEYS() { 383 return %GetPropertyNames(this); 384 } 385 386 387 // Filter a given key against an object by checking if the object 388 // has a property with the given key; return the key as a string if 389 // it has. Otherwise returns null. Used in for-in statements. 390 function FILTER_KEY(key) { 391 var string = %ToString(key); 392 if (%HasProperty(this, string)) return string; 393 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 function APPLY_PREPARE(args) { 416 var length; 417 // First check whether length is a positive Smi and args is an 418 // array. This is the fast case. If this fails, we do the slow case 419 // that takes care of more eventualities. 420 if (IS_ARRAY(args)) { 421 length = args.length; 422 if (%_IsSmi(length) && length >= 0 && length < 0x800000 && IS_FUNCTION(this)) { 423 return length; 424 } 425 } 426 427 length = (args == null) ? 0 : %ToUint32(args.length); 428 429 // We can handle any number of apply arguments if the stack is 430 // big enough, but sanity check the value to avoid overflow when 431 // multiplying with pointer size. 432 if (length > 0x800000) { 433 throw %MakeRangeError('apply_overflow', [length]); 434 } 435 436 if (!IS_FUNCTION(this)) { 437 throw %MakeTypeError('apply_non_function', [ %ToString(this), typeof this ]); 438 } 439 440 // Make sure the arguments list has the right type. 441 if (args != null && !IS_ARRAY(args) && !IS_ARGUMENTS(args)) { 442 throw %MakeTypeError('apply_wrong_args', []); 443 } 444 445 // Return the length which is the number of arguments to copy to the 446 // stack. It is guaranteed to be a small integer at this point. 447 return length; 448 } 449 450 451 function APPLY_OVERFLOW(length) { 452 throw %MakeRangeError('apply_overflow', [length]); 453 } 454 455 456 // Convert the receiver to an object - forward to ToObject. 457 function TO_OBJECT() { 458 return %ToObject(this); 459 } 460 461 462 // Convert the receiver to a number - forward to ToNumber. 463 function TO_NUMBER() { 464 return %ToNumber(this); 465 } 466 467 468 // Convert the receiver to a string - forward to ToString. 469 function TO_STRING() { 470 return %ToString(this); 471 } 472 473 474 // Specialized version of String.charAt. It assumes string as 475 // the receiver type and that the index is a number. 476 function STRING_CHAR_AT(pos) { 477 var char_code = %_FastCharCodeAt(this, pos); 478 if (!%_IsSmi(char_code)) { 479 return %StringCharAt(this, pos); 480 } 481 return %CharFromCode(char_code); 482 } 483 484 485 /* ------------------------------------- 486 - - - C o n v e r s i o n s - - - 487 ------------------------------------- 488 */ 489 490 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, 491 // (1) for number hint, and (2) for string hint. 492 function ToPrimitive(x, hint) { 493 // Fast case check. 494 if (IS_STRING(x)) return x; 495 // Normal behavior. 496 if (!IS_OBJECT(x) && !IS_FUNCTION(x)) return x; 497 if (x == null) return x; // check for null, undefined 498 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; 499 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x); 500 } 501 502 503 // ECMA-262, section 9.2, page 30 504 function ToBoolean(x) { 505 if (IS_BOOLEAN(x)) return x; 506 if (IS_STRING(x)) return x.length != 0; 507 if (x == null) return false; 508 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); 509 return true; 510 } 511 512 513 // ECMA-262, section 9.3, page 31. 514 function ToNumber(x) { 515 if (IS_NUMBER(x)) return x; 516 if (IS_STRING(x)) return %StringToNumber(x); 517 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 // ECMA-262, section 9.8, page 35. 524 function ToString(x) { 525 if (IS_STRING(x)) return x; 526 if (IS_NUMBER(x)) return %_NumberToString(x); 527 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; 528 if (IS_UNDEFINED(x)) return 'undefined'; 529 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); 530 } 531 532 function NonStringToString(x) { 533 if (IS_NUMBER(x)) return %NumberToString(x); 534 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; 535 if (IS_UNDEFINED(x)) return 'undefined'; 536 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); 537 } 538 539 540 // ECMA-262, section 9.9, page 36. 541 function ToObject(x) { 542 if (IS_STRING(x)) return new $String(x); 543 if (IS_NUMBER(x)) return new $Number(x); 544 if (IS_BOOLEAN(x)) return new $Boolean(x); 545 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { 546 throw %MakeTypeError('null_to_object', []); 547 } 548 return x; 549 } 550 551 552 // ECMA-262, section 9.4, page 34. 553 function ToInteger(x) { 554 if (%_IsSmi(x)) return x; 555 return %NumberToInteger(ToNumber(x)); 556 } 557 558 559 // ECMA-262, section 9.6, page 34. 560 function ToUint32(x) { 561 if (%_IsSmi(x) && x >= 0) return x; 562 return %NumberToJSUint32(ToNumber(x)); 563 } 564 565 566 // ECMA-262, section 9.5, page 34 567 function ToInt32(x) { 568 if (%_IsSmi(x)) return x; 569 return %NumberToJSInt32(ToNumber(x)); 570 } 571 572 573 // ES5, section 9.12 574 function SameValue(x, y) { 575 if (typeof x != typeof y) return false; 576 if (IS_NULL_OR_UNDEFINED(x)) return true; 577 if (IS_NUMBER(x)) { 578 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; 579 // x is +0 and y is -0 or vice versa 580 if (x === 0 && y === 0 && !%_IsSmi(x) && !%_IsSmi(y) && 581 ((1 / x < 0 && 1 / y > 0) || (1 / x > 0 && 1 / y < 0))) { 582 return false; 583 } 584 return x == y; 585 } 586 if (IS_STRING(x)) return %StringEquals(x, y); 587 if (IS_BOOLEAN(x))return %NumberEquals(%ToNumber(x),%ToNumber(y)); 588 589 return %_ObjectEquals(x, y); 590 } 591 592 593 /* --------------------------------- 594 - - - U t i l i t i e s - - - 595 --------------------------------- 596 */ 597 598 // Returns if the given x is a primitive value - not an object or a 599 // function. 600 function IsPrimitive(x) { 601 if (!IS_OBJECT(x) && !IS_FUNCTION(x)) { 602 return true; 603 } else { 604 // Even though the type of null is "object", null is still 605 // considered a primitive value. 606 return IS_NULL(x); 607 } 608 } 609 610 611 // ECMA-262, section 8.6.2.6, page 28. 612 function DefaultNumber(x) { 613 if (IS_FUNCTION(x.valueOf)) { 614 var v = x.valueOf(); 615 if (%IsPrimitive(v)) return v; 616 } 617 618 if (IS_FUNCTION(x.toString)) { 619 var s = x.toString(); 620 if (%IsPrimitive(s)) return s; 621 } 622 623 throw %MakeTypeError('cannot_convert_to_primitive', []); 624 } 625 626 627 // ECMA-262, section 8.6.2.6, page 28. 628 function DefaultString(x) { 629 if (IS_FUNCTION(x.toString)) { 630 var s = x.toString(); 631 if (%IsPrimitive(s)) return s; 632 } 633 634 if (IS_FUNCTION(x.valueOf)) { 635 var v = x.valueOf(); 636 if (%IsPrimitive(v)) return v; 637 } 638 639 throw %MakeTypeError('cannot_convert_to_primitive', []); 640 } 641 642 643 // NOTE: Setting the prototype for Array must take place as early as 644 // possible due to code generation for array literals. When 645 // generating code for a array literal a boilerplate array is created 646 // that is cloned when running the code. It is essiential that the 647 // boilerplate gets the right prototype. 648 %FunctionSetPrototype($Array, new $Array(0)); 649