summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/basic/object-assign-plain.js
blob: cf0db7134e42247a944ad321a92dc5e200470ed6 (plain)
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
// Tests for Object.assign's fast path for plain objects.

load(libdir + "asserts.js");

function testProtoSetter() {
    var from = Object.create(null);
    from.__proto__ = {};
    assertEq(Object.getPrototypeOf(from), null);

    var to = Object.assign({}, from);
    assertEq(Object.getPrototypeOf(to), from.__proto__);
    assertEq(Object.getOwnPropertyNames(to).length, 0);
}
testProtoSetter();
testProtoSetter();
testProtoSetter();

function testProtoDataProp() {
    var to = Object.create(null);
    to.__proto__ = 1;
    var from = Object.create(null);
    from.__proto__ = 2;
    Object.assign(to, from);
    assertEq(Object.getPrototypeOf(to), null);
    assertEq(to.__proto__, 2);
}
testProtoDataProp();
testProtoDataProp();
testProtoDataProp();

function testNonExtensible() {
    var to = Object.preventExtensions({x: 1});
    Object.assign(to, {x: 2});
    assertEq(to.x, 2);
    assertThrowsInstanceOf(() => Object.assign(to, {x: 3, y: 4}), TypeError);
    assertEq(to.x, 3);
    assertEq("y" in to, false);
}
testNonExtensible();
testNonExtensible();
testNonExtensible();

function testNonExtensibleNoProps() {
    var to = Object.preventExtensions({});
    Object.assign(to, {}); // No exception.
}
testNonExtensibleNoProps();
testNonExtensibleNoProps();
testNonExtensibleNoProps();

function testDenseElements() {
    var to = Object.assign({}, {0: 1, 1: 2});
    assertEq(to[0], 1);
    assertEq(to[1], 2);
}
testDenseElements();
testDenseElements();
testDenseElements();

function testNonWritableOnProto() {
    var proto = {};
    Object.defineProperty(proto, "x", {value: 1, enumerable: true, configurable: true});
    var to = Object.create(proto);
    assertThrowsInstanceOf(() => Object.assign(to, {x: 2}), TypeError);
    assertEq(to.x, 1);
    assertEq(Object.getOwnPropertyNames(to).length, 0);
}
testNonWritableOnProto();
testNonWritableOnProto();
testNonWritableOnProto();

function testAccessorOnProto() {
    var setterVal;
    var proto = {set a(v) { setterVal = v; }};
    var to = Object.assign(Object.create(proto), {a: 9});
    assertEq(setterVal, 9);
    assertEq(Object.getOwnPropertyNames(to).length, 0);
}
testAccessorOnProto();
testAccessorOnProto();
testAccessorOnProto();

function testSetAndAdd() {
    var to = Object.assign({x: 1, y: 2}, {x: 3, y: 4, z: 5});
    assertEq(to.x, 3);
    assertEq(to.y, 4);
    assertEq(to.z, 5);
}
testSetAndAdd();
testSetAndAdd();
testSetAndAdd();

function testNonConfigurableFrom() {
    var from = {};
    Object.defineProperty(from, "x", {value: 1, enumerable: true, writable: true});
    var to = Object.assign({}, from);
    assertEq(to.x, 1);
    assertEq(Object.getOwnPropertyDescriptor(to, "x").configurable, true);
}
testNonConfigurableFrom();
testNonConfigurableFrom();
testNonConfigurableFrom();

function testNonEnumerableFrom() {
    var from = {};
    Object.defineProperty(from, "x", {value: 1, configurable: true, writable: true});
    var to = Object.assign({}, from);
    assertEq(Object.getOwnPropertyNames(to).length, 0);
    assertEq(to.x, undefined);
}
testNonEnumerableFrom();
testNonEnumerableFrom();
testNonEnumerableFrom();

function testNonWritableFrom() {
    var from = {};
    Object.defineProperty(from, "x", {value: 1, configurable: true, enumerable: true});
    var to = Object.assign({}, from);
    assertEq(to.x, 1);
    assertEq(Object.getOwnPropertyDescriptor(to, "x").writable, true);
}
testNonWritableFrom();
testNonWritableFrom();
testNonWritableFrom();

function testFrozenProto() {
    var proto = Object.freeze({x: 1});
    var target = Object.create(proto);
    Object.assign(target, {foo: 1});
    assertEq(target.foo, 1);
    assertThrowsInstanceOf(() => Object.assign(target, {x: 2}), TypeError);
    assertEq(target.x, 1);
}
testFrozenProto();
testFrozenProto();
testFrozenProto();

function testReuseShape() {
    var from = {};
    from.x = 1;
    from.y = 2;
    var to = Object.assign({}, from);
    assertEq(to.x, 1);
    assertEq(to.y, 2);
}
testReuseShape();
testReuseShape();
testReuseShape();