object-is-stricteq.js (4046B)
1 // Test when Object.is() is inlined as JSOP_STRICTEQ 2 3 function SameValue(x, y) { 4 if (x === y) { 5 return (x !== 0) || (1 / x === 1 / y); 6 } 7 return (x !== x && y !== y); 8 } 9 10 var compareTemplate = function compare(name, xs, ys) { 11 // Compare each entry in xs with each entry in ys and ensure Object.is 12 // computes the same result as SameValue. 13 for (var i = 0; i < 1000; ++i) { 14 var xi = (i % xs.length) | 0; 15 var yi = ((i + ((i / ys.length) | 0)) % ys.length) | 0; 16 17 assertEq(Object.is(xs[xi], ys[yi]), SameValue(xs[xi], ys[yi]), name); 18 } 19 } 20 21 const objects = { 22 plain: {}, 23 function: function(){}, 24 proxy: new Proxy({}, {}), 25 }; 26 27 const sym = Symbol(); 28 29 const testCases = [ 30 { 31 name: "Homogenous-Int32", 32 xs: [-1, 0, 1, 2, 0x7fffffff], 33 ys: [2, 1, 0, -5, -0x80000000], 34 }, 35 { 36 name: "Homogenous-Boolean", 37 xs: [true, false], 38 ys: [true, false], 39 }, 40 { 41 name: "Homogenous-Object", 42 xs: [{}, [], objects.plain, objects.proxy], 43 ys: [{}, objects.function, objects.plain, objects.proxy], 44 }, 45 { 46 name: "Homogenous-String", 47 xs: ["", "abc", "αβγαβγ", "𝐀𝐁𝐂𝐀𝐁𝐂", "ABCabc"], 48 ys: ["abc", "ABC", "ABCABC", "αβγαβγ", "𝐀𝐁𝐂𝐀𝐁𝐂"], 49 }, 50 { 51 name: "Homogenous-Symbol", 52 xs: [Symbol.iterator, Symbol(), Symbol.for("object-is"), sym], 53 ys: [sym, Symbol.match, Symbol(), Symbol.for("object-is-two")], 54 }, 55 { 56 name: "Homogenous-Null", 57 xs: [null, null], 58 ys: [null, null], 59 }, 60 { 61 name: "Homogenous-Undefined", 62 xs: [undefined, undefined], 63 ys: [undefined, undefined], 64 }, 65 66 // Note: Values must not include floating-point types! 67 { 68 name: "String-Value", 69 xs: ["", "abc", "αβγαβγ", "𝐀𝐁𝐂𝐀𝐁𝐂"], 70 ys: [null, undefined, sym, true, 0, "", {}], 71 }, 72 { 73 name: "Null-Value", 74 xs: [null, null], 75 ys: [null, undefined, sym, true, 0, "", {}], 76 }, 77 { 78 name: "Undefined-Value", 79 xs: [undefined, undefined], 80 ys: [null, undefined, sym, true, 0, "", {}], 81 }, 82 { 83 name: "Boolean-Value", 84 xs: [true, false], 85 ys: [null, undefined, sym, true, 0, "", {}], 86 }, 87 88 // Note: Values must not include floating-point types! 89 { 90 name: "Value-String", 91 xs: [null, undefined, sym, true, 0, "", {}], 92 ys: ["", "abc", "αβγαβγ", "𝐀𝐁𝐂𝐀𝐁𝐂"], 93 }, 94 { 95 name: "Value-Null", 96 xs: [null, undefined, sym, true, 0, "", {}], 97 ys: [null, null], 98 }, 99 { 100 name: "Value-Undefined", 101 xs: [null, undefined, sym, true, 0, "", {}], 102 ys: [undefined, undefined], 103 }, 104 { 105 name: "Value-Boolean", 106 xs: [null, undefined, sym, true, 0, "", {}], 107 ys: [undefined, undefined], 108 }, 109 110 // Strict-equal comparison can be optimized to bitwise comparison when 111 // string types are not possible. 112 // Note: Values must not include floating-point types! 113 { 114 name: "Value-Value", 115 xs: [null, undefined, sym, true, 0, {}], 116 ys: [null, undefined, sym, true, 0, {}], 117 }, 118 { 119 name: "ValueMaybeString-ValueMaybeString", 120 xs: [null, undefined, sym, true, 0, "", {}], 121 ys: [null, undefined, sym, true, 0, "", {}], 122 }, 123 { 124 name: "Value-ValueMaybeString", 125 xs: [null, undefined, sym, true, 0, {}], 126 ys: [null, undefined, sym, true, 0, "", {}], 127 }, 128 { 129 name: "ValueMaybeString-Value", 130 xs: [null, undefined, sym, true, 0, "", {}], 131 ys: [null, undefined, sym, true, 0, {}], 132 }, 133 ]; 134 135 for (let {name, xs, ys} of testCases) { 136 // Create a separate function for each test case. 137 // Use indirect eval to avoid possible direct eval deopts. 138 const compare = (0, eval)(`(${compareTemplate})`); 139 140 for (let i = 0; i < 5; ++i) { 141 compare(name, xs, ys); 142 } 143 }