Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(505)

Side by Side Diff: test/mjsunit/harmony/unscopables.js

Issue 384963002: ES6 Unscopables (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Use new GetHolder Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --harmony-unscopables
6 // Flags: --harmony-collections
7
8 var global = this;
9 var globalProto = Object.getPrototypeOf(global);
10
11 // Number of objects being tested. There is an assert ensuring this is correct.
12 var objectCount = 21;
13
14
15 function runTest(f) {
16 function restore(object, oldProto) {
17 Object.setPrototypeOf(object, oldProto);
18 delete object[Symbol.unscopables];
19 delete object.x;
20 delete object.x_;
21 delete object.y;
22 delete object.z;
23 }
24
25 function getObject(i) {
26 var objects = [
27 {},
28 [],
29 function() {},
30 function() {
31 return arguments;
32 }(),
33 function() {
34 'use strict';
35 return arguments;
36 }(),
37 Object(1),
38 Object(true),
39 Object('bla'),
40 new Date,
41 new RegExp,
42 new Set,
43 new Map,
44 new WeakMap,
45 new WeakSet,
46 new ArrayBuffer(10),
47 new Int32Array(5),
48 Object,
49 Function,
50 Date,
51 RegExp,
52 global
53 ];
54
55 assertEquals(objectCount, objects.length);
56 return objects[i];
57 }
58
59 if (f.length === 1) {
60 for (var i = 0; i < objectCount; i++) {
61 var object = getObject(i);
62 var oldObjectProto = Object.getPrototypeOf(object);
63 f(object);
64 restore(object, oldObjectProto);
65 }
66 } else {
67 for (var i = 0; i < objectCount; i++) {
68 for (var j = 0; j < objectCount; j++) {
69 var object = getObject(i);
70 var proto = getObject(j);
71 if (object === proto) {
72 continue;
73 }
74 var oldObjectProto = Object.getPrototypeOf(object);
75 var oldProtoProto = Object.getPrototypeOf(proto);
76 f(object, proto);
77 restore(object, oldObjectProto);
78 restore(proto, oldProtoProto);
79 }
80 }
81 }
82 }
83
84
85 function TestBasics(object) {
86 var x = 1;
87 var y = 2;
88 var z = 3;
89 object.x = 4;
90 object.y = 5;
91
92 with (object) {
93 assertEquals(4, x);
94 assertEquals(5, y);
95 assertEquals(3, z);
96 }
97
98 object[Symbol.unscopables] = {x: true};
99 with (object) {
100 assertEquals(1, x);
101 assertEquals(5, y);
102 assertEquals(3, z);
103 }
104
105 object[Symbol.unscopables] = {x: 0, y: true};
106 with (object) {
107 assertEquals(1, x);
108 assertEquals(2, y);
109 assertEquals(3, z);
110 }
111 }
112 runTest(TestBasics);
113
114
115 function TestBasicsSet(object) {
116 var x = 1;
117 object.x = 2;
118
119 with (object) {
120 assertEquals(2, x);
121 }
122
123 object[Symbol.unscopables] = {x: true};
124 with (object) {
125 assertEquals(1, x);
126 x = 3;
127 assertEquals(3, x);
128 }
129
130 assertEquals(3, x);
131 assertEquals(2, object.x);
132 }
133 runTest(TestBasicsSet);
134
135
136 function TestOnProto(object, proto) {
137 var x = 1;
138 var y = 2;
139 var z = 3;
140 proto.x = 4;
141
142 Object.setPrototypeOf(object, proto);
143 object.y = 5;
144
145 with (object) {
146 assertEquals(4, x);
147 assertEquals(5, y);
148 assertEquals(3, z);
149 }
150
151 proto[Symbol.unscopables] = {x: true};
152 with (object) {
153 assertEquals(1, x);
154 assertEquals(5, y);
155 assertEquals(3, z);
156 }
157
158 object[Symbol.unscopables] = {y: true};
159 with (object) {
160 assertEquals(1, x);
161 assertEquals(2, y);
162 assertEquals(3, z);
163 }
164
165 proto[Symbol.unscopables] = {y: true};
166 object[Symbol.unscopables] = {x: true};
167 with (object) {
168 assertEquals(4, x);
169 assertEquals(5, y);
170 assertEquals(3, z);
171 }
172 }
173 runTest(TestOnProto);
174
175
176 function TestNonObject(object) {
177 var x = 1;
178 var y = 2;
179 object.x = 3;
180 object.y = 4;
181
182 object[Symbol.unscopables] = 'xy';
183 with (object) {
184 assertEquals(3, x);
185 assertEquals(4, y);
186 }
187
188 object[Symbol.unscopables] = null;
189 with (object) {
190 assertEquals(3, x);
191 assertEquals(4, y);
192 }
193 }
194 runTest(TestNonObject);
195
196
197 function TestChangeDuringWith(object) {
198 var x = 1;
199 var y = 2;
200 object.x = 3;
201 object.y = 4;
202
203 with (object) {
204 assertEquals(3, x);
205 assertEquals(4, y);
206 object[Symbol.unscopables] = {x: true};
207 assertEquals(1, x);
208 assertEquals(4, y);
209 }
210 }
211 runTest(TestChangeDuringWith);
212
213
214 function TestChangeDuringWithWithPossibleOptimization(object) {
215 var x = 1;
216 object.x = 2;
217 with (object) {
218 for (var i = 0; i < 1000; i++) {
219 if (i === 500) object[Symbol.unscopables] = {x: true};
220 assertEquals(i < 500 ? 2: 1, x);
221 }
222 }
223 }
224 TestChangeDuringWithWithPossibleOptimization({});
225
226
227 function TestChangeDuringWithWithPossibleOptimization2(object) {
228 var x = 1;
229 object.x = 2;
230 object[Symbol.unscopables] = {x: true};
231 with (object) {
232 for (var i = 0; i < 1000; i++) {
233 if (i === 500) delete object[Symbol.unscopables];
234 assertEquals(i < 500 ? 1 : 2, x);
235 }
236 }
237 }
238 TestChangeDuringWithWithPossibleOptimization2({});
239
240
241 function TestChangeDuringWithWithPossibleOptimization3(object) {
242 var x = 1;
243 object.x = 2;
244 object[Symbol.unscopables] = {};
245 with (object) {
246 for (var i = 0; i < 1000; i++) {
247 if (i === 500) object[Symbol.unscopables].x = true;
248 assertEquals(i < 500 ? 2 : 1, x);
249 }
250 }
251 }
252 TestChangeDuringWithWithPossibleOptimization3({});
253
254
255 function TestChangeDuringWithWithPossibleOptimization4(object) {
256 var x = 1;
257 object.x = 2;
258 object[Symbol.unscopables] = {x: true};
259 with (object) {
260 for (var i = 0; i < 1000; i++) {
261 if (i === 500) delete object[Symbol.unscopables].x;
262 assertEquals(i < 500 ? 1 : 2, x);
263 }
264 }
265 }
266 TestChangeDuringWithWithPossibleOptimization4({});
267
268
269 (function TestGlobal() {
270 global.values = 'global.values';
271 Array.prototype.values = 'Array.prototype.values';
272 Array.prototype[Symbol.unscopables] = {values: true};
273 Array.prototype.__proto__ = {values: 42};
274 var array = [];
275 with (array) {
276 assertEquals(42, values);
277 }
278 })();
279
280
281 function TestAccessorReceiver(object, proto) {
282 var x = 'local';
283
284 Object.defineProperty(proto, 'x', {
285 get: function() {
286 assertEquals(object, this);
287 return this.x_;
288 },
289 configurable: true
290 });
291 proto.x_ = 'proto';
292
293 Object.setPrototypeOf(object, proto);
294 proto.x_ = 'object';
295
296 with (object) {
297 assertEquals('object', x);
298 }
299 }
300 runTest(TestAccessorReceiver);
301
302
303 function TestUnscopablesGetter(object) {
304 // This test gets really messy when object is the global since the assert
305 // functions are properties on the global object and the call count gets
306 // completely different.
307 if (object === global) return;
308
309 var x = 'local';
310 object.x = 'object';
311
312 var callCount = 0;
313 Object.defineProperty(object, Symbol.unscopables, {
314 get: function() {
315 callCount++;
316 return {};
317 },
318 configurable: true
319 });
320 with (object) {
321 assertEquals('object', x);
322 }
323 // Once for HasBinding and once for GetBindingValue
324 assertEquals(2, callCount);
325
326 callCount = 0;
327 Object.defineProperty(object, Symbol.unscopables, {
328 get: function() {
329 callCount++;
330 return {x: true};
331 },
332 configurable: true
333 });
334 with (object) {
335 assertEquals('local', x);
336 }
337 // Once for HasBinding
338 assertEquals(1, callCount);
339
340 callCount = 0;
341 Object.defineProperty(object, Symbol.unscopables, {
342 get: function() {
343 callCount++;
344 return callCount === 1 ? {} : {x: true};
345 },
346 configurable: true
347 });
348 with (object) {
349 assertEquals(void 0, x);
350 }
351 // Once for HasBinding, once for GetBindingValue.
352 assertEquals(2, callCount);
353
354 callCount = 0;
355 Object.defineProperty(object, Symbol.unscopables, {
356 get: function() {
357 callCount++;
358 return callCount === 1 ? {} : {x: true};
359 },
360 configurable: true
361 });
362 with (object) {
363 x = 1;
364 }
365 // Once for HasBinding
366 assertEquals(1, callCount);
367 assertEquals(1, object.x);
368 assertEquals('local', x);
369 with (object) {
370 x = 2;
371 }
372 // One more HasBinding.
373 assertEquals(2, callCount);
374 assertEquals(1, object.x);
375 assertEquals(2, x);
376 }
377 runTest(TestUnscopablesGetter);
378
379
380 var global = this;
381 (function TestUnscopablesGetter2() {
382 var x = 'local';
383
384 var protos = [{}, [], function() {}, global];
385 var objects = [{}, [], function() {}];
386
387 protos.forEach(function(proto) {
388 objects.forEach(function(object) {
389 Object.defineProperty(proto, 'x', {
390 get: function() {
391 assertEquals(object, this);
392 return 'proto';
393 },
394 configurable: true
395 });
396
397 object.__proto__ = proto;
398 Object.defineProperty(object, 'x', {
399 get: function() {
400 assertEquals(object, this);
401 return 'object';
402 },
403 configurable: true
404 });
405
406 with (object) {
407 assertEquals('object', x);
408 }
409
410 object[Symbol.unscopables] = {x: true};
411 with (object) {
412 assertEquals('proto', x);
413 }
414
415 delete proto[Symbol.unscopables];
416 delete object[Symbol.unscopables];
417 });
418 });
419 })();
420
421
422 function TestSetterOnBlacklisted(object, proto) {
423 Object.defineProperty(proto, 'x', {
424 set: function(x) {
425 assertUnreachable();
426 },
427 get: function() {
428 return 'proto';
429 },
430 configurable: true
431 });
432 Object.setPrototypeOf(object, proto);
433 Object.defineProperty(object, 'x', {
434 get: function() {
435 return this.x_;
436 },
437 set: function(x) {
438 this.x_ = x;
439 },
440 configurable: true
441 });
442 object.x_ = 1;
443
444 with (object) {
445 x = 2;
446 assertEquals(2, x);
447 }
448
449 assertEquals(2, object.x);
450
451 object[Symbol.unscopables] = {x: true};
452
453 with (object) {
454 x = 3;
455 assertEquals('proto', x);
456 }
457
458 assertEquals(3, object.x);
459 }
460 runTest(TestSetterOnBlacklisted);
461
462
463 function TestObjectsAsUnscopables(object, unscopables) {
464 var x = 1;
465 object.x = 2;
466
467 with (object) {
468 assertEquals(2, x);
469 object[Symbol.unscopables] = unscopables;
470 assertEquals(2, x);
471 }
472 }
473 runTest(TestAccessorOnUnscopables);
474
475
476 function TestAccessorOnUnscopables(object) {
477 var x = 1;
478 object.x = 2;
479
480 var unscopables = {
481 get x() {
482 assertUnreachable();
483 }
484 };
485
486 with (object) {
487 assertEquals(2, x);
488 object[Symbol.unscopables] = unscopables;
489 assertEquals(1, x);
490 }
491 }
492 runTest(TestAccessorOnUnscopables);
493
494
495 function TestLengthUnscopables(object, proto) {
496 var length = 2;
497 with (object) {
498 assertEquals(1, length);
499 object[Symbol.unscopables] = {length: true};
500 assertEquals(0, length); // From prototype
501 proto[Symbol.unscopables] = {length: true};
502 assertEquals(2, length);
503 delete proto[Symbol.unscopables];
504 assertEquals(0, length); // From prototype
505 }
506 }
507 TestLengthUnscopables([1], Array.prototype);
508 TestLengthUnscopables(function(x) {}, Function.prototype);
509 TestLengthUnscopables(new String('x'), String.prototype);
510
511
512 function TestFunctionNameUnscopables(object) {
513 var name = 'local';
514 with (object) {
515 assertEquals('f', name);
516 object[Symbol.unscopables] = {name: true};
517 assertEquals('Empty', name); // From prototype
518 Object.getPrototypeOf(object)[Symbol.unscopables] = {name: true};
519 assertEquals('local', name);
520 delete Object.getPrototypeOf(object)[Symbol.unscopables];
521 assertEquals('Empty', name); // From prototype
522 }
523 }
524 TestFunctionNameUnscopables(function f() {});
525
526
527 function TestFunctionPrototypeUnscopables() {
528 var prototype = 'local';
529 var f = function() {};
530 var g = function() {};
531 Object.setPrototypeOf(f, g);
532 var fp = f.prototype;
533 var gp = g.prototype;
534 with (f) {
535 assertEquals(fp, prototype);
536 f[Symbol.unscopables] = {prototype: true};
537 assertEquals(gp, prototype);
538 g[Symbol.unscopables] = {prototype: true};
539 assertEquals('local', prototype);
540 }
541 }
542 TestFunctionPrototypeUnscopables(function() {});
543
544
545 function TestFunctionArgumentsUnscopables() {
546 var func = function() {
547 var arguments = 'local';
548 var args = func.arguments;
549 with (func) {
550 assertEquals(args, arguments);
551 func[Symbol.unscopables] = {arguments: true};
552 assertEquals(null, arguments); // From prototype
553 Function.prototype[Symbol.unscopables] = {arguments: true};
554 assertEquals('local', arguments);
555 delete Function.prototype[Symbol.unscopables];
556 assertEquals(null, arguments); // From prototype
557 }
558 }
559 func(1);
560 }
561 TestFunctionArgumentsUnscopables();
562
563
564 function TestArgumentsLengthUnscopables() {
565 var func = function() {
566 var length = 'local';
567 with (arguments) {
568 assertEquals(1, length);
569 arguments[Symbol.unscopables] = {length: true};
570 assertEquals('local', length);
571 }
572 }
573 func(1);
574 }
575 TestArgumentsLengthUnscopables();
576
577
578 function TestFunctionCallerUnscopables() {
579 var func = function() {
580 var caller = 'local';
581 with (func) {
582 assertEquals(TestFunctionCallerUnscopables, caller);
583 func[Symbol.unscopables] = {caller: true};
584 assertEquals(null, caller); // From prototype
585 Function.prototype[Symbol.unscopables] = {caller: true};
586 assertEquals('local', caller);
587 delete Function.prototype[Symbol.unscopables];
588 assertEquals(null, caller); // From prototype
589 }
590 }
591 func(1);
592 }
593 TestFunctionCallerUnscopables();
594
595
596 function TestGetUnscopablesGetterThrows() {
597 var object = {
598 get x() {
599 assertUnreachable();
600 }
601 };
602 function CustomError() {}
603 Object.defineProperty(object, Symbol.unscopables, {
604 get: function() {
605 throw new CustomError();
606 }
607 });
608 assertThrows(function() {
609 with (object) {
610 x;
611 }
612 }, CustomError);
613 }
614 TestGetUnscopablesGetterThrows();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698