Index: test/mjsunit/harmony/unscopables.js |
diff --git a/test/mjsunit/harmony/unscopables.js b/test/mjsunit/harmony/unscopables.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2ab4da5a7bb7fc3793e1eb7872ffe747cb723134 |
--- /dev/null |
+++ b/test/mjsunit/harmony/unscopables.js |
@@ -0,0 +1,389 @@ |
+// Copyright 2014 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Flags: --harmony-unscopables |
+ |
+var global = this; |
+var globalProto = Object.getPrototypeOf(global); |
+ |
+ |
+function runtTest(f) { |
rossberg
2014/07/17 15:12:13
Nit: s/runtTest/runTest/
arv (Not doing code reviews)
2014/07/17 23:34:36
Done.
|
+ function restoreGlobal() { |
+ Object.setPrototypeOf(global, globalProto); |
+ delete global[Symbol.unscopables]; |
+ delete global.x; |
+ delete global.x_; |
+ delete global.y; |
+ delete global.z; |
+ } |
+ |
+ function getObject(i) { |
+ switch (i) { |
+ case 0: return {}; |
rossberg
2014/07/17 15:12:13
Why not simply use an array? Then you don't need t
arv (Not doing code reviews)
2014/07/17 23:34:37
These needs to be new objects every time but I agr
|
+ case 1: return []; |
+ case 2: return function() {}; |
+ case 3: return global; |
+ } |
+ } |
+ |
+ if (f.length === 1) { |
+ for (var i = 0; i < 4; i++) { |
+ f(getObject(i)); |
+ restoreGlobal(); |
+ } |
+ } else { |
+ for (var i = 0; i < 4; i++) { |
+ for (var j = 0; j < 4; j++) { |
+ var object = getObject(i); |
+ var proto = getObject(i); |
rossberg
2014/07/17 15:12:13
getObject(j) ? Otherwise, you don't execute anythi
arv (Not doing code reviews)
2014/07/17 23:34:37
wow. That refactoring just completely failed.
|
+ if (object === proto) { |
+ continue; |
+ } |
+ f(getObject(i), getObject(j)); |
rossberg
2014/07/17 15:12:13
f(object, proto) ?
arv (Not doing code reviews)
2014/07/17 23:34:37
Done.
|
+ restoreGlobal(); |
+ } |
+ } |
+ } |
+} |
+ |
+ |
+function TestBasics(object) { |
+ var x = 1; |
+ var y = 2; |
+ var z = 3; |
+ object.x = 4; |
+ object.y = 5; |
+ |
+ with (object) { |
+ assertEquals(4, x); |
+ assertEquals(5, y); |
+ assertEquals(3, z); |
+ } |
+ |
+ object[Symbol.unscopables] = {x: true}; |
+ with (object) { |
+ assertEquals(1, x); |
+ assertEquals(5, y); |
+ assertEquals(3, z); |
+ } |
+ |
+ object[Symbol.unscopables] = {x: 0, y: true}; |
+ with (object) { |
+ assertEquals(1, x); |
+ assertEquals(2, y); |
+ assertEquals(3, z); |
+ } |
+} |
+runtTest(TestBasics); |
+ |
+ |
+function TestBasicsSet(object) { |
+ var x = 1; |
+ object.x = 2; |
+ |
+ with (object) { |
+ assertEquals(2, x); |
+ } |
+ |
+ object[Symbol.unscopables] = {x: true}; |
+ with (object) { |
+ assertEquals(1, x); |
+ x = 3; |
+ assertEquals(3, x); |
+ } |
+ |
+ assertEquals(3, x); |
+ assertEquals(2, object.x); |
+} |
+runtTest(TestBasicsSet); |
+ |
+ |
+function TestOnProto(object, proto) { |
+ var x = 1; |
+ var y = 2; |
+ var z = 3; |
+ proto.x = 4; |
+ |
+ Object.setPrototypeOf(object, proto); |
+ object.y = 5; |
+ |
+ with (object) { |
+ assertEquals(4, x); |
+ assertEquals(5, y); |
+ assertEquals(3, z); |
+ } |
+ |
+ proto[Symbol.unscopables] = {x: true}; |
+ with (object) { |
+ assertEquals(1, x); |
+ assertEquals(5, y); |
+ assertEquals(3, z); |
+ } |
+ |
+ object[Symbol.unscopables] = {y: true}; |
+ with (object) { |
+ assertEquals(1, x); |
+ assertEquals(2, y); |
+ assertEquals(3, z); |
+ } |
+ |
+ proto[Symbol.unscopables] = {y: true}; |
+ object[Symbol.unscopables] = {x: true}; |
+ with (object) { |
+ assertEquals(4, x); |
+ assertEquals(5, y); |
+ assertEquals(3, z); |
+ } |
+} |
+runtTest(TestOnProto); |
+ |
+ |
+function TestNonObject(object) { |
+ var x = 1; |
+ var y = 2; |
+ object.x = 3; |
+ object.y = 4; |
+ |
+ object[Symbol.unscopables] = 'xy'; |
+ with (object) { |
+ assertEquals(3, x); |
+ assertEquals(4, y); |
+ } |
+ |
+ object[Symbol.unscopables] = null; |
+ with (object) { |
+ assertEquals(3, x); |
+ assertEquals(4, y); |
+ } |
+} |
+runtTest(TestNonObject); |
+ |
+ |
+function TestChangeDuringWith(object) { |
+ var x = 1; |
+ var y = 2; |
+ object.x = 3; |
+ object.y = 4; |
+ |
+ with (object) { |
+ assertEquals(3, x); |
+ assertEquals(4, y); |
+ object[Symbol.unscopables] = {x: true}; |
+ assertEquals(1, x); |
+ assertEquals(4, y); |
+ } |
+} |
+runtTest(TestChangeDuringWith); |
+ |
+ |
+function TestChangeDuringWithWithPossibleOptimization(object) { |
rossberg
2014/07/17 15:12:13
I had in mind a test like the following:
var x =
arv (Not doing code reviews)
2014/07/17 23:34:37
Thanks. Added.
|
+ var x = 1; |
+ object.x = 2; |
+ |
+ with (object) { |
+ for (var i = 0; i < 1e3; i++) { |
+ assertEquals(2, x); |
+ } |
+ object[Symbol.unscopables] = {x: true}; |
+ for (var i = 0; i < 1e3; i++) { |
+ assertEquals(1, x); |
+ } |
+ } |
+} |
+runtTest(TestChangeDuringWithWithPossibleOptimization); |
+ |
+ |
+var global = this; |
+(function TestGlobal() { |
+ global.values = 'global.values'; |
+ Array.prototype.values = 'Array.prototype.values'; |
+ Array.prototype[Symbol.unscopables] = {values: true}; |
+ Array.prototype.__proto__ = {values: 42}; |
+ var array = []; |
+ with (array) { |
+ assertEquals(42, values); |
+ } |
+})(); |
+ |
+ |
+function TestAccessorReceiver(object, proto) { |
+ var x = 'local'; |
+ |
+ Object.defineProperty(proto, 'x', { |
+ get: function() { |
+ assertEquals(object, this); |
+ return this.x_; |
+ }, |
+ configurable: true |
+ }); |
+ proto.x_ = 'proto'; |
+ |
+ Object.setPrototypeOf(object, proto); |
+ proto.x_ = 'object'; |
+ |
+ with (object) { |
+ assertEquals('object', x); |
+ } |
+} |
+runtTest(TestAccessorReceiver); |
+ |
+ |
+function TestUnscopablesGetter(object) { |
+ // This test gets really messy when object is the global since the assert |
+ // functions are properties on the global object and the call count gets |
+ // completely different. |
+ if (object === global) return; |
+ |
+ var x = 'local'; |
+ object.x = 'object'; |
+ |
+ var callCount = 0; |
+ Object.defineProperty(object, Symbol.unscopables, { |
+ get: function() { |
+ callCount++; |
+ return {}; |
+ }, |
+ configurable: true |
+ }); |
+ with (object) { |
+ assertEquals('object', x); |
+ } |
+ // Once for HasBinding and once for GetBindingValue |
+ assertEquals(2, callCount); |
+ |
+ callCount = 0; |
+ Object.defineProperty(object, Symbol.unscopables, { |
+ get: function() { |
+ callCount++; |
+ return {x: true}; |
+ }, |
+ configurable: true |
+ }); |
+ with (object) { |
+ assertEquals('local', x); |
+ } |
+ // Once for HasBinding |
+ assertEquals(1, callCount); |
+ |
+ callCount = 0; |
+ Object.defineProperty(object, Symbol.unscopables, { |
+ get: function() { |
+ callCount++; |
+ return callCount === 1 ? {} : {x: true}; |
+ }, |
+ configurable: true |
+ }); |
+ with (object) { |
+ assertEquals(void 0, x); |
+ } |
+ // Once for HasBinding, once for GetBindingValue. |
+ assertEquals(2, callCount); |
+ |
+ callCount = 0; |
+ Object.defineProperty(object, Symbol.unscopables, { |
+ get: function() { |
+ callCount++; |
+ return callCount === 1 ? {} : {x: true}; |
+ }, |
+ configurable: true |
+ }); |
+ with (object) { |
+ x = 1; |
+ } |
+ // Once for HasBinding |
+ assertEquals(1, callCount); |
+ assertEquals(1, object.x); |
+ assertEquals('local', x); |
+ with (object) { |
+ x = 2; |
+ } |
+ // One more HasBinding. |
+ assertEquals(2, callCount); |
+ assertEquals(1, object.x); |
+ assertEquals(2, x); |
+} |
+runtTest(TestUnscopablesGetter); |
+ |
+ |
+var global = this; |
+(function TestUnscopablesGetter2() { |
+ var x = 'local'; |
+ |
+ var protos = [{}, [], function() {}, global]; |
+ var objects = [{}, [], function() {}]; |
+ |
+ protos.forEach(function(proto) { |
+ objects.forEach(function(object) { |
+ Object.defineProperty(proto, 'x', { |
+ get: function() { |
+ assertEquals(object, this); |
+ return 'proto'; |
+ }, |
+ configurable: true |
+ }); |
+ |
+ object.__proto__ = proto; |
+ Object.defineProperty(object, 'x', { |
+ get: function() { |
+ assertEquals(object, this); |
+ return 'object'; |
+ }, |
+ configurable: true |
+ }); |
+ |
+ with (object) { |
+ assertEquals('object', x); |
+ } |
+ |
+ object[Symbol.unscopables] = {x: true}; |
+ with (object) { |
+ assertEquals('proto', x); |
+ } |
+ |
+ delete proto[Symbol.unscopables]; |
+ delete object[Symbol.unscopables]; |
+ }); |
+ }); |
+})(); |
+ |
+ |
+function TestSetterOnBlacklisted(object, proto) { |
+ Object.defineProperty(proto, 'x', { |
+ set: function(x) { |
+ assertTrue(false); |
rossberg
2014/07/17 15:12:13
assertUnreachable()
arv (Not doing code reviews)
2014/07/17 23:34:37
Done.
|
+ }, |
+ get: function() { |
+ return 'proto'; |
+ }, |
+ configurable: true |
+ }); |
+ Object.setPrototypeOf(object, proto); |
+ Object.defineProperty(object, 'x', { |
+ get: function() { |
+ return this.x_; |
+ }, |
+ set: function(x) { |
+ this.x_ = x; |
+ }, |
+ configurable: true |
+ }); |
+ object.x_ = 1; |
+ |
+ with (object) { |
+ x = 2; |
+ assertEquals(2, x); |
+ } |
+ |
+ assertEquals(2, object.x); |
+ |
+ object[Symbol.unscopables] = {x: true}; |
+ |
+ with (object) { |
+ x = 3; |
rossberg
2014/07/17 15:12:13
Yeah, what this does is really weird...
arv (Not doing code reviews)
2014/07/17 23:34:37
Acknowledged.
|
+ assertEquals('proto', x); |
+ } |
+ |
+ assertEquals(3, object.x); |
+} |
+runtTest(TestSetterOnBlacklisted); |