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

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: Updated to reflect the July 2014 consensus 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 delete object[Symbol.unscopables];
18 delete object.x;
19 delete object.x_;
20 delete object.y;
21 delete object.z;
22 Object.setPrototypeOf(object, oldProto);
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 TestUnscopableChain(object) {
116 var x = 1;
117 object.x = 2;
118
119 with (object) {
120 assertEquals(2, x);
121 }
122
123 object[Symbol.unscopables] = {
124 __proto__: {x: true}
125 };
126 with (object) {
127 assertEquals(1, x);
128 }
129 }
130 runTest(TestUnscopableChain);
131
132
133 function TestBasicsSet(object) {
134 var x = 1;
135 object.x = 2;
136
137 with (object) {
138 assertEquals(2, x);
139 }
140
141 object[Symbol.unscopables] = {x: true};
142 with (object) {
143 assertEquals(1, x);
144 x = 3;
145 assertEquals(3, x);
146 }
147
148 assertEquals(3, x);
149 assertEquals(2, object.x);
150 }
151 runTest(TestBasicsSet);
152
153
154 function TestOnProto(object, proto) {
155 var x = 1;
156 var y = 2;
157 var z = 3;
158 proto.x = 4;
159
160 Object.setPrototypeOf(object, proto);
161 object.y = 5;
162
163 with (object) {
164 assertEquals(4, x);
165 assertEquals(5, y);
166 assertEquals(3, z);
167 }
168
169 proto[Symbol.unscopables] = {x: true};
170 with (object) {
171 assertEquals(1, x);
172 assertEquals(5, y);
173 assertEquals(3, z);
174 }
175
176 object[Symbol.unscopables] = {y: true};
177 with (object) {
178 assertEquals(4, x);
179 assertEquals(2, y);
180 assertEquals(3, z);
181 }
182
183 proto[Symbol.unscopables] = {y: true};
184 object[Symbol.unscopables] = {x: true};
185 with (object) {
186 assertEquals(1, x);
187 assertEquals(5, y);
188 assertEquals(3, z);
189 }
190 }
191 runTest(TestOnProto);
192
193
194 function TestSetBlockedOnProto(object, proto) {
195 var x = 1;
196 object.x = 2;
197
198 with (object) {
199 assertEquals(2, x);
200 }
201
202 Object.setPrototypeOf(object, proto);
203 proto[Symbol.unscopables] = {x: true};
204 with (object) {
205 assertEquals(1, x);
206 x = 3;
207 assertEquals(3, x);
208 }
209
210 assertEquals(3, x);
211 assertEquals(2, object.x);
212 }
213 runTest(TestSetBlockedOnProto);
214
215
216 function TestNonObject(object) {
217 var x = 1;
218 var y = 2;
219 object.x = 3;
220 object.y = 4;
221
222 object[Symbol.unscopables] = 'xy';
223 with (object) {
224 assertEquals(3, x);
225 assertEquals(4, y);
226 }
227
228 object[Symbol.unscopables] = null;
229 with (object) {
230 assertEquals(3, x);
231 assertEquals(4, y);
232 }
233 }
234 runTest(TestNonObject);
235
236
237 function TestChangeDuringWith(object) {
238 var x = 1;
239 var y = 2;
240 object.x = 3;
241 object.y = 4;
242
243 with (object) {
244 assertEquals(3, x);
245 assertEquals(4, y);
246 object[Symbol.unscopables] = {x: true};
247 assertEquals(1, x);
248 assertEquals(4, y);
249 }
250 }
251 runTest(TestChangeDuringWith);
252
253
254 function TestChangeDuringWithWithPossibleOptimization(object) {
255 var x = 1;
256 object.x = 2;
257 with (object) {
258 for (var i = 0; i < 1000; i++) {
259 if (i === 500) object[Symbol.unscopables] = {x: true};
260 assertEquals(i < 500 ? 2: 1, x);
261 }
262 }
263 }
264 TestChangeDuringWithWithPossibleOptimization({});
265
266
267 function TestChangeDuringWithWithPossibleOptimization2(object) {
268 var x = 1;
269 object.x = 2;
270 object[Symbol.unscopables] = {x: true};
271 with (object) {
272 for (var i = 0; i < 1000; i++) {
273 if (i === 500) delete object[Symbol.unscopables];
274 assertEquals(i < 500 ? 1 : 2, x);
275 }
276 }
277 }
278 TestChangeDuringWithWithPossibleOptimization2({});
279
280
281 function TestChangeDuringWithWithPossibleOptimization3(object) {
282 var x = 1;
283 object.x = 2;
284 object[Symbol.unscopables] = {};
285 with (object) {
286 for (var i = 0; i < 1000; i++) {
287 if (i === 500) object[Symbol.unscopables].x = true;
288 assertEquals(i < 500 ? 2 : 1, x);
289 }
290 }
291 }
292 TestChangeDuringWithWithPossibleOptimization3({});
293
294
295 function TestChangeDuringWithWithPossibleOptimization4(object) {
296 var x = 1;
297 object.x = 2;
298 object[Symbol.unscopables] = {x: true};
299 with (object) {
300 for (var i = 0; i < 1000; i++) {
301 if (i === 500) delete object[Symbol.unscopables].x;
302 assertEquals(i < 500 ? 1 : 2, x);
303 }
304 }
305 }
306 TestChangeDuringWithWithPossibleOptimization4({});
307
308
309 function TestAccessorReceiver(object, proto) {
310 var x = 'local';
311
312 Object.defineProperty(proto, 'x', {
313 get: function() {
314 assertEquals(object, this);
315 return this.x_;
316 },
317 configurable: true
318 });
319 proto.x_ = 'proto';
320
321 Object.setPrototypeOf(object, proto);
322 proto.x_ = 'object';
323
324 with (object) {
325 assertEquals('object', x);
326 }
327 }
328 runTest(TestAccessorReceiver);
329
330
331 function TestUnscopablesGetter(object) {
332 // This test gets really messy when object is the global since the assert
333 // functions are properties on the global object and the call count gets
334 // completely different.
335 if (object === global) return;
336
337 var x = 'local';
338 object.x = 'object';
339
340 var callCount = 0;
341 Object.defineProperty(object, Symbol.unscopables, {
342 get: function() {
343 callCount++;
344 return {};
345 },
346 configurable: true
347 });
348 with (object) {
349 assertEquals('object', x);
350 }
351 // Once for HasBinding
352 assertEquals(1, callCount);
353
354 callCount = 0;
355 Object.defineProperty(object, Symbol.unscopables, {
356 get: function() {
357 callCount++;
358 return {x: true};
359 },
360 configurable: true
361 });
362 with (object) {
363 assertEquals('local', x);
364 }
365 // Once for HasBinding
366 assertEquals(1, callCount);
367
368 callCount = 0;
369 Object.defineProperty(object, Symbol.unscopables, {
370 get: function() {
371 callCount++;
372 return callCount == 1 ? {} : {x: true};
373 },
374 configurable: true
375 });
376 with (object) {
377 x = 1;
378 }
379 // Once for HasBinding
380 assertEquals(1, callCount);
381 assertEquals(1, object.x);
382 assertEquals('local', x);
383 with (object) {
384 x = 2;
385 }
386 // One more HasBinding.
387 assertEquals(2, callCount);
388 assertEquals(1, object.x);
389 assertEquals(2, x);
390 }
391 runTest(TestUnscopablesGetter);
392
393
394 var global = this;
395 function TestUnscopablesGetter2() {
396 var x = 'local';
397
398 var globalProto = Object.getPrototypeOf(global);
399 var protos = [{}, [], function() {}, global];
400 var objects = [{}, [], function() {}];
401
402 protos.forEach(function(proto) {
403 objects.forEach(function(object) {
404 Object.defineProperty(proto, 'x', {
405 get: function() {
406 assertEquals(object, this);
407 return 'proto';
408 },
409 configurable: true
410 });
411
412 object.__proto__ = proto;
413 Object.defineProperty(object, 'x', {
414 get: function() {
415 assertEquals(object, this);
416 return 'object';
417 },
418 configurable: true
419 });
420
421 with (object) {
422 assertEquals('object', x);
423 }
424
425 object[Symbol.unscopables] = {x: true};
426 with (object) {
427 assertEquals('local', x);
428 }
429
430 delete proto[Symbol.unscopables];
431 delete object[Symbol.unscopables];
432 });
433 });
434
435 delete global.x;
436 Object.setPrototypeOf(global, globalProto);
437 }
438 TestUnscopablesGetter2();
439
440
441 function TestSetterOnBlacklisted(object, proto) {
442 var x = 'local';
443 Object.defineProperty(proto, 'x', {
444 set: function(x) {
445 assertUnreachable();
446 },
447 get: function() {
448 return 'proto';
449 },
450 configurable: true
451 });
452 Object.setPrototypeOf(object, proto);
453 Object.defineProperty(object, 'x', {
454 get: function() {
455 return this.x_;
456 },
457 set: function(x) {
458 this.x_ = x;
459 },
460 configurable: true
461 });
462 object.x_ = 1;
463
464 with (object) {
465 x = 2;
466 assertEquals(2, x);
467 }
468
469 assertEquals(2, object.x);
470
471 object[Symbol.unscopables] = {x: true};
472
473 with (object) {
474 x = 3;
475 assertEquals(3, x);
476 }
477
478 assertEquals(2, object.x);
479 }
480 runTest(TestSetterOnBlacklisted);
481
482
483 function TestObjectsAsUnscopables(object, unscopables) {
484 var x = 1;
485 object.x = 2;
486
487 with (object) {
488 assertEquals(2, x);
489 object[Symbol.unscopables] = unscopables;
490 assertEquals(2, x);
491 }
492 }
493 runTest(TestObjectsAsUnscopables);
494
495
496 function TestAccessorOnUnscopables(object) {
497 var x = 1;
498 object.x = 2;
499
500 var unscopables = {
501 get x() {
502 assertUnreachable();
503 }
504 };
505
506 with (object) {
507 assertEquals(2, x);
508 object[Symbol.unscopables] = unscopables;
509 assertEquals(1, x);
510 }
511 }
512 runTest(TestAccessorOnUnscopables);
513
514
515 function TestLengthUnscopables(object, proto) {
516 var length = 2;
517 with (object) {
518 assertEquals(1, length);
519 object[Symbol.unscopables] = {length: true};
520 assertEquals(2, length);
521 delete object[Symbol.unscopables];
522 assertEquals(1, length);
523 }
524 }
525 TestLengthUnscopables([1], Array.prototype);
526 TestLengthUnscopables(function(x) {}, Function.prototype);
527 TestLengthUnscopables(new String('x'), String.prototype);
528
529
530 function TestFunctionNameUnscopables(object) {
531 var name = 'local';
532 with (object) {
533 assertEquals('f', name);
534 object[Symbol.unscopables] = {name: true};
535 assertEquals('local', name);
536 delete object[Symbol.unscopables];
537 assertEquals('f', name);
538 }
539 }
540 TestFunctionNameUnscopables(function f() {});
541
542
543 function TestFunctionPrototypeUnscopables() {
544 var prototype = 'local';
545 var f = function() {};
546 var g = function() {};
547 Object.setPrototypeOf(f, g);
548 var fp = f.prototype;
549 var gp = g.prototype;
550 with (f) {
551 assertEquals(fp, prototype);
552 f[Symbol.unscopables] = {prototype: true};
553 assertEquals('local', prototype);
554 delete f[Symbol.unscopables];
555 assertEquals(fp, prototype);
556 }
557 }
558 TestFunctionPrototypeUnscopables(function() {});
559
560
561 function TestFunctionArgumentsUnscopables() {
562 var func = function() {
563 var arguments = 'local';
564 var args = func.arguments;
565 with (func) {
566 assertEquals(args, arguments);
567 func[Symbol.unscopables] = {arguments: true};
568 assertEquals('local', arguments);
569 delete func[Symbol.unscopables];
570 assertEquals(args, arguments);
571 }
572 }
573 func(1);
574 }
575 TestFunctionArgumentsUnscopables();
576
577
578 function TestArgumentsLengthUnscopables() {
579 var func = function() {
580 var length = 'local';
581 with (arguments) {
582 assertEquals(1, length);
583 arguments[Symbol.unscopables] = {length: true};
584 assertEquals('local', length);
585 }
586 }
587 func(1);
588 }
589 TestArgumentsLengthUnscopables();
590
591
592 function TestFunctionCallerUnscopables() {
593 var func = function() {
594 var caller = 'local';
595 with (func) {
596 assertEquals(TestFunctionCallerUnscopables, caller);
597 func[Symbol.unscopables] = {caller: true};
598 assertEquals('local', caller);
599 delete func[Symbol.unscopables];
600 assertEquals(TestFunctionCallerUnscopables, caller);
601 }
602 }
603 func(1);
604 }
605 TestFunctionCallerUnscopables();
606
607
608 function TestGetUnscopablesGetterThrows() {
609 var object = {
610 get x() {
611 assertUnreachable();
612 }
613 };
614 function CustomError() {}
615 Object.defineProperty(object, Symbol.unscopables, {
616 get: function() {
617 throw new CustomError();
618 }
619 });
620 assertThrows(function() {
621 with (object) {
622 x;
623 }
624 }, CustomError);
625 }
626 TestGetUnscopablesGetterThrows();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698