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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 } else if (IS_STRING(x)) { | 68 } else if (IS_STRING(x)) { |
69 while (true) { | 69 while (true) { |
70 if (IS_STRING(y)) return %StringEquals(x, y); | 70 if (IS_STRING(y)) return %StringEquals(x, y); |
71 if (IS_SYMBOL(y)) return 1; // not equal | 71 if (IS_SYMBOL(y)) return 1; // not equal |
72 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); | 72 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); |
73 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); | 73 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); |
74 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 74 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
75 y = %ToPrimitive(y, NO_HINT); | 75 y = %ToPrimitive(y, NO_HINT); |
76 } | 76 } |
77 } else if (IS_SYMBOL(x)) { | 77 } else if (IS_SYMBOL(x)) { |
78 while (true) { | 78 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
79 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1; | 79 return 1; // not equal |
80 if (!IS_SPEC_OBJECT(y)) return 1; // not equal | |
81 y = %ToPrimitive(y, NO_HINT); | |
82 } | |
83 } else if (IS_BOOLEAN(x)) { | 80 } else if (IS_BOOLEAN(x)) { |
84 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; | 81 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
85 if (IS_NULL_OR_UNDEFINED(y)) return 1; | 82 if (IS_NULL_OR_UNDEFINED(y)) return 1; |
86 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); | 83 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); |
87 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); | 84 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); |
88 if (IS_SYMBOL(y)) return 1; // not equal | 85 if (IS_SYMBOL(y)) return 1; // not equal |
89 // y is object. | 86 // y is object. |
90 x = %ToNumber(x); | 87 x = %ToNumber(x); |
91 y = %ToPrimitive(y, NO_HINT); | 88 y = %ToPrimitive(y, NO_HINT); |
92 } else if (IS_NULL_OR_UNDEFINED(x)) { | 89 } else if (IS_NULL_OR_UNDEFINED(x)) { |
93 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; | 90 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; |
94 } else { | 91 } else { |
95 // x is an object. | 92 // x is an object. |
96 if (IS_SPEC_OBJECT(y)) { | 93 if (IS_SPEC_OBJECT(y)) { |
97 return %_ObjectEquals(x, y) ? 0 : 1; | 94 return %_ObjectEquals(x, y) ? 0 : 1; |
98 } | 95 } |
99 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal | 96 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
| 97 if (IS_SYMBOL(y)) return 1; // not equal |
100 if (IS_BOOLEAN(y)) y = %ToNumber(y); | 98 if (IS_BOOLEAN(y)) y = %ToNumber(y); |
101 x = %ToPrimitive(x, NO_HINT); | 99 x = %ToPrimitive(x, NO_HINT); |
102 } | 100 } |
103 } | 101 } |
104 } | 102 } |
105 | 103 |
106 // ECMA-262, section 11.9.4, page 56. | 104 // ECMA-262, section 11.9.4, page 56. |
107 function STRICT_EQUALS(x) { | 105 function STRICT_EQUALS(x) { |
108 if (IS_STRING(this)) { | 106 if (IS_STRING(this)) { |
109 if (!IS_STRING(x)) return 1; // not equal | 107 if (!IS_STRING(x)) return 1; // not equal |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 ------------------------------------- | 492 ------------------------------------- |
495 */ | 493 */ |
496 | 494 |
497 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, | 495 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, |
498 // (1) for number hint, and (2) for string hint. | 496 // (1) for number hint, and (2) for string hint. |
499 function ToPrimitive(x, hint) { | 497 function ToPrimitive(x, hint) { |
500 // Fast case check. | 498 // Fast case check. |
501 if (IS_STRING(x)) return x; | 499 if (IS_STRING(x)) return x; |
502 // Normal behavior. | 500 // Normal behavior. |
503 if (!IS_SPEC_OBJECT(x)) return x; | 501 if (!IS_SPEC_OBJECT(x)) return x; |
504 if (IS_SYMBOL_WRAPPER(x)) return %_ValueOf(x); | 502 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []); |
505 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; | 503 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; |
506 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x); | 504 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x); |
507 } | 505 } |
508 | 506 |
509 | 507 |
510 // ECMA-262, section 9.2, page 30 | 508 // ECMA-262, section 9.2, page 30 |
511 function ToBoolean(x) { | 509 function ToBoolean(x) { |
512 if (IS_BOOLEAN(x)) return x; | 510 if (IS_BOOLEAN(x)) return x; |
513 if (IS_STRING(x)) return x.length != 0; | 511 if (IS_STRING(x)) return x.length != 0; |
514 if (x == null) return false; | 512 if (x == null) return false; |
(...skipping 26 matching lines...) Expand all Loading... |
541 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x)); | 539 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x)); |
542 } | 540 } |
543 | 541 |
544 | 542 |
545 // ECMA-262, section 9.8, page 35. | 543 // ECMA-262, section 9.8, page 35. |
546 function ToString(x) { | 544 function ToString(x) { |
547 if (IS_STRING(x)) return x; | 545 if (IS_STRING(x)) return x; |
548 if (IS_NUMBER(x)) return %_NumberToString(x); | 546 if (IS_NUMBER(x)) return %_NumberToString(x); |
549 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 547 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
550 if (IS_UNDEFINED(x)) return 'undefined'; | 548 if (IS_UNDEFINED(x)) return 'undefined'; |
| 549 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []); |
551 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); | 550 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); |
552 } | 551 } |
553 | 552 |
554 function NonStringToString(x) { | 553 function NonStringToString(x) { |
555 if (IS_NUMBER(x)) return %_NumberToString(x); | 554 if (IS_NUMBER(x)) return %_NumberToString(x); |
556 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 555 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
557 if (IS_UNDEFINED(x)) return 'undefined'; | 556 if (IS_UNDEFINED(x)) return 'undefined'; |
| 557 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []); |
558 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); | 558 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); |
559 } | 559 } |
560 | 560 |
561 | 561 |
562 // ES6 symbols | 562 // ES6 symbols |
563 function ToName(x) { | 563 function ToName(x) { |
564 return IS_SYMBOL(x) ? x : %ToString(x); | 564 return IS_SYMBOL(x) ? x : %ToString(x); |
565 } | 565 } |
566 | 566 |
567 | 567 |
568 // ECMA-262, section 9.9, page 36. | 568 // ECMA-262, section 9.9, page 36. |
569 function ToObject(x) { | 569 function ToObject(x) { |
570 if (IS_STRING(x)) return new $String(x); | 570 if (IS_STRING(x)) return new $String(x); |
571 if (IS_SYMBOL(x)) return new $Symbol(x); | |
572 if (IS_NUMBER(x)) return new $Number(x); | 571 if (IS_NUMBER(x)) return new $Number(x); |
573 if (IS_BOOLEAN(x)) return new $Boolean(x); | 572 if (IS_BOOLEAN(x)) return new $Boolean(x); |
| 573 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x); |
574 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { | 574 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { |
575 throw %MakeTypeError('undefined_or_null_to_object', []); | 575 throw %MakeTypeError('undefined_or_null_to_object', []); |
576 } | 576 } |
577 return x; | 577 return x; |
578 } | 578 } |
579 | 579 |
580 | 580 |
581 // ECMA-262, section 9.4, page 34. | 581 // ECMA-262, section 9.4, page 34. |
582 function ToInteger(x) { | 582 function ToInteger(x) { |
583 if (%_IsSmi(x)) return x; | 583 if (%_IsSmi(x)) return x; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 return i; | 668 return i; |
669 } | 669 } |
670 | 670 |
671 | 671 |
672 // NOTE: Setting the prototype for Array must take place as early as | 672 // NOTE: Setting the prototype for Array must take place as early as |
673 // possible due to code generation for array literals. When | 673 // possible due to code generation for array literals. When |
674 // generating code for a array literal a boilerplate array is created | 674 // generating code for a array literal a boilerplate array is created |
675 // that is cloned when running the code. It is essential that the | 675 // that is cloned when running the code. It is essential that the |
676 // boilerplate gets the right prototype. | 676 // boilerplate gets the right prototype. |
677 %FunctionSetPrototype($Array, new $Array(0)); | 677 %FunctionSetPrototype($Array, new $Array(0)); |
OLD | NEW |