[JSC] Don't reference the properties of @Reflect directly
[WebKit-https.git] / Source / JavaScriptCore / tests / stress / object-assign-correctness.js
1 function assert(b) {
2     if (!b)
3         throw new Error("Bad assertion.");
4 }
5 function test(f) {
6     for (let i = 0; i < 500; i++)
7         f();
8 }
9
10 var originalReflect = Reflect;
11 var ownKeys = Reflect.ownKeys;
12 var getOwnPropertyDescriptor = Reflect.getOwnPropertyDescriptor;
13
14 function runTests() {
15     test(function() {
16         let ownKeysCalled = false;
17         let getOwnPropertyDescriptorProps = [];
18         let getProps = [];
19         let enumerableCalled = false;
20         let handler = {
21             getOwnPropertyDescriptor: function(target, key) {
22                 getOwnPropertyDescriptorProps.push(key);
23                 switch(key) {
24                 case "foo":
25                     return {
26                         enumerable: true,
27                         configurable: true,
28                         value: 45
29                     };
30                 case "bar":
31                     return  {
32                         enumerable: true,
33                         get enumerable() {
34                             enumerableCalled = true;
35                             return true;
36                         },
37                         configurable: true,
38                         value: 50
39                     }
40                 case "baz":
41                     return  {
42                         enumerable: false,
43                         configurable: true,
44                         value: 50
45                     }
46                 default:
47                     assert(false, "should not be reached.");
48                     break;
49                 }
50             },
51             ownKeys: function(target) {
52                 ownKeysCalled = true;
53                 return ["foo", "bar", "baz"];
54             },
55             get: function(target, key) {
56                 getProps.push(key);
57                 switch(key) {
58                 case "foo":
59                     return 20;
60                 case "bar":
61                     return "bar";
62                 default:
63                     assert(false, "should not be reached.");
64                     break;
65                 }
66             }
67         };
68
69         let proxy = new Proxy({}, handler);
70         let foo = {};
71         Object.assign(foo, proxy);
72
73         assert(enumerableCalled);
74
75         assert(ownKeys(foo).length === 2);
76         assert(ownKeys(foo)[0] === "foo");
77         assert(ownKeys(foo)[1] === "bar");
78         assert(foo.foo === 20);
79         assert(foo.bar === "bar");
80
81         assert(ownKeysCalled);
82         assert(getOwnPropertyDescriptorProps.length === 3);
83         assert(getOwnPropertyDescriptorProps[0] === "foo");
84         assert(getOwnPropertyDescriptorProps[1] === "bar");
85         assert(getOwnPropertyDescriptorProps[2] === "baz");
86
87         assert(getProps.length === 2);
88         assert(getProps[0] === "foo");
89         assert(getProps[1] === "bar");
90     });
91
92
93     let oldReflect = Reflect;
94     Reflect = null;
95     assert(Reflect === null); // Make sure Object.assign's use of Reflect is safe.
96
97     test(function() {
98         let ownKeysCalled = false;
99         let getOwnPropertyDescriptorProps = [];
100         let getProps = [];
101         let enumerableCalled = false;
102         let handler = {
103             getOwnPropertyDescriptor: function(target, key) {
104                 getOwnPropertyDescriptorProps.push(key);
105                 switch(key) {
106                 case "foo":
107                     return {
108                         enumerable: true,
109                         configurable: true,
110                         value: 45
111                     };
112                 case "bar":
113                     return  {
114                         get enumerable() {
115                             enumerableCalled = true;
116                             return true;
117                         },
118                         configurable: true,
119                         value: 50
120                     }
121                 case "baz":
122                     return  {
123                         enumerable: false,
124                         configurable: true,
125                         value: 50
126                     }
127                 default:
128                     assert(false, "should not be reached.");
129                     break;
130                 }
131             },
132             ownKeys: function(target) {
133                 ownKeysCalled = true;
134                 return ["foo", "bar", "baz"];
135             },
136             get: function(target, key) {
137                 getProps.push(key);
138                 switch(key) {
139                 case "foo":
140                     return 20;
141                 case "bar":
142                     return "bar";
143                 default:
144                     assert(false, "should not be reached.");
145                     break;
146                 }
147             }
148         };
149
150         let proxy = new Proxy({}, handler);
151         let foo = {};
152         Object.assign(foo, proxy);
153
154         assert(enumerableCalled);
155
156         assert(ownKeys(foo).length === 2);
157         assert(ownKeys(foo)[0] === "foo");
158         assert(ownKeys(foo)[1] === "bar");
159         assert(foo.foo === 20);
160         assert(foo.bar === "bar");
161
162         assert(ownKeysCalled);
163         assert(getOwnPropertyDescriptorProps.length === 3);
164         assert(getOwnPropertyDescriptorProps[0] === "foo");
165         assert(getOwnPropertyDescriptorProps[1] === "bar");
166         assert(getOwnPropertyDescriptorProps[2] === "baz");
167
168         assert(getProps.length === 2);
169         assert(getProps[0] === "foo");
170         assert(getProps[1] === "bar");
171
172     });
173
174     Reflect = oldReflect;
175 }
176
177 runTests();
178 Reflect.ownKeys = function () {};
179 Reflect.getOwnPropertyDescriptor = function () {};
180 runTests();