|
| 1 | +//------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 4 | +//------------------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js"); |
| 7 | + |
| 8 | +var tests = [ |
| 9 | + { |
| 10 | + name: "OS21193960: Proxy [[Construct]] trap confuses super call flag", |
| 11 | + body: function () { |
| 12 | + function test(){ |
| 13 | + let ctorCount = 0; |
| 14 | + function ctor() { |
| 15 | + if (new.target !== undefined) { |
| 16 | + ctorCount++; |
| 17 | + this.prop0 = 123; |
| 18 | + assert.isTrue(proxy === new.target, "proxy === new.target"); |
| 19 | + } else { |
| 20 | + assert.fail('call ctor'); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + let ctor_prototype = ctor.prototype; |
| 25 | + let getCount = 0; |
| 26 | + let proxyHandler = { |
| 27 | + ['get'](handler, prop, target) { |
| 28 | + getCount++; |
| 29 | + assert.isTrue(prop !== 'prop0', "prop !== 'prop0'"); |
| 30 | + switch(prop) { |
| 31 | + case 'prototype': |
| 32 | + return ctor_prototype; |
| 33 | + } |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + var proxy = new Proxy(ctor, proxyHandler); |
| 38 | + let newProxy = new proxy; |
| 39 | + |
| 40 | + assert.isTrue(proxy !== newProxy, 'proxy !== newProxy'); |
| 41 | + assert.areEqual('object', typeof newProxy, '"object" === typeof newProxy'); |
| 42 | + assert.areEqual(1, ctorCount, "1 === ctorCount"); |
| 43 | + |
| 44 | + assert.areEqual(123, newProxy.prop0, "123 === newProxy.prop0"); |
| 45 | + assert.areEqual(0, getCount, "0 === getCount"); |
| 46 | + }; |
| 47 | + |
| 48 | + test(); |
| 49 | + test(); |
| 50 | + } |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Proxy target is a base class", |
| 54 | + body: function () { |
| 55 | + function test(){ |
| 56 | + let ctorCount = 0; |
| 57 | + class ctor { |
| 58 | + constructor() { |
| 59 | + if (new.target !== undefined) { |
| 60 | + ctorCount++; |
| 61 | + this.prop0 = 123; |
| 62 | + assert.isTrue(proxy === new.target, "proxy === new.target"); |
| 63 | + } else { |
| 64 | + assert.fail('call ctor'); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + let ctor_prototype = ctor.prototype; |
| 70 | + let getCount = 0; |
| 71 | + let proxyHandler = { |
| 72 | + ['get'](handler, prop, target) { |
| 73 | + getCount++; |
| 74 | + assert.isTrue(prop !== 'prop0', "prop !== 'prop0'"); |
| 75 | + switch(prop) { |
| 76 | + case 'prototype': |
| 77 | + return ctor_prototype; |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + var proxy = new Proxy(ctor, proxyHandler); |
| 83 | + let newProxy = new proxy; |
| 84 | + |
| 85 | + assert.isTrue(proxy !== newProxy, 'proxy !== newProxy'); |
| 86 | + assert.areEqual('object', typeof newProxy, '"object" === typeof newProxy'); |
| 87 | + assert.areEqual(1, ctorCount, "1 === ctorCount"); |
| 88 | + |
| 89 | + assert.areEqual(123, newProxy.prop0, "123 === newProxy.prop0"); |
| 90 | + assert.areEqual(1, getCount, "1 === getCount"); |
| 91 | + }; |
| 92 | + |
| 93 | + test(); |
| 94 | + test(); |
| 95 | + } |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Proxy target is a derived class", |
| 99 | + body: function () { |
| 100 | + function test(){ |
| 101 | + let baseCount = 0; |
| 102 | + class base { |
| 103 | + constructor() { |
| 104 | + if (new.target !== undefined) { |
| 105 | + baseCount++; |
| 106 | + assert.isTrue(proxy === new.target, "proxy === new.target"); |
| 107 | + } else { |
| 108 | + assert.fail('call base'); |
| 109 | + } |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + let ctorCount = 0; |
| 114 | + class ctor extends base { |
| 115 | + constructor() { |
| 116 | + if (new.target !== undefined) { |
| 117 | + ctorCount++; |
| 118 | + super(); |
| 119 | + this.prop0 = 123; |
| 120 | + assert.isTrue(proxy === new.target, "proxy === new.target"); |
| 121 | + } else { |
| 122 | + assert.fail('call ctor'); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + let ctor_prototype = ctor.prototype; |
| 128 | + let getCount = 0; |
| 129 | + let proxyHandler = { |
| 130 | + ['get'](handler, prop, target) { |
| 131 | + getCount++; |
| 132 | + assert.isTrue(prop !== 'prop0', "prop !== 'prop0'"); |
| 133 | + switch(prop) { |
| 134 | + case 'prototype': |
| 135 | + return ctor_prototype; |
| 136 | + } |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + var proxy = new Proxy(ctor, proxyHandler); |
| 141 | + let newProxy = new proxy; |
| 142 | + |
| 143 | + assert.isTrue(proxy !== newProxy, 'proxy !== newProxy'); |
| 144 | + assert.areEqual('object', typeof newProxy, '"object" === typeof newProxy'); |
| 145 | + assert.areEqual(1, ctorCount, "1 === ctorCount"); |
| 146 | + assert.areEqual(1, baseCount, "1 === baseCount"); |
| 147 | + |
| 148 | + assert.areEqual(123, newProxy.prop0, "123 === newProxy.prop0"); |
| 149 | + assert.areEqual(1, getCount, "1 === getCount"); |
| 150 | + }; |
| 151 | + |
| 152 | + test(); |
| 153 | + test(); |
| 154 | + } |
| 155 | + }, |
| 156 | +]; |
| 157 | + |
| 158 | +testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" }); |
0 commit comments