|
| 1 | +function shouldBe(actual, expected) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error(`Bad value: ${actual}, expected: ${expected}`); |
| 4 | +} |
| 5 | + |
| 6 | +function shouldThrow(func, errorType) { |
| 7 | + let error; |
| 8 | + try { |
| 9 | + func(); |
| 10 | + } catch (e) { |
| 11 | + error = e; |
| 12 | + } |
| 13 | + if (!(error instanceof errorType)) |
| 14 | + throw new Error(`Expected ${errorType.name}, got: ${error}`); |
| 15 | +} |
| 16 | + |
| 17 | +function checkDescriptor(object, key, value, writable, enumerable, configurable) { |
| 18 | + let descriptor = Object.getOwnPropertyDescriptor(object, key); |
| 19 | + shouldBe(descriptor.value, value); |
| 20 | + shouldBe(descriptor.writable, writable); |
| 21 | + shouldBe(descriptor.enumerable, enumerable); |
| 22 | + shouldBe(descriptor.configurable, configurable); |
| 23 | +} |
| 24 | + |
| 25 | +function testResult() { |
| 26 | + let result = Proxy.revocable({}, {}); |
| 27 | + shouldBe(Object.getPrototypeOf(result), Object.prototype); |
| 28 | + shouldBe(JSON.stringify(Reflect.ownKeys(result)), `["proxy","revoke"]`); |
| 29 | + checkDescriptor(result, "proxy", result.proxy, true, true, true); |
| 30 | + checkDescriptor(result, "revoke", result.revoke, true, true, true); |
| 31 | + |
| 32 | + let { proxy, revoke } = result; |
| 33 | + for (let i = 0; i < 10; ++i) |
| 34 | + result["p" + i] = i; |
| 35 | + shouldBe(result.proxy, proxy); |
| 36 | + shouldBe(result.revoke, revoke); |
| 37 | + for (let i = 0; i < 10; ++i) |
| 38 | + shouldBe(result["p" + i], i); |
| 39 | + shouldBe(delete result.proxy, true); |
| 40 | + shouldBe("proxy" in result, false); |
| 41 | + shouldBe(result.revoke, revoke); |
| 42 | + result.revoke = 42; |
| 43 | + shouldBe(result.revoke, 42); |
| 44 | +} |
| 45 | + |
| 46 | +function testRevokeFunction() { |
| 47 | + let { revoke } = Proxy.revocable({}, {}); |
| 48 | + shouldBe(typeof revoke, "function"); |
| 49 | + shouldBe(Object.getPrototypeOf(revoke), Function.prototype); |
| 50 | + shouldBe(JSON.stringify(Reflect.ownKeys(revoke)), `["length","name"]`); |
| 51 | + checkDescriptor(revoke, "length", 0, false, false, true); |
| 52 | + checkDescriptor(revoke, "name", "", false, false, true); |
| 53 | + shouldBe(revoke.toString(), "function () {\n [native code]\n}"); |
| 54 | + shouldThrow(() => new revoke, TypeError); |
| 55 | + shouldThrow(() => Reflect.construct(Object, [], revoke), TypeError); |
| 56 | + |
| 57 | + revoke.extra = 1; |
| 58 | + shouldBe(delete revoke.name, true); |
| 59 | + shouldBe(JSON.stringify(Reflect.ownKeys(revoke)), `["length","extra"]`); |
| 60 | + shouldBe(revoke(), undefined); |
| 61 | +} |
| 62 | + |
| 63 | +function testRevoke() { |
| 64 | + let target = { value: 1 }; |
| 65 | + let first = Proxy.revocable(target, {}); |
| 66 | + let second = Proxy.revocable(target, {}); |
| 67 | + shouldBe(first.revoke === second.revoke, false); |
| 68 | + shouldBe(first.proxy.value, 1); |
| 69 | + shouldBe(second.proxy.value, 1); |
| 70 | + |
| 71 | + shouldBe(first.revoke.call(second.proxy, second.proxy), undefined); |
| 72 | + shouldThrow(() => first.proxy.value, TypeError); |
| 73 | + shouldBe(second.proxy.value, 1); |
| 74 | + |
| 75 | + shouldBe(first.revoke(), undefined); |
| 76 | + shouldBe(second.proxy.value, 1); |
| 77 | + |
| 78 | + shouldBe(second.revoke(), undefined); |
| 79 | + shouldThrow(() => second.proxy.value, TypeError); |
| 80 | +} |
| 81 | + |
| 82 | +function testCallableTarget() { |
| 83 | + let { proxy, revoke } = Proxy.revocable(function () { return 42; }, {}); |
| 84 | + shouldBe(typeof proxy, "function"); |
| 85 | + shouldBe(proxy(), 42); |
| 86 | + revoke(); |
| 87 | + shouldBe(typeof proxy, "function"); |
| 88 | + shouldThrow(() => proxy(), TypeError); |
| 89 | + shouldThrow(() => new proxy, TypeError); |
| 90 | +} |
| 91 | + |
| 92 | +function testRealm() { |
| 93 | + let other = createGlobalObject(); |
| 94 | + let result = other.Proxy.revocable({}, {}); |
| 95 | + shouldBe(Object.getPrototypeOf(result), other.Object.prototype); |
| 96 | + shouldBe(Object.getPrototypeOf(result.revoke), other.Function.prototype); |
| 97 | + shouldBe(JSON.stringify(Reflect.ownKeys(result)), `["proxy","revoke"]`); |
| 98 | + result.revoke(); |
| 99 | + shouldThrow(() => result.proxy.value, TypeError); |
| 100 | +} |
| 101 | + |
| 102 | +testResult(); |
| 103 | +testRevokeFunction(); |
| 104 | +testCallableTarget(); |
| 105 | +testRealm(); |
| 106 | +for (let i = 0; i < testLoopCount; ++i) |
| 107 | + testRevoke(); |
0 commit comments