| OLD | NEW |
| (Empty) | |
| 1 // Copyright 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 // Flags: --allow-natives-syntax |
| 29 |
| 30 function IS_STRING(arg) { return (typeof(arg) === 'string'); } |
| 31 function IS_NUMBER(arg) { return (typeof(arg) === 'number'); } |
| 32 function IS_BOOLEAN(arg) { return (typeof(arg) === 'boolean'); } |
| 33 function IS_OBJECT(arg) { return %_IsObject(arg); } |
| 34 function IS_FUNCTION(arg) { return %_IsFunction(arg); } |
| 35 const NO_HINT = 0; |
| 36 |
| 37 // ECMA-262, section 11.9.1, page 55. |
| 38 function EQUALS(x, y) { |
| 39 if (IS_STRING(x) && IS_STRING(y)) return %StringEquals(x, y); |
| 40 |
| 41 // NOTE: We use iteration instead of recursion, because it is |
| 42 // difficult to call EQUALS with the correct setting of 'this' in |
| 43 // an efficient way. |
| 44 while (true) { |
| 45 if (IS_NUMBER(x)) { |
| 46 if (y == null) return 1; // not equal |
| 47 return %NumberEquals(x, %ToNumber(y)); |
| 48 } else if (IS_STRING(x)) { |
| 49 if (IS_STRING(y)) return %StringEquals(x, y); |
| 50 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); |
| 51 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); |
| 52 if (y == null) return 1; // not equal |
| 53 y = %ToPrimitive(y, NO_HINT); |
| 54 } else if (IS_BOOLEAN(x)) { |
| 55 if (IS_BOOLEAN(y)) { |
| 56 return %_ObjectEquals(x, y) ? 0 : 1; |
| 57 } |
| 58 if (y == null) return 1; // not equal |
| 59 return %NumberEquals(%ToNumber(x), %ToNumber(y)); |
| 60 } else if (x == null) { |
| 61 // NOTE: This checks for both null and undefined. |
| 62 return (y == null) ? 0 : 1; |
| 63 } else { |
| 64 // x is not a number, boolean, null or undefined. |
| 65 if (y == null) return 1; // not equal |
| 66 if (IS_OBJECT(y)) { |
| 67 return %_ObjectEquals(x, y) ? 0 : 1; |
| 68 } |
| 69 if (IS_FUNCTION(y)) { |
| 70 return %_ObjectEquals(x, y) ? 0 : 1; |
| 71 } |
| 72 |
| 73 x = %ToPrimitive(x, NO_HINT); |
| 74 } |
| 75 } |
| 76 } |
| 77 |
| 78 var values = [ |
| 79 0, |
| 80 1, |
| 81 -1, |
| 82 NaN, |
| 83 function() {return 0;}, |
| 84 function() {return 1;}, |
| 85 {}, |
| 86 'a', |
| 87 'b', |
| 88 'c', |
| 89 {toString: function(){return 'a'}}, |
| 90 {valueOf: function(){return 1;}}, |
| 91 {valueOf: function(){return 'b'}}, |
| 92 [1, 2, 3], |
| 93 ['1', '2', '3'], |
| 94 {a: 'b'}, |
| 95 1.126, |
| 96 void 0, |
| 97 null, |
| 98 undefined, |
| 99 100000000000000000, |
| 100 '1.126' |
| 101 ]; |
| 102 |
| 103 for (var i = 0; i < values.length; i++) { |
| 104 for (var j = 0; j < values.length; j++) { |
| 105 var x = values[i]; |
| 106 var y = values[j]; |
| 107 |
| 108 var r1 = EQUALS(x, y); // Reference implementation. |
| 109 var r2 = (x == y) ? 0 : 1; |
| 110 |
| 111 assertEquals(r1, r2, 'Compared ' + x + ' vs ' + y); |
| 112 } |
| 113 } |
| 114 |
| OLD | NEW |