1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Copyright 2015 the V8 project authors. All rights reserved. 16// Use of this source code is governed by a BSD-style license that can be 17// found in the LICENSE file. 18 19var target = function () {}; 20var handler = { construct (target) { 21 throw 42; 22}}; 23 24var proxy = new Proxy(target, handler); 25 26try { 27 // opfunc_call 28 new proxy(5) 29 assert(false); 30} catch (e) { 31 assert(e === 42); 32} 33 34try { 35 // 22.1.2.3.4.a 36 Array.of.call(proxy); 37 assert(false); 38} catch (e) { 39 assert(e === 42); 40} 41 42// test basic functionality 43var proxy = new Proxy({},{}); 44 45try { 46 new proxy(); 47 assert(false); 48} catch (e) { 49 assert(e instanceof TypeError); 50} 51 52var proxy2 = new Proxy(proxy, {}); 53 54try { 55 new proxy2(); 56 assert(false); 57} catch (e) { 58 assert(e instanceof TypeError); 59} 60 61var called = false; 62 63function Target() { 64 called = true; 65 this.property1 = 'value1'; 66}; 67 68Target.prototype = {}; 69var proxy = new Proxy(Target, {}); 70 71assert(called === false); 72 73var instance = new proxy(); 74 75assert(called === true); 76assert('value1' === instance.property1); 77assert(Target.prototype === Object.getPrototypeOf(instance)); 78 79var proxy2 = new Proxy(proxy, {}); 80called = false; 81var instance2 = new proxy2(); 82 83assert(called === true); 84assert('value1' === instance2.property1); 85assert(Target.prototype === Object.getPrototypeOf(instance)); 86 87function Target2(a, b) { 88 this.sum = a + b; 89}; 90var handler = { 91 construct(t, c, args) { 92 return { sum: 42 }; 93 } 94}; 95var proxy = new Proxy(Target2, handler); 96assert((new proxy(1, 2)).sum === 42); 97 98function Target3(arg1, arg2) { 99 this.arg1 = arg1; 100 this.arg2 = arg2; 101} 102var seen_target, seen_arguments, seen_new_target; 103var handler = { 104 construct(target, args, new_target) { 105 seen_target = target; 106 seen_arguments = args; 107 seen_new_target = new_target; 108 return Reflect.construct(target, args, new_target); 109 } 110} 111var proxy = new Proxy(Target3, handler); 112var instance = new proxy('a', 'b'); 113 114assert(Target3 === seen_target); 115assert(JSON.stringify(seen_arguments) === '["a","b"]'); 116assert(proxy === seen_new_target); 117assert('a' === instance.arg1); 118assert('b' === instance.arg2); 119 120var instance2 = Reflect.construct(proxy, ['a1', 'b1'], Array); 121assert(Target3 === seen_target); 122assert(JSON.stringify(seen_arguments) === '["a1","b1"]'); 123assert(Array === seen_new_target); 124assert('a1'=== instance2.arg1); 125assert('b1' === instance2.arg2); 126 127var p = new Proxy(function() {}, { 128 construct: function(target, argumentsList, newTarget) { 129 throw 42; 130 } 131}); 132 133try { 134 new p(); 135 assert(false); 136} catch (e) { 137 assert(e === 42); 138} 139 140// test when invariants gets violated 141var p = new Proxy(function() {}, { 142 construct: function(target, argumentsList, newTarget) { 143 return 1; 144 } 145}); 146 147try { 148 new p(); 149 assert(false); 150} catch (e) { 151 assert(e instanceof TypeError); 152} 153 154var p = new Proxy({}, { 155 construct: function(target, argumentsList, newTarget) { 156 return {}; 157 } 158}); 159 160try { 161 new p(); 162 assert(false); 163} catch (e) { 164 assert(e instanceof TypeError); 165} 166