summaryrefslogtreecommitdiff
path: root/js/src/tests/ecma_2017/Function/Object-toSource.js
blob: 33b9e588eec7e030d33e98d9bdc47ce76b7974de (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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var BUGNUMBER = 1317400;
var summary = "Function string representation in Object.prototype.toSource";

print(BUGNUMBER + ": " + summary);

// Methods.

assertEq(({ foo(){} }).toSource(),
         "({foo(){}})");
assertEq(({ *foo(){} }).toSource(),
         "({*foo(){}})");
assertEq(({ async foo(){} }).toSource(),
         "({async foo(){}})");

assertEq(({ 1(){} }).toSource(),
         "({1(){}})");

// Methods with more spacing.
// Spacing is kept.

assertEq(({ foo (){} }).toSource(),
         "({foo (){}})");
assertEq(({ foo () {} }).toSource(),
         "({foo () {}})");

// Methods with computed name.
// Method syntax is composed.

let name = "foo";
assertEq(({ [name](){} }).toSource(),
         "({foo(){}})");
assertEq(({ *[name](){} }).toSource(),
         "({*foo(){}})");
assertEq(({ async [name](){} }).toSource(),
         "({async foo(){}})");

assertEq(({ [ Symbol.iterator ](){} }).toSource(),
         "({[Symbol.iterator](){}})");

// Accessors.

assertEq(({ get foo(){} }).toSource(),
         "({get foo(){}})");
assertEq(({ set foo(v){} }).toSource(),
         "({set foo(v){}})");

// Accessors with computed name.
// Method syntax is composed.

assertEq(({ get [name](){} }).toSource(),
         "({get foo(){}})");
assertEq(({ set [name](v){} }).toSource(),
         "({set foo(v){}})");

assertEq(({ get [ Symbol.iterator ](){} }).toSource(),
         "({get [Symbol.iterator](){}})");
assertEq(({ set [ Symbol.iterator ](v){} }).toSource(),
         "({set [Symbol.iterator](v){}})");

// Getter and setter with same name.
// Getter always comes before setter.

assertEq(({ get foo(){}, set foo(v){} }).toSource(),
         "({get foo(){}, set foo(v){}})");
assertEq(({ set foo(v){}, get foo(){} }).toSource(),
         "({get foo(){}, set foo(v){}})");

// Normal properties.

assertEq(({ foo: function(){} }).toSource(),
         "({foo:(function(){})})");
assertEq(({ foo: function bar(){} }).toSource(),
         "({foo:(function bar(){})})");
assertEq(({ foo: function*(){} }).toSource(),
         "({foo:(function*(){})})");
assertEq(({ foo: async function(){} }).toSource(),
         "({foo:(async function(){})})");

// Normal properties with computed name.

assertEq(({ [ Symbol.iterator ]: function(){} }).toSource(),
         "({[Symbol.iterator]:(function(){})})");

// Dynamically defined properties with function expression.
// Never become a method syntax.

let obj = {};
obj.foo = function() {};
assertEq(obj.toSource(),
         "({foo:(function() {})})");

obj = {};
Object.defineProperty(obj, "foo", {value: function() {}});
assertEq(obj.toSource(),
         "({})");

obj = {};
Object.defineProperty(obj, "foo", {value: function() {}, enumerable: true});
assertEq(obj.toSource(),
         "({foo:(function() {})})");

obj = {};
Object.defineProperty(obj, "foo", {value: function bar() {}, enumerable: true});
assertEq(obj.toSource(),
         "({foo:(function bar() {})})");

obj = {};
Object.defineProperty(obj, Symbol.iterator, {value: function() {}, enumerable: true});
assertEq(obj.toSource(),
         "({[Symbol.iterator]:(function() {})})");

// Dynamically defined property with other object's method.
// Method syntax is composed.

let method = ({foo() {}}).foo;

obj = {};
Object.defineProperty(obj, "foo", {value: method, enumerable: true});
assertEq(obj.toSource(),
         "({foo() {}})");

obj = {};
Object.defineProperty(obj, "bar", {value: method, enumerable: true});
assertEq(obj.toSource(),
         "({bar() {}})");

method = ({*foo() {}}).foo;

obj = {};
Object.defineProperty(obj, "bar", {value: method, enumerable: true});
assertEq(obj.toSource(),
         "({*bar() {}})");

method = ({async foo() {}}).foo;

obj = {};
Object.defineProperty(obj, "bar", {value: method, enumerable: true});
assertEq(obj.toSource(),
         "({async bar() {}})");

// Dynamically defined accessors.
// Accessor syntax is composed.

obj = {};
Object.defineProperty(obj, "foo", {get: function() {}, enumerable: true});
assertEq(obj.toSource(),
         "({get foo() {}})");

obj = {};
Object.defineProperty(obj, "foo", {set: function() {}, enumerable: true});
assertEq(obj.toSource(),
         "({set foo() {}})");

obj = {};
Object.defineProperty(obj, Symbol.iterator, {get: function() {}, enumerable: true});
assertEq(obj.toSource(),
         "({get [Symbol.iterator]() {}})");

obj = {};
Object.defineProperty(obj, Symbol.iterator, {set: function() {}, enumerable: true});
assertEq(obj.toSource(),
         "({set [Symbol.iterator]() {}})");

// Dynamically defined accessors with other object's accessors.
// Accessor syntax is composed.

let accessor = Object.getOwnPropertyDescriptor({ get foo() {} }, "foo").get;
obj = {};
Object.defineProperty(obj, "foo", {get: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({get foo() {}})");

accessor = Object.getOwnPropertyDescriptor({ get bar() {} }, "bar").get;
obj = {};
Object.defineProperty(obj, "foo", {get: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({get foo() {}})");

accessor = Object.getOwnPropertyDescriptor({ set foo(v) {} }, "foo").set;
obj = {};
Object.defineProperty(obj, "foo", {get: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({get foo(v) {}})");

accessor = Object.getOwnPropertyDescriptor({ set bar(v) {} }, "bar").set;
obj = {};
Object.defineProperty(obj, "foo", {get: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({get foo(v) {}})");

accessor = Object.getOwnPropertyDescriptor({ get foo() {} }, "foo").get;
obj = {};
Object.defineProperty(obj, "foo", {set: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({set foo() {}})");

accessor = Object.getOwnPropertyDescriptor({ get bar() {} }, "bar").get;
obj = {};
Object.defineProperty(obj, "foo", {set: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({set foo() {}})");

accessor = Object.getOwnPropertyDescriptor({ set foo(v) {} }, "foo").set;
obj = {};
Object.defineProperty(obj, "foo", {set: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({set foo(v) {}})");

accessor = Object.getOwnPropertyDescriptor({ set bar(v) {} }, "bar").set;
obj = {};
Object.defineProperty(obj, "foo", {set: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({set foo(v) {}})");

// Methods with proxy.
// Treated as normal property.

method = ({foo() {}}).foo;
let handler = {
  get(that, name) {
    if (name == "toSource") {
      return function() {
        return that.toSource();
      };
    }
    return that[name];
  }
};
let proxy = new Proxy(method, handler);

obj = {};
Object.defineProperty(obj, "foo", {value: proxy, enumerable: true});
assertEq(obj.toSource(),
         "({foo:foo() {}})");

// Accessors with proxy.
// Accessor syntax is composed.

accessor = Object.getOwnPropertyDescriptor({ get foo() {} }, "foo").get;
proxy = new Proxy(accessor, handler);

obj = {};
Object.defineProperty(obj, "foo", {get: proxy, enumerable: true});
assertEq(obj.toSource(),
         "({get foo() {}})");

obj = {};
Object.defineProperty(obj, "foo", {set: proxy, enumerable: true});
assertEq(obj.toSource(),
         "({set foo() {}})");

// Methods from other global.
// Treated as normal property.

let g = newGlobal();

method = g.eval("({ foo() {} }).foo");

obj = {};
Object.defineProperty(obj, "foo", {value: method, enumerable: true});
assertEq(obj.toSource(),
         "({foo:foo() {}})");

// Accessors from other global.
// Accessor syntax is composed.

accessor = g.eval("Object.getOwnPropertyDescriptor({ get foo() {} }, 'foo').get");

obj = {};
Object.defineProperty(obj, "foo", {get: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({get foo() {}})");

accessor = g.eval("Object.getOwnPropertyDescriptor({ get bar() {} }, 'bar').get");

obj = {};
Object.defineProperty(obj, "foo", {get: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({get foo() {}})");

accessor = g.eval("Object.getOwnPropertyDescriptor({ set foo(v) {} }, 'foo').set");

obj = {};
Object.defineProperty(obj, "foo", {get: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({get foo(v) {}})");

accessor = g.eval("Object.getOwnPropertyDescriptor({ set bar(v) {} }, 'bar').set");

obj = {};
Object.defineProperty(obj, "foo", {get: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({get foo(v) {}})");

accessor = g.eval("Object.getOwnPropertyDescriptor({ get foo() {} }, 'foo').get");

obj = {};
Object.defineProperty(obj, "foo", {set: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({set foo() {}})");

accessor = g.eval("Object.getOwnPropertyDescriptor({ get bar() {} }, 'bar').get");

obj = {};
Object.defineProperty(obj, "foo", {set: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({set foo() {}})");

accessor = g.eval("Object.getOwnPropertyDescriptor({ set foo(v) {} }, 'foo').set");

obj = {};
Object.defineProperty(obj, "foo", {set: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({set foo(v) {}})");

accessor = g.eval("Object.getOwnPropertyDescriptor({ set bar(v) {} }, 'bar').set");

obj = {};
Object.defineProperty(obj, "foo", {set: accessor, enumerable: true});
assertEq(obj.toSource(),
         "({set foo(v) {}})");

// **** Some weird cases ****

// Accessors with generator or async.

obj = {};
Object.defineProperty(obj, "foo", {get: function*() {}, enumerable: true});
assertEq(obj.toSource(),
         "({get foo() {}})");

obj = {};
Object.defineProperty(obj, "foo", {set: async function() {}, enumerable: true});
assertEq(obj.toSource(),
         "({set foo() {}})");

// Modified toSource.

obj = { foo() {} };
obj.foo.toSource = () => "hello";
assertEq(obj.toSource(),
         "({hello})");

obj = { foo() {} };
obj.foo.toSource = () => "bar() {}";
assertEq(obj.toSource(),
         "({bar() {}})");

// Modified toSource with different method name.

obj = {};
Object.defineProperty(obj, "foo", {value: function bar() {}, enumerable: true});
obj.foo.toSource = () => "hello";
assertEq(obj.toSource(),
         "({foo:hello})");

obj = {};
Object.defineProperty(obj, "foo", {value: function* bar() {}, enumerable: true});
obj.foo.toSource = () => "hello";
assertEq(obj.toSource(),
         "({foo:hello})");

obj = {};
Object.defineProperty(obj, "foo", {value: async function bar() {}, enumerable: true});
obj.foo.toSource = () => "hello";
assertEq(obj.toSource(),
         "({foo:hello})");

if (typeof reportCompare === "function")
    reportCompare(true, true);