Index: test/mjsunit/harmony/proxies-construct.js |
diff --git a/test/mjsunit/harmony/proxies-construct.js b/test/mjsunit/harmony/proxies-construct.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cdb05b13e4a5db7f86582902bfb349878c994609 |
--- /dev/null |
+++ b/test/mjsunit/harmony/proxies-construct.js |
@@ -0,0 +1,97 @@ |
+// Copyright 2015 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-proxies --harmony-reflect |
+ |
+(function testNonConstructable() { |
+ var proxy = new Proxy({},{}); |
+ assertThrows(function(){ new proxy() }, TypeError); |
+ |
+ var proxy2 = new Proxy(proxy, {}); |
+ assertThrows(function(){ proxy2() }, TypeError); |
+})(); |
+ |
+(function testFailingConstructRevoked() { |
+ var pair = Proxy.revocable(Array, {}); |
+ var instance = new pair.proxy(); |
+ pair.revoke(); |
+ assertThrows(function(){ new pair.proxy() }, TypeError); |
+})(); |
+ |
+(function testFailingGetTrap() { |
+ var handler = { |
+ get: function() { |
+ throw TypeError(); |
+ } |
+ } |
+ var proxy = new Proxy({},{}); |
+ var proxy2 = new Proxy({}, proxy); |
+ assertThrows(function(){ new proxy2() }, TypeError); |
+})(); |
+ |
+(function testConstructFallback() { |
+ var called = false; |
+ function Target() { |
+ called = true; |
+ this.property1 = 'value1'; |
+ }; |
+ Target.prototype = {}; |
+ var proxy = new Proxy(Target, {}); |
+ |
+ assertFalse(called); |
+ var instance = new proxy(); |
+ assertTrue(called); |
+ assertEquals('value1', instance.property1); |
+ assertSame(Target.prototype, Reflect.getPrototypeOf(instance)); |
+ |
+ var proxy2 = new Proxy(proxy, {}); |
+ called = false; |
+ var instance2 = new proxy2(); |
+ assertTrue(called); |
+ assertEquals('value1', instance2.property1); |
+ assertSame(Target.prototype, Reflect.getPrototypeOf(instance)); |
+})(); |
+ |
+(function testConstructTrapDirectReturn() { |
+ function Target(a, b) { |
+ this.sum = a + b; |
+ }; |
+ var handler = { |
+ construct: function(t, c, args) { |
+ return { sum: 42 }; |
+ } |
+ }; |
+ var proxy = new Proxy(Target, handler); |
+ assertEquals(42, (new proxy(1, 2)).sum); |
+})(); |
+ |
+(function testConstructTrap() { |
+ function Target(arg1, arg2) { |
+ this.arg1 = arg1; |
+ this.arg2 = arg2; |
+ } |
+ var seend_target, seen_arguments, seen_new_target; |
Toon Verwaest
2015/12/08 20:27:22
seend? :)
Camillo Bruni
2015/12/09 12:15:44
see, seen, seend, isn't that obivous?
|
+ var handler = { |
+ construct: function(target, args, new_target) { |
+ seen_target = target; |
+ seen_arguments = args; |
+ seen_new_target = new_target; |
+ return Reflect.construct(target, args, new_target); |
+ } |
+ } |
+ var proxy = new Proxy(Target, handler); |
+ var instance = new proxy('a', 'b'); |
+ assertEquals(Target, seen_target); |
+ assertEquals(['a','b'], seen_arguments); |
+ assertEquals(proxy, seen_new_target); |
+ assertEquals('a', instance.arg1); |
+ assertEquals('b', instance.arg2); |
+ |
+ var instance2 = Reflect.construct(proxy, ['a1', 'b1'], Array); |
+ assertEquals(Target, seen_target); |
+ assertEquals(['a1', 'b1'], seen_arguments); |
+ assertEquals(Array, seen_new_target); |
+ assertEquals('a1', instance2.arg1); |
+ assertEquals('b1', instance2.arg2); |
+})() |