|
| 1 | +function shouldBe(actual, expected, string, atom) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error("bad value: " + actual + " expected " + expected + " for " + JSON.stringify(string) + " === " + JSON.stringify(atom)); |
| 4 | +} |
| 5 | + |
| 6 | +const atoms = [ |
| 7 | + "", "a", "if", "var", "this", "while", "return", "default", "function", |
| 8 | + "abcdefghi", "instanceof", "abcdefghijk", "abcdefghijkl", "abcdefghijklm", |
| 9 | + "abcdefghijklmn", "abcdefghijklmno", "abcdefghijklmnop", "abcdefghijklmnopq", |
| 10 | +]; |
| 11 | + |
| 12 | +function test0(s) { return s === ""; } |
| 13 | +function test1(s) { return s === "a"; } |
| 14 | +function test2(s) { return s === "if"; } |
| 15 | +function test3(s) { return s === "var"; } |
| 16 | +function test4(s) { return s === "this"; } |
| 17 | +function test5(s) { return s === "while"; } |
| 18 | +function test6(s) { return s === "return"; } |
| 19 | +function test7(s) { return s === "default"; } |
| 20 | +function test8(s) { return s === "function"; } |
| 21 | +function test9(s) { return "abcdefghi" === s; } |
| 22 | +function test10(s) { return s === "instanceof"; } |
| 23 | +function test11(s) { return s === "abcdefghijk"; } |
| 24 | +function test12(s) { return s === "abcdefghijkl"; } |
| 25 | +function test13(s) { return s === "abcdefghijklm"; } |
| 26 | +function test14(s) { return s === "abcdefghijklmn"; } |
| 27 | +function test15(s) { return s === "abcdefghijklmno"; } |
| 28 | +function test16(s) { return s === "abcdefghijklmnop"; } |
| 29 | +function test17(s) { return s === "abcdefghijklmnopq"; } |
| 30 | +const tests = [test0, test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15, test16, test17]; |
| 31 | +for (const test of tests) |
| 32 | + noInline(test); |
| 33 | + |
| 34 | +function keyword(id) { |
| 35 | + switch (id.length) { |
| 36 | + case 5: |
| 37 | + return id === "while" || id === "break" || id === "catch" || id === "throw" || id === "const"; |
| 38 | + default: |
| 39 | + return false; |
| 40 | + } |
| 41 | +} |
| 42 | +noInline(keyword); |
| 43 | + |
| 44 | +function flatten(string) { |
| 45 | + string.indexOf("#"); |
| 46 | + return string; |
| 47 | +} |
| 48 | + |
| 49 | +const source8 = flatten(" " + atoms.join(" ") + " "); |
| 50 | +const source16 = flatten("\u3042" + source8); |
| 51 | + |
| 52 | +function candidates(atom) { |
| 53 | + const result = []; |
| 54 | + for (const source of [source8, source16]) { |
| 55 | + const index = source.indexOf(" " + atom + " ") + 1; |
| 56 | + result.push([source.slice(index, index + atom.length), true]); |
| 57 | + } |
| 58 | + result.push([atom.split("").join(""), true]); |
| 59 | + if (atom.length) { |
| 60 | + const half = atom.length >> 1; |
| 61 | + result.push([atom.slice(0, half) + atom.slice(half), true]); |
| 62 | + result.push([flatten("\u3042" + atom + " ").slice(1, 1 + atom.length), true]); |
| 63 | + for (let i = 0; i < atom.length; ++i) { |
| 64 | + const mutated = atom.slice(0, i) + String.fromCharCode(atom.charCodeAt(i) ^ 1) + atom.slice(i + 1); |
| 65 | + result.push([mutated, false]); |
| 66 | + const wide = flatten("\u3042" + mutated + " "); |
| 67 | + result.push([wide.slice(1, 1 + atom.length), false]); |
| 68 | + const narrow = flatten("_" + mutated + " "); |
| 69 | + result.push([narrow.slice(1, 1 + atom.length), false]); |
| 70 | + } |
| 71 | + result.push([atom.slice(1), false]); |
| 72 | + result.push([source8.slice(2, 2 + atom.length + 1), false]); |
| 73 | + } |
| 74 | + result.push([atom + "x", false]); |
| 75 | + result.push([flatten("y" + atom + "x").slice(1), false]); |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +function equalsByCharacter(a, b) { |
| 80 | + if (a.length !== b.length) |
| 81 | + return false; |
| 82 | + for (let i = 0; i < a.length; ++i) { |
| 83 | + if (a.charCodeAt(i) !== b.charCodeAt(i)) |
| 84 | + return false; |
| 85 | + } |
| 86 | + return true; |
| 87 | +} |
| 88 | +noDFG(equalsByCharacter); |
| 89 | + |
| 90 | +const cases = atoms.map(candidates); |
| 91 | +const keywords = ["while", "break", "catch", "throw", "const"]; |
| 92 | +const keywordCandidates = [...keywords, "whilf", "count", "const ", "wh", "xyzzy"]; |
| 93 | +const keywordSource = flatten("." + keywordCandidates.join(".") + "."); |
| 94 | +const keywordSlices = keywordCandidates.map(word => { |
| 95 | + const index = keywordSource.indexOf("." + word + ".") + 1; |
| 96 | + return keywordSource.slice(index, index + word.length); |
| 97 | +}); |
| 98 | + |
| 99 | +function untyped(value) { return value === "while"; } |
| 100 | +noInline(untyped); |
| 101 | +function notDouble(value) { return "abcdefghij" === value; } |
| 102 | +noInline(notDouble); |
| 103 | + |
| 104 | +const whileSource = flatten(".while.whilf."); |
| 105 | +const untypedValues = [ |
| 106 | + [whileSource.slice(1, 6), true], [whileSource.slice(7, 12), false], ["wh" + "ile".split("").join(""), true], |
| 107 | + [flatten("\u3042while").slice(1), true], ["while", true], [42, false], [4.2, false], [undefined, false], |
| 108 | + [null, false], [{}, false], [Symbol(), false], [true, false], [10n, false], |
| 109 | +]; |
| 110 | +const abcSource = flatten(".abcdefghij.abcdefghik."); |
| 111 | +const notDoubleValues = [ |
| 112 | + [abcSource.slice(1, 11), true], [abcSource.slice(12, 22), false], ["abcde" + "fghij".split("").join(""), true], |
| 113 | + [flatten("\u3042abcdefghij").slice(1), true], ["abcdefghij", true], [42, false], [undefined, false], |
| 114 | + [null, false], [{}, false], [Symbol(), false], [false, false], |
| 115 | +]; |
| 116 | + |
| 117 | +for (let iteration = 0; iteration < testLoopCount; ++iteration) { |
| 118 | + for (let i = 0; i < tests.length; ++i) { |
| 119 | + const [string, expected] = cases[i][iteration % cases[i].length]; |
| 120 | + shouldBe(tests[i](string), expected, string, atoms[i]); |
| 121 | + const other = cases[(i + 1) % tests.length]; |
| 122 | + const otherString = other[iteration % other.length][0]; |
| 123 | + shouldBe(tests[i](otherString), equalsByCharacter(otherString, atoms[i]), otherString, atoms[i]); |
| 124 | + } |
| 125 | + const index = iteration % keywordCandidates.length; |
| 126 | + shouldBe(keyword(keywordSlices[index]), keywords.includes(keywordCandidates[index]), keywordSlices[index], "keyword"); |
| 127 | + |
| 128 | + const [untypedValue, untypedExpected] = untypedValues[iteration % untypedValues.length]; |
| 129 | + shouldBe(untyped(untypedValue), untypedExpected, String(untypedValue), "while"); |
| 130 | + const [notDoubleValue, notDoubleExpected] = notDoubleValues[iteration % notDoubleValues.length]; |
| 131 | + shouldBe(notDouble(notDoubleValue), notDoubleExpected, String(notDoubleValue), "abcdefghij"); |
| 132 | +} |
0 commit comments