1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
function checkDataProperty(object, propertyKey, value, writable, enumerable, configurable) {
var desc = Object.getOwnPropertyDescriptor(object, propertyKey);
assertEq(desc === undefined, false);
assertEq('value' in desc, true);
assertEq(desc.value, value);
assertEq(desc.writable, writable);
assertEq(desc.enumerable, enumerable);
assertEq(desc.configurable, configurable);
}
/* 19.1.2.1 Object.assign ( target, ...sources ) */
assertEq(Object.assign.length, 2);
// Basic functionality works with multiple sources
function basicMultipleSources() {
var a = {};
var b = { bProp : 7 };
var c = { cProp : 8 };
Object.assign(a, b, c);
assertEq(a.bProp, 7);
assertEq(a.cProp, 8);
}
basicMultipleSources();
// Basic functionality works with symbols (Bug 1052358)
function basicSymbols() {
var a = {};
var b = { bProp : 7 };
var aSymbol = Symbol("aSymbol");
b[aSymbol] = 22;
Object.assign(a, b);
assertEq(a.bProp, 7);
assertEq(a[aSymbol], 22);
}
basicSymbols();
// Calls ToObject() for target, skips null/undefined sources, uses
// ToObject(source) otherwise.
function testToObject() {
assertThrowsInstanceOf(() => Object.assign(null, null), TypeError);
assertThrowsInstanceOf(() => Object.assign(), TypeError);
assertThrowsInstanceOf(() => Object.assign(null, {}), TypeError);
assertEq(Object.assign({}, null) instanceof Object, true);
assertEq(Object.assign({}, undefined) instanceof Object, true);
// Technically an embedding could have this as extension acting differently
// from ours, so a feature-test is inadequate. We can move this subtest
// into extensions/ if that ever matters.
if (typeof objectEmulatingUndefined === "function") {
var falsyObject = objectEmulatingUndefined();
falsyObject.foo = 7;
var obj = Object.assign({}, falsyObject);
assertEq(obj instanceof Object, true);
assertEq(obj.foo, 7);
}
assertEq(Object.assign(true, {}) instanceof Boolean, true);
assertEq(Object.assign(1, {}) instanceof Number, true);
assertEq(Object.assign("string", {}) instanceof String, true);
var o = {};
assertEq(Object.assign(o, {}), o);
}
testToObject();
// Invokes [[OwnPropertyKeys]] on ToObject(source)
function testOwnPropertyKeys() {
assertThrowsInstanceOf(() => Object.assign(null, new Proxy({}, {
getOwnPropertyNames: () => { throw new Error("not called"); }
})), TypeError);
var ownKeysCalled = false;
Object.assign({}, new Proxy({}, {
ownKeys: function() {
ownKeysCalled = true;
return [];
}
}));
assertEq(ownKeysCalled, true);
};
testOwnPropertyKeys();
// Ensure correct property traversal
function correctPropertyTraversal() {
var log = "";
var source = new Proxy({a: 1, b: 2}, {
ownKeys: () => ["b", "c", "a"],
getOwnPropertyDescriptor: function(t, pk) {
log += "#" + pk;
return Object.getOwnPropertyDescriptor(t, pk);
},
get: function(t, pk, r) {
log += "-" + pk;
return t[pk];
},
});
Object.assign({}, source);
assertEq(log, "#b-b#c#a-a");
}
correctPropertyTraversal();
// Only [[Enumerable]] properties are assigned to target
function onlyEnumerablePropertiesAssigned() {
var source = Object.defineProperties({}, {
a: {value: 1, enumerable: true},
b: {value: 2, enumerable: false},
});
var target = Object.assign({}, source);
assertEq("a" in target, true);
assertEq("b" in target, false);
}
onlyEnumerablePropertiesAssigned();
// Enumerability is decided on-time, not before main loop (1)
function testEnumerabilityDeterminedInLoop1()
{
var getterCalled = false;
var sourceTarget = {
get a() { getterCalled = true },
get b() { Object.defineProperty(sourceTarget, "a", {enumerable: false}) },
};
var source = new Proxy(sourceTarget, { ownKeys: () => ["b", "a"] });
Object.assign({}, source);
assertEq(getterCalled, false);
}
testEnumerabilityDeterminedInLoop1();
// Enumerability is decided on-time, not before main loop (2)
function testEnumerabilityDeterminedInLoop2()
{
var getterCalled = false;
var sourceTarget = {
get a() { getterCalled = true },
get b() { Object.defineProperty(sourceTarget, "a", {enumerable: true}) },
};
var source = new Proxy(sourceTarget, {
ownKeys: () => ["b", "a"]
});
Object.defineProperty(sourceTarget, "a", {enumerable: false});
Object.assign({}, source);
assertEq(getterCalled, true);
}
testEnumerabilityDeterminedInLoop2();
// Properties are retrieved through Get() and assigned onto
// the target as data properties, not in any sense cloned over as descriptors
function testPropertiesRetrievedThroughGet() {
var getterCalled = false;
Object.assign({}, {get a() { getterCalled = true }});
assertEq(getterCalled, true);
}
testPropertiesRetrievedThroughGet();
// Properties are retrieved through Get()
// Properties are assigned through Put()
function testPropertiesAssignedThroughPut() {
var setterCalled = false;
Object.assign({set a(v) { setterCalled = v }}, {a: true});
assertEq(setterCalled, true);
}
testPropertiesAssignedThroughPut();
// Properties are retrieved through Get()
// Properties are assigned through Put(): Existing property attributes are not altered
function propertiesAssignedExistingNotAltered() {
var source = {a: 1, b: 2, c: 3};
var target = {a: 0, b: 0, c: 0};
Object.defineProperty(target, "a", {enumerable: false});
Object.defineProperty(target, "b", {configurable: false});
Object.defineProperty(target, "c", {enumerable: false, configurable: false});
Object.assign(target, source);
checkDataProperty(target, "a", 1, true, false, true);
checkDataProperty(target, "b", 2, true, true, false);
checkDataProperty(target, "c", 3, true, false, false);
}
propertiesAssignedExistingNotAltered();
// Properties are retrieved through Get()
// Properties are assigned through Put(): Throws TypeError if non-writable
function propertiesAssignedTypeErrorNonWritable() {
var source = {a: 1};
var target = {a: 0};
Object.defineProperty(target, "a", {writable: false});
assertThrowsInstanceOf(() => Object.assign(target, source), TypeError);
checkDataProperty(target, "a", 0, false, true, true);
}
propertiesAssignedTypeErrorNonWritable();
// Properties are retrieved through Get()
// Put() creates standard properties; Property attributes from source are ignored
function createsStandardProperties() {
var source = {a: 1, b: 2, c: 3, get d() { return 4 }};
Object.defineProperty(source, "b", {writable: false});
Object.defineProperty(source, "c", {configurable: false});
var target = Object.assign({}, source);
checkDataProperty(target, "a", 1, true, true, true);
checkDataProperty(target, "b", 2, true, true, true);
checkDataProperty(target, "c", 3, true, true, true);
checkDataProperty(target, "d", 4, true, true, true);
}
createsStandardProperties();
// Properties created during traversal are not copied
function propertiesCreatedDuringTraversalNotCopied() {
var source = {get a() { this.b = 2 }};
var target = Object.assign({}, source);
assertEq("a" in target, true);
assertEq("b" in target, false);
}
propertiesCreatedDuringTraversalNotCopied();
// Properties deleted during traversal are not copied
function testDeletePropertiesNotCopied() {
var source = new Proxy({
get a() { delete this.b },
b: 2,
}, {
getOwnPropertyNames: () => ["a", "b"]
});
var target = Object.assign({}, source);
assertEq("a" in target, true);
assertEq("b" in target, false);
}
testDeletePropertiesNotCopied();
function testDeletionExposingShadowedProperty()
{
var srcProto = { b: 42 };
var src =
Object.create(srcProto,
{ a: { enumerable: true, get: function() { delete this.b; } },
b: { value: 2, configurable: true, enumerable: true } });
var source = new Proxy(src, { getOwnPropertyNames: () => ["a", "b"] });
var target = Object.assign({}, source);
assertEq("a" in target, true);
assertEq("b" in target, false);
}
testDeletionExposingShadowedProperty();
// Properties first deleted and then recreated during traversal are copied (1)
function testDeletedAndRecreatedPropertiesCopied1() {
var source = new Proxy({
get a() { delete this.c },
get b() { this.c = 4 },
c: 3,
}, {
getOwnPropertyNames: () => ["a", "b", "c"]
});
var target = Object.assign({}, source);
assertEq("a" in target, true);
assertEq("b" in target, true);
assertEq("c" in target, true);
checkDataProperty(target, "c", 4, true, true, true);
}
testDeletedAndRecreatedPropertiesCopied1();
// Properties first deleted and then recreated during traversal are copied (2)
function testDeletedAndRecreatedPropertiesCopied2() {
var source = new Proxy({
get a() { delete this.c },
get b() { this.c = 4 },
c: 3,
}, {
ownKeys: () => ["a", "c", "b"]
});
var target = Object.assign({}, source);
assertEq("a" in target, true);
assertEq("b" in target, true);
assertEq("c" in target, false);
}
testDeletedAndRecreatedPropertiesCopied2();
// String and Symbol valued properties are copied
function testStringAndSymbolPropertiesCopied() {
var keyA = "str-prop";
var source = {"str-prop": 1};
var target = Object.assign({}, source);
checkDataProperty(target, keyA, 1, true, true, true);
}
testStringAndSymbolPropertiesCopied();
// Intermediate exceptions stop traversal and throw exception
function testExceptionsDoNotStopFirstReported1() {
var TestError = function TestError() {};
var source = new Proxy({}, {
getOwnPropertyDescriptor: function(t, pk) {
assertEq(pk, "b");
throw new TestError();
},
ownKeys: () => ["b", "a"]
});
assertThrowsInstanceOf(() => Object.assign({}, source), TestError);
}
testExceptionsDoNotStopFirstReported1();
if (typeof reportCompare == "function")
reportCompare(true, true);
|