| 1 |
// Test case |typeof| folding in simple comparison contexts. |
| 2 |
|
| 3 |
// Create functions to test if |typeof x| is equal to a constant string. |
| 4 |
// - Covers all possible |typeof| results, plus the invalid string "bad". |
| 5 |
// - Covers all four possible equality comparison operators. |
| 6 |
function createFunctions() { |
| 7 |
return [ |
| 8 |
"undefined", |
| 9 |
"object", |
| 10 |
"function", |
| 11 |
"string", |
| 12 |
"number", |
| 13 |
"boolean", |
| 14 |
"symbol", |
| 15 |
"bigint", |
| 16 |
"bad", |
| 17 |
].flatMap(type => [ |
| 18 |
"==", |
| 19 |
"===", |
| 20 |
"!=", |
| 21 |
"!==" |
| 22 |
].map(op => ({ |
| 23 |
type, |
| 24 |
equal: op[0] === "=", |
| 25 |
fn: Function("thing", `return typeof thing ${op} "${type}"`) |
| 26 |
}))); |
| 27 |
} |
| 28 |
|
| 29 |
let functions = createFunctions(); |
| 30 |
|
| 31 |
function test() { |
| 32 |
const ccwGlobal = newGlobal({newCompartment: true}); |
| 33 |
const xs = [ |
| 34 |
// "undefined" |
| 35 |
// Plain undefined and object emulating undefined, including various proxy wrapper cases. |
| 36 |
undefined, |
| 37 |
createIsHTMLDDA(), |
| 38 |
wrapWithProto(createIsHTMLDDA(), null), |
| 39 |
ccwGlobal.eval("createIsHTMLDDA()"), |
| 40 |
|
| 41 |
// "object" |
| 42 |
// Plain object and various proxy wrapper cases. |
| 43 |
{}, |
| 44 |
this, |
| 45 |
new Proxy({}, {}), |
| 46 |
wrapWithProto({}, null), |
| 47 |
transplantableObject({proxy: true}).object, |
| 48 |
ccwGlobal.Object(), |
| 49 |
|
| 50 |
// "function" |
| 51 |
// Plain function and various proxy wrapper cases. |
| 52 |
function(){}, |
| 53 |
new Proxy(function(){}, {}), |
| 54 |
new Proxy(createIsHTMLDDA(), {}), |
| 55 |
wrapWithProto(function(){}, null), |
| 56 |
ccwGlobal.Function(), |
| 57 |
|
| 58 |
// "string" |
| 59 |
"", |
| 60 |
|
| 61 |
// "number" |
| 62 |
// Int32 and Double numbers. |
| 63 |
0, |
| 64 |
1.23, |
| 65 |
|
| 66 |
// "boolean" |
| 67 |
true, |
| 68 |
|
| 69 |
// "symbol" |
| 70 |
Symbol(), |
| 71 |
|
| 72 |
// "bigint" |
| 73 |
0n, |
| 74 |
]; |
| 75 |
|
| 76 |
for (let i = 0; i < 200; ++i) { |
| 77 |
let x = xs[i % xs.length]; |
| 78 |
for (let {type, equal, fn} of functions) { |
| 79 |
assertEq(fn(x), (typeof x === type) === equal); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
for (let i = 0; i < 2; ++i) test(); |
| 84 |
|
| 85 |
// Fresh set of functions to gather new type info. |
| 86 |
let functionsObject = createFunctions(); |
| 87 |
|
| 88 |
// Additional test when the input is always an object. |
| 89 |
function testObject() { |
| 90 |
const ccwGlobal = newGlobal({newCompartment: true}); |
| 91 |
|
| 92 |
// All elements of this array are objects to cover the case when |MTypeOf| has |
| 93 |
// a |MIRType::Object| input. |
| 94 |
const xs = [ |
| 95 |
// "undefined" |
| 96 |
// Object emulating undefined, including various proxy wrapper cases. |
| 97 |
createIsHTMLDDA(), |
| 98 |
wrapWithProto(createIsHTMLDDA(), null), |
| 99 |
ccwGlobal.eval("createIsHTMLDDA()"), |
| 100 |
|
| 101 |
// "object" |
| 102 |
// Plain object and various proxy wrapper cases. |
| 103 |
{}, |
| 104 |
this, |
| 105 |
new Proxy({}, {}), |
| 106 |
wrapWithProto({}, null), |
| 107 |
transplantableObject({proxy: true}).object, |
| 108 |
ccwGlobal.Object(), |
| 109 |
|
| 110 |
// "function" |
| 111 |
// Plain function and various proxy wrapper cases. |
| 112 |
function(){}, |
| 113 |
new Proxy(function(){}, {}), |
| 114 |
new Proxy(createIsHTMLDDA(), {}), |
| 115 |
wrapWithProto(function(){}, null), |
| 116 |
ccwGlobal.Function(), |
| 117 |
]; |
| 118 |
|
| 119 |
for (let i = 0; i < 200; ++i) { |
| 120 |
let x = xs[i % xs.length]; |
| 121 |
for (let {type, equal, fn} of functionsObject) { |
| 122 |
assertEq(fn(x), (typeof x === type) === equal); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
for (let i = 0; i < 2; ++i) testObject(); |