|
| 1 | +function shouldBe(actual, expected) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error("bad value: " + actual + ", expected: " + expected); |
| 4 | +} |
| 5 | + |
| 6 | +function describe(object) { |
| 7 | + return Reflect.ownKeys(object).map((key) => { |
| 8 | + let descriptor = Object.getOwnPropertyDescriptor(object, key); |
| 9 | + shouldBe(descriptor.writable && descriptor.enumerable && descriptor.configurable, true); |
| 10 | + return String(key) + "=" + String(descriptor.value); |
| 11 | + }).join(); |
| 12 | +} |
| 13 | +noInline(describe); |
| 14 | + |
| 15 | +function spreadOnly(source) { |
| 16 | + return { ...source }; |
| 17 | +} |
| 18 | +noInline(spreadOnly); |
| 19 | + |
| 20 | +function spreadFirst(source) { |
| 21 | + return { ...source, x: "x" }; |
| 22 | +} |
| 23 | +noInline(spreadFirst); |
| 24 | + |
| 25 | +function spreadLast(source) { |
| 26 | + return { x: "x", ...source }; |
| 27 | +} |
| 28 | +noInline(spreadLast); |
| 29 | + |
| 30 | +function restOnly(source) { |
| 31 | + let { ...rest } = source; |
| 32 | + return rest; |
| 33 | +} |
| 34 | +noInline(restOnly); |
| 35 | + |
| 36 | +function restExcludingA(source) { |
| 37 | + let { a, ...rest } = source; |
| 38 | + return rest; |
| 39 | +} |
| 40 | +noInline(restExcludingA); |
| 41 | + |
| 42 | +function restExcludingComputed(source, key) { |
| 43 | + let { [key]: ignored, ...rest } = source; |
| 44 | + return rest; |
| 45 | +} |
| 46 | +noInline(restExcludingComputed); |
| 47 | + |
| 48 | +function describeWithoutX(object) { |
| 49 | + shouldBe(object.x, "x"); |
| 50 | + delete object.x; |
| 51 | + return describe(object); |
| 52 | +} |
| 53 | +noInline(describeWithoutX); |
| 54 | + |
| 55 | +function check(source, expected) { |
| 56 | + shouldBe(describe(spreadOnly(source)), expected); |
| 57 | + shouldBe(describe(restOnly(source)), expected); |
| 58 | + shouldBe(describeWithoutX(spreadFirst(source)), expected); |
| 59 | + shouldBe(describeWithoutX(spreadLast(source)), expected); |
| 60 | +} |
| 61 | +noInline(check); |
| 62 | + |
| 63 | +function withProperties(source) { |
| 64 | + source.a = 1; |
| 65 | + source.b = 2; |
| 66 | + source.c = 3; |
| 67 | + return source; |
| 68 | +} |
| 69 | + |
| 70 | +let factories = [ |
| 71 | + () => function () { }, |
| 72 | + () => function named(x, y) { }, |
| 73 | + () => function () { "use strict"; }, |
| 74 | + () => () => { }, |
| 75 | + () => ({ method() { } }).method, |
| 76 | + () => Object.getOwnPropertyDescriptor({ get getter() { return 1; } }, "getter").get, |
| 77 | + () => class { }, |
| 78 | + () => class Named { constructor(x) { } }, |
| 79 | + () => class extends Object { }, |
| 80 | + () => function* () { }, |
| 81 | + () => async function () { }, |
| 82 | + () => async function* () { }, |
| 83 | + () => async () => { }, |
| 84 | + () => function (x) { }.bind(null), |
| 85 | + () => { let result; new Promise((resolve) => { result = resolve; }); return result; }, |
| 86 | + () => new Function("x", "return x"), |
| 87 | + () => new (class extends Function { }), |
| 88 | + () => createGlobalObject().Function("x", "return x"), |
| 89 | +]; |
| 90 | + |
| 91 | +for (let factory of factories) { |
| 92 | + check(factory(), ""); |
| 93 | + |
| 94 | + let source = withProperties(factory()); |
| 95 | + check(source, "a=1,b=2,c=3"); |
| 96 | + shouldBe(describe(restExcludingA(source)), "b=2,c=3"); |
| 97 | + shouldBe(describe(restExcludingComputed(source, "b")), "a=1,c=3"); |
| 98 | + shouldBe(describe(restExcludingComputed(source, "name")), "a=1,b=2,c=3"); |
| 99 | + shouldBe(Object.getPrototypeOf(spreadOnly(source)), Object.prototype); |
| 100 | + shouldBe(Reflect.ownKeys(source).join().replace(",prototype", ""), "length,name,a,b,c"); |
| 101 | + source.prototype; |
| 102 | + check(source, "a=1,b=2,c=3"); |
| 103 | + Object.defineProperty(source, "name", { enumerable: true }); |
| 104 | + shouldBe(Object.keys(spreadOnly(source)).join(), "name,a,b,c"); |
| 105 | + shouldBe(Object.keys(restOnly(source)).join(), "name,a,b,c"); |
| 106 | + Object.defineProperty(source, "length", { enumerable: true }); |
| 107 | + shouldBe(Object.keys(spreadOnly(source)).join(), "length,name,a,b,c"); |
| 108 | + shouldBe(Object.keys(restOnly(source)).join(), "length,name,a,b,c"); |
| 109 | +} |
| 110 | + |
| 111 | +for (let i = 0; i < testLoopCount; ++i) { |
| 112 | + let source = withProperties(i & 1 ? function () { } : () => { }); |
| 113 | + shouldBe(Object.keys(spreadOnly(source)).join(), "a,b,c"); |
| 114 | + shouldBe(Object.keys(spreadFirst(source)).join(), "a,b,c,x"); |
| 115 | + shouldBe(Object.keys(spreadLast(source)).join(), "x,a,b,c"); |
| 116 | + shouldBe(Object.keys(restExcludingA(source)).join(), "b,c"); |
| 117 | + shouldBe(Object.keys(spreadOnly(class { static a = 1; static name = "field"; })).join(), "name,a"); |
| 118 | +} |
| 119 | + |
| 120 | +{ |
| 121 | + let source = withProperties(function named() { }); |
| 122 | + Object.defineProperty(source, "name", { enumerable: true }); |
| 123 | + check(source, "name=named,a=1,b=2,c=3"); |
| 124 | +} |
| 125 | + |
| 126 | +{ |
| 127 | + let source = withProperties(function (x, y) { }); |
| 128 | + Object.defineProperty(source, "length", { enumerable: true }); |
| 129 | + check(source, "length=2,a=1,b=2,c=3"); |
| 130 | +} |
| 131 | + |
| 132 | +{ |
| 133 | + let source = withProperties((x) => { }); |
| 134 | + Object.defineProperty(source, "name", { value: "defined", enumerable: true }); |
| 135 | + Object.defineProperty(source, "length", { enumerable: true }); |
| 136 | + check(source, "length=1,name=defined,a=1,b=2,c=3"); |
| 137 | +} |
| 138 | + |
| 139 | +{ |
| 140 | + let source = withProperties(function named() { }); |
| 141 | + check(source, "a=1,b=2,c=3"); |
| 142 | + Reflect.defineProperty(source, "name", { enumerable: true }); |
| 143 | + check(source, "name=named,a=1,b=2,c=3"); |
| 144 | +} |
| 145 | + |
| 146 | +{ |
| 147 | + let source = withProperties(function named(x) { }.bind(null)); |
| 148 | + Object.defineProperty(source, "name", { enumerable: true }); |
| 149 | + check(source, "name=bound named,a=1,b=2,c=3"); |
| 150 | +} |
| 151 | + |
| 152 | +check(class { static a = 1; static name = "field"; static b = 2; }, "name=field,a=1,b=2"); |
| 153 | +check(class { static a = 1; static length = "field"; static b = 2; }, "length=field,a=1,b=2"); |
| 154 | +check(class { static a = 1; static ["name"] = "field"; static ["length"] = "other"; }, "length=other,name=field,a=1"); |
| 155 | +check(class { static a = 1; static name() { } static length() { } static b = 2; }, "a=1,b=2"); |
| 156 | +check(class extends class { static inherited = 1; } { static a = 1; }, "a=1"); |
| 157 | + |
| 158 | +{ |
| 159 | + let source = withProperties(function () { }); |
| 160 | + Object.defineProperty(source, "hidden", { value: 1, enumerable: false }); |
| 161 | + Object.defineProperty(source, "readOnly", { value: 2, enumerable: true }); |
| 162 | + check(source, "a=1,b=2,c=3,readOnly=2"); |
| 163 | +} |
| 164 | + |
| 165 | +{ |
| 166 | + let source = function () { }; |
| 167 | + source[Symbol("symbol")] = 1; |
| 168 | + source.a = 2; |
| 169 | + check(source, "a=2,Symbol(symbol)=1"); |
| 170 | +} |
| 171 | + |
| 172 | +{ |
| 173 | + let source = withProperties(function () { }); |
| 174 | + source[1] = "one"; |
| 175 | + source[0] = "zero"; |
| 176 | + check(source, "0=zero,1=one,a=1,b=2,c=3"); |
| 177 | +} |
| 178 | + |
| 179 | +{ |
| 180 | + let source = withProperties(() => { }); |
| 181 | + source.prototype = 4; |
| 182 | + check(source, "a=1,b=2,c=3,prototype=4"); |
| 183 | +} |
| 184 | + |
| 185 | +{ |
| 186 | + let source = withProperties(function () { }); |
| 187 | + source.prototype = 4; |
| 188 | + check(source, "a=1,b=2,c=3"); |
| 189 | +} |
| 190 | + |
| 191 | +{ |
| 192 | + let source = withProperties(function () { }); |
| 193 | + Object.defineProperty(source, "__proto__", { value: 4, writable: true, enumerable: true, configurable: true }); |
| 194 | + check(source, "a=1,b=2,c=3,__proto__=4"); |
| 195 | + shouldBe(describe({ get __proto__() { return "getter"; }, set __proto__(value) { throw new Error("setter"); }, ...source }), "__proto__=4,a=1,b=2,c=3"); |
| 196 | +} |
| 197 | + |
| 198 | +check(Object.freeze(withProperties(function () { })), "a=1,b=2,c=3"); |
| 199 | +check(Object.seal(withProperties(function () { })), "a=1,b=2,c=3"); |
| 200 | + |
| 201 | +{ |
| 202 | + let source = withProperties(function () { }); |
| 203 | + delete source.b; |
| 204 | + delete source.name; |
| 205 | + delete source.length; |
| 206 | + check(source, "a=1,c=3"); |
| 207 | +} |
| 208 | + |
| 209 | +{ |
| 210 | + let source = function () { }; |
| 211 | + for (let i = 0; i < 200; ++i) |
| 212 | + source["k" + i] = i; |
| 213 | + shouldBe(Object.keys(spreadOnly(source)).length, 200); |
| 214 | + shouldBe(Object.keys(restOnly(source)).length, 200); |
| 215 | + for (let i = 0; i < 200; ++i) { |
| 216 | + if (i % 50) |
| 217 | + delete source["k" + i]; |
| 218 | + } |
| 219 | + check(source, "k0=0,k50=50,k100=100,k150=150"); |
| 220 | +} |
| 221 | + |
| 222 | +{ |
| 223 | + let log = []; |
| 224 | + let source = function () { }; |
| 225 | + source.a = 1; |
| 226 | + Object.defineProperty(source, "b", { get() { log.push("b"); delete source.c; source.d = 4; return 2; }, enumerable: true, configurable: true }); |
| 227 | + source.c = 3; |
| 228 | + shouldBe(describe(spreadOnly(source)), "a=1,b=2"); |
| 229 | + shouldBe(log.join(), "b"); |
| 230 | + shouldBe(describe(restOnly(source)), "a=1,b=2,d=4"); |
| 231 | + shouldBe(log.join(), "b,b"); |
| 232 | +} |
| 233 | + |
| 234 | +{ |
| 235 | + class Private { |
| 236 | + static #field = 1; |
| 237 | + static #method() { } |
| 238 | + static a = 1; |
| 239 | + static hasField(object) { return #field in object; } |
| 240 | + static hasMethod(object) { return #method in object; } |
| 241 | + } |
| 242 | + shouldBe(Private.hasField(Private), true); |
| 243 | + shouldBe(Private.hasMethod(Private), true); |
| 244 | + for (let result of [spreadOnly(Private), spreadFirst(Private), spreadLast(Private), restOnly(Private)]) { |
| 245 | + shouldBe(result.a, 1); |
| 246 | + shouldBe(Private.hasField(result), false); |
| 247 | + shouldBe(Private.hasMethod(result), false); |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +{ |
| 252 | + let source = withProperties(function () { }); |
| 253 | + let result = spreadOnly(source); |
| 254 | + result.a = 10; |
| 255 | + source.b = 20; |
| 256 | + shouldBe(describe(result), "a=10,b=2,c=3"); |
| 257 | + shouldBe(describe(spreadOnly(source)), "a=1,b=20,c=3"); |
| 258 | +} |
| 259 | + |
| 260 | +check(Number, ""); |
| 261 | +check(Promise, ""); |
| 262 | +check(Math.max, ""); |
| 263 | +check(Array.prototype.map, ""); |
0 commit comments