|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +function shouldBe(actual, expected, message) { |
| 4 | + if (actual !== expected) |
| 5 | + throw new Error(`${message}: expected ${String(expected)} but got ${String(actual)}`); |
| 6 | +} |
| 7 | + |
| 8 | +function shouldThrow(func, errorType, message) { |
| 9 | + let error; |
| 10 | + try { |
| 11 | + func(); |
| 12 | + } catch (e) { |
| 13 | + error = e; |
| 14 | + } |
| 15 | + if (!(error instanceof errorType)) |
| 16 | + throw new Error(`${message}: expected ${errorType.name} but got ${String(error)}`); |
| 17 | +} |
| 18 | + |
| 19 | +function target(a, b) { |
| 20 | + return [this, a, b, arguments.length]; |
| 21 | +} |
| 22 | + |
| 23 | +function callProxy(proxy, thisValue, a, b) { |
| 24 | + const result = proxy.call(thisValue, a, b); |
| 25 | + return result; |
| 26 | +} |
| 27 | +noInline(callProxy); |
| 28 | + |
| 29 | +function callDirect(proxy, a) { |
| 30 | + const result = proxy(a); |
| 31 | + return result; |
| 32 | +} |
| 33 | +noInline(callDirect); |
| 34 | + |
| 35 | +function tailCall(proxy, a) { |
| 36 | + "use strict"; |
| 37 | + return proxy(a); |
| 38 | +} |
| 39 | +noInline(tailCall); |
| 40 | + |
| 41 | +function callMethod(object, a, b) { |
| 42 | + const result = object.method(a, b); |
| 43 | + return result; |
| 44 | +} |
| 45 | +noInline(callMethod); |
| 46 | + |
| 47 | +function construct(proxy, a) { |
| 48 | + return new proxy(a); |
| 49 | +} |
| 50 | +noInline(construct); |
| 51 | + |
| 52 | +{ |
| 53 | + const proxy = new Proxy(target, { }); |
| 54 | + const nested = new Proxy(new Proxy(target, { }), { get() { return 0; } }); |
| 55 | + const thisValue = { }; |
| 56 | + for (let i = 0; i < testLoopCount; ++i) { |
| 57 | + let result = callMethod({ method: proxy }, i, i + 1); |
| 58 | + shouldBe(result[1], i, "first argument"); |
| 59 | + shouldBe(result[2], i + 1, "second argument"); |
| 60 | + shouldBe(result[3], 2, "argument count"); |
| 61 | + result = callDirect(proxy, i); |
| 62 | + shouldBe(result[0], undefined, "this of a plain call"); |
| 63 | + shouldBe(result[2], undefined, "missing argument"); |
| 64 | + shouldBe(result[3], 1, "argument count with missing argument"); |
| 65 | + shouldBe(callProxy(proxy, thisValue, 1, 2)[0], thisValue, "this through Function.prototype.call"); |
| 66 | + result = tailCall(proxy, i); |
| 67 | + shouldBe(result[0], undefined, "this of a tail call"); |
| 68 | + shouldBe(result[3], 1, "argument count of a tail call"); |
| 69 | + shouldBe(tailCall(nested, i)[1], i, "tail call through nested proxies"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +{ |
| 74 | + let applyCount = 0; |
| 75 | + const handler = { }; |
| 76 | + const proxy = new Proxy(target, handler); |
| 77 | + for (let i = 0; i < testLoopCount; ++i) { |
| 78 | + if (i === (testLoopCount >> 1)) |
| 79 | + handler.apply = function (t, thisArgument, args) { ++applyCount; return "trapped"; }; |
| 80 | + let result = callDirect(proxy, i); |
| 81 | + if (i < (testLoopCount >> 1)) |
| 82 | + shouldBe(result[1], i, "before adding the apply trap"); |
| 83 | + else |
| 84 | + shouldBe(result, "trapped", "after adding the apply trap"); |
| 85 | + } |
| 86 | + shouldBe(applyCount, testLoopCount - (testLoopCount >> 1), "apply trap count"); |
| 87 | +} |
| 88 | + |
| 89 | +{ |
| 90 | + const proxy = new Proxy(target, { }); |
| 91 | + for (let i = 0; i < testLoopCount; ++i) { |
| 92 | + if (i === (testLoopCount >> 1)) |
| 93 | + Object.prototype.apply = function () { return "from Object.prototype"; }; |
| 94 | + let result = callDirect(proxy, i); |
| 95 | + if (i < (testLoopCount >> 1)) |
| 96 | + shouldBe(result[1], i, "before adding Object.prototype.apply"); |
| 97 | + else |
| 98 | + shouldBe(result, "from Object.prototype", "after adding Object.prototype.apply"); |
| 99 | + } |
| 100 | + delete Object.prototype.apply; |
| 101 | +} |
| 102 | + |
| 103 | +{ |
| 104 | + let getterCount = 0; |
| 105 | + const handler = { |
| 106 | + get apply() { |
| 107 | + ++getterCount; |
| 108 | + return undefined; |
| 109 | + } |
| 110 | + }; |
| 111 | + const proxy = new Proxy(target, handler); |
| 112 | + for (let i = 0; i < testLoopCount; ++i) |
| 113 | + shouldBe(callDirect(proxy, i)[1], i, "apply getter returning undefined"); |
| 114 | + shouldBe(getterCount, testLoopCount, "apply getter count"); |
| 115 | +} |
| 116 | + |
| 117 | +{ |
| 118 | + const { proxy, revoke } = Proxy.revocable(target, { }); |
| 119 | + for (let i = 0; i < testLoopCount; ++i) { |
| 120 | + if (i === (testLoopCount >> 1)) |
| 121 | + revoke(); |
| 122 | + if (i < (testLoopCount >> 1)) |
| 123 | + shouldBe(callDirect(proxy, i)[1], i, "before revocation"); |
| 124 | + else |
| 125 | + shouldThrow(() => callDirect(proxy, i), TypeError, "after revocation"); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +{ |
| 130 | + class C { constructor(a) { this.a = a; } } |
| 131 | + const proxy = new Proxy(C, { }); |
| 132 | + const nonCallable = new Proxy({ }, { }); |
| 133 | + const native = new Proxy(Math.max, { }); |
| 134 | + const bound = new Proxy(target.bind(null, "bound"), { }); |
| 135 | + for (let i = 0; i < testLoopCount; ++i) { |
| 136 | + shouldThrow(() => callDirect(proxy, i), TypeError, "calling a class constructor"); |
| 137 | + shouldBe(construct(proxy, i).a, i, "constructing through the proxy"); |
| 138 | + shouldThrow(() => callDirect(nonCallable, i), TypeError, "calling a non-callable proxy"); |
| 139 | + shouldBe(callDirect(native, i), i, "native target"); |
| 140 | + let result = callDirect(bound, i); |
| 141 | + shouldBe(result[1], "bound", "bound target first argument"); |
| 142 | + shouldBe(result[2], i, "bound target second argument"); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +{ |
| 147 | + const error = new Error("thrown"); |
| 148 | + const proxy = new Proxy(function () { throw error; }, { }); |
| 149 | + for (let i = 0; i < testLoopCount; ++i) { |
| 150 | + let caught; |
| 151 | + try { |
| 152 | + callDirect(proxy, i); |
| 153 | + } catch (e) { |
| 154 | + caught = e; |
| 155 | + } |
| 156 | + shouldBe(caught, error, "exception from the target"); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function callPolymorphic(callee, a) { |
| 161 | + const result = callee(a); |
| 162 | + return result; |
| 163 | +} |
| 164 | +noInline(callPolymorphic); |
| 165 | + |
| 166 | +function tailCallPolymorphic(callee, a) { |
| 167 | + return callee(a); |
| 168 | +} |
| 169 | +noInline(tailCallPolymorphic); |
| 170 | + |
| 171 | +function callMegamorphic(callee, a) { |
| 172 | + const result = callee(a); |
| 173 | + return result; |
| 174 | +} |
| 175 | +noInline(callMegamorphic); |
| 176 | + |
| 177 | +function tailCallMegamorphic(callee, a) { |
| 178 | + return callee(a); |
| 179 | +} |
| 180 | +noInline(tailCallMegamorphic); |
| 181 | + |
| 182 | +{ |
| 183 | + const functions = []; |
| 184 | + for (let i = 0; i < 4; ++i) |
| 185 | + functions.push(new Function("a", `return a + ${i};`)); |
| 186 | + const handler = { }; |
| 187 | + const proxy = new Proxy(target, handler); |
| 188 | + const internalFunctionProxy = new Proxy(Array, { }); |
| 189 | + for (let i = 0; i < testLoopCount; ++i) { |
| 190 | + for (let j = 0; j < functions.length; ++j) { |
| 191 | + shouldBe(callPolymorphic(functions[j], i), i + j, "polymorphic function"); |
| 192 | + shouldBe(tailCallPolymorphic(functions[j], i), i + j, "polymorphic function tail call"); |
| 193 | + } |
| 194 | + if (i === (testLoopCount >> 1)) |
| 195 | + handler.apply = function () { return "trapped"; }; |
| 196 | + let result = callPolymorphic(proxy, i); |
| 197 | + if (i < (testLoopCount >> 1)) |
| 198 | + shouldBe(result[1], i, "polymorphic proxy before adding the apply trap"); |
| 199 | + else |
| 200 | + shouldBe(result, "trapped", "polymorphic proxy after adding the apply trap"); |
| 201 | + result = tailCallPolymorphic(proxy, i); |
| 202 | + if (i < (testLoopCount >> 1)) |
| 203 | + shouldBe(result[1], i, "polymorphic proxy tail call before adding the apply trap"); |
| 204 | + else |
| 205 | + shouldBe(result, "trapped", "polymorphic proxy tail call after adding the apply trap"); |
| 206 | + shouldBe(callPolymorphic(internalFunctionProxy, 3).length, 3, "polymorphic proxy of an internal function"); |
| 207 | + shouldBe(tailCallPolymorphic(internalFunctionProxy, 3).length, 3, "polymorphic proxy tail call of an internal function"); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +{ |
| 212 | + const functions = []; |
| 213 | + for (let i = 0; i < 32; ++i) |
| 214 | + functions.push(new Function("a", `return a + ${i};`)); |
| 215 | + const handler = { }; |
| 216 | + const proxy = new Proxy(target, handler); |
| 217 | + const nested = new Proxy(new Proxy(target, { }), { }); |
| 218 | + const internalFunctionProxy = new Proxy(Array, { }); |
| 219 | + for (let i = 0; i < testLoopCount; ++i) { |
| 220 | + const j = i % functions.length; |
| 221 | + shouldBe(callMegamorphic(functions[j], i), i + j, "megamorphic function"); |
| 222 | + shouldBe(tailCallMegamorphic(functions[j], i), i + j, "megamorphic function tail call"); |
| 223 | + if (i === (testLoopCount >> 1)) |
| 224 | + handler.apply = function () { return "trapped"; }; |
| 225 | + let result = callMegamorphic(proxy, i); |
| 226 | + if (i < (testLoopCount >> 1)) |
| 227 | + shouldBe(result[1], i, "megamorphic proxy before adding the apply trap"); |
| 228 | + else |
| 229 | + shouldBe(result, "trapped", "megamorphic proxy after adding the apply trap"); |
| 230 | + result = tailCallMegamorphic(proxy, i); |
| 231 | + if (i < (testLoopCount >> 1)) |
| 232 | + shouldBe(result[1], i, "megamorphic proxy tail call before adding the apply trap"); |
| 233 | + else |
| 234 | + shouldBe(result, "trapped", "megamorphic proxy tail call after adding the apply trap"); |
| 235 | + shouldBe(callMegamorphic(nested, i)[1], i, "megamorphic nested proxy"); |
| 236 | + shouldBe(tailCallMegamorphic(nested, i)[1], i, "megamorphic nested proxy tail call"); |
| 237 | + shouldBe(callMegamorphic(internalFunctionProxy, 3).length, 3, "megamorphic proxy of an internal function"); |
| 238 | + shouldBe(tailCallMegamorphic(internalFunctionProxy, 3).length, 3, "megamorphic proxy tail call of an internal function"); |
| 239 | + } |
| 240 | +} |
0 commit comments