OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 // Fast case check. | 499 // Fast case check. |
500 if (IS_STRING(x)) return x; | 500 if (IS_STRING(x)) return x; |
501 // Normal behavior. | 501 // Normal behavior. |
502 if (!IS_OBJECT(x) && !IS_FUNCTION(x)) return x; | 502 if (!IS_OBJECT(x) && !IS_FUNCTION(x)) return x; |
503 if (x == null) return x; // check for null, undefined | 503 if (x == null) return x; // check for null, undefined |
504 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; | 504 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; |
505 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x); | 505 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x); |
506 } | 506 } |
507 | 507 |
508 | 508 |
| 509 // ECMA-262, section 9.2, page 30 |
| 510 function ToBoolean(x) { |
| 511 if (IS_BOOLEAN(x)) return x; |
| 512 if (IS_STRING(x)) return x.length != 0; |
| 513 if (x == null) return false; |
| 514 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); |
| 515 return true; |
| 516 } |
| 517 |
| 518 |
509 // ECMA-262, section 9.3, page 31. | 519 // ECMA-262, section 9.3, page 31. |
510 function ToNumber(x) { | 520 function ToNumber(x) { |
511 if (IS_NUMBER(x)) return x; | 521 if (IS_NUMBER(x)) return x; |
512 if (IS_STRING(x)) return %StringToNumber(x); | 522 if (IS_STRING(x)) return %StringToNumber(x); |
513 if (IS_BOOLEAN(x)) return x ? 1 : 0; | 523 if (IS_BOOLEAN(x)) return x ? 1 : 0; |
514 if (IS_UNDEFINED(x)) return $NaN; | 524 if (IS_UNDEFINED(x)) return $NaN; |
515 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x)); | 525 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x)); |
516 } | 526 } |
517 | 527 |
518 | 528 |
519 // ECMA-262, section 9.8, page 35. | 529 // ECMA-262, section 9.8, page 35. |
520 function ToString(x) { | 530 function ToString(x) { |
521 if (IS_STRING(x)) return x; | 531 if (IS_STRING(x)) return x; |
522 if (IS_NUMBER(x)) return %NumberToString(x); | 532 if (IS_NUMBER(x)) return %NumberToString(x); |
523 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 533 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
524 if (IS_UNDEFINED(x)) return 'undefined'; | 534 if (IS_UNDEFINED(x)) return 'undefined'; |
525 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); | 535 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); |
526 } | 536 } |
527 | 537 |
528 | 538 |
529 // ... where did this come from? | |
530 function ToBoolean(x) { | |
531 if (IS_BOOLEAN(x)) return x; | |
532 if (IS_STRING(x)) return x.length != 0; | |
533 if (x == null) return false; | |
534 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); | |
535 return true; | |
536 } | |
537 | |
538 | |
539 // ECMA-262, section 9.9, page 36. | 539 // ECMA-262, section 9.9, page 36. |
540 function ToObject(x) { | 540 function ToObject(x) { |
541 if (IS_STRING(x)) return new $String(x); | 541 if (IS_STRING(x)) return new $String(x); |
542 if (IS_NUMBER(x)) return new $Number(x); | 542 if (IS_NUMBER(x)) return new $Number(x); |
543 if (IS_BOOLEAN(x)) return new $Boolean(x); | 543 if (IS_BOOLEAN(x)) return new $Boolean(x); |
544 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { | 544 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { |
545 throw %MakeTypeError('null_to_object', []); | 545 throw %MakeTypeError('null_to_object', []); |
546 } | 546 } |
547 return x; | 547 return x; |
548 } | 548 } |
(...skipping 13 matching lines...) Expand all Loading... |
562 } | 562 } |
563 | 563 |
564 | 564 |
565 // ECMA-262, section 9.5, page 34 | 565 // ECMA-262, section 9.5, page 34 |
566 function ToInt32(x) { | 566 function ToInt32(x) { |
567 if (%_IsSmi(x)) return x; | 567 if (%_IsSmi(x)) return x; |
568 return %NumberToJSInt32(ToNumber(x)); | 568 return %NumberToJSInt32(ToNumber(x)); |
569 } | 569 } |
570 | 570 |
571 | 571 |
| 572 // ES5, section 9.12 |
| 573 function SameValue(x, y) { |
| 574 if (typeof x != typeof y) return false; |
| 575 if (IS_NULL_OR_UNDEFINED(x)) return true; |
| 576 if (IS_NUMBER(x)) { |
| 577 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; |
| 578 // x is +0 and y is -0 or vice versa |
| 579 if (x === 0 && y === 0 && !%_IsSmi(x) && !%_IsSmi(y) && |
| 580 ((1 / x < 0 && 1 / y > 0) || (1 / x > 0 && 1 / y < 0))) { |
| 581 return false; |
| 582 } |
| 583 return x == y; |
| 584 } |
| 585 if (IS_STRING(x)) return %StringEquals(x, y); |
| 586 if (IS_BOOLEAN(x))return %NumberEquals(%ToNumber(x),%ToNumber(y)); |
| 587 |
| 588 return %_ObjectEquals(x, y); |
| 589 } |
| 590 |
572 | 591 |
573 /* --------------------------------- | 592 /* --------------------------------- |
574 - - - U t i l i t i e s - - - | 593 - - - U t i l i t i e s - - - |
575 --------------------------------- | 594 --------------------------------- |
576 */ | 595 */ |
577 | 596 |
578 // Returns if the given x is a primitive value - not an object or a | 597 // Returns if the given x is a primitive value - not an object or a |
579 // function. | 598 // function. |
580 function IsPrimitive(x) { | 599 function IsPrimitive(x) { |
581 if (!IS_OBJECT(x) && !IS_FUNCTION(x)) { | 600 if (!IS_OBJECT(x) && !IS_FUNCTION(x)) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 throw %MakeTypeError('cannot_convert_to_primitive', []); | 638 throw %MakeTypeError('cannot_convert_to_primitive', []); |
620 } | 639 } |
621 | 640 |
622 | 641 |
623 // NOTE: Setting the prototype for Array must take place as early as | 642 // NOTE: Setting the prototype for Array must take place as early as |
624 // possible due to code generation for array literals. When | 643 // possible due to code generation for array literals. When |
625 // generating code for a array literal a boilerplate array is created | 644 // generating code for a array literal a boilerplate array is created |
626 // that is cloned when running the code. It is essiential that the | 645 // that is cloned when running the code. It is essiential that the |
627 // boilerplate gets the right prototype. | 646 // boilerplate gets the right prototype. |
628 %FunctionSetPrototype($Array, new $Array(0)); | 647 %FunctionSetPrototype($Array, new $Array(0)); |
OLD | NEW |