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 19// test basic funcionality 20function Monster() { 21 this.eyeCount = 4; 22} 23 24var handler = { 25 set(obj, prop, value) { 26 if (prop == 'eyeCount') { 27 obj[prop] = value; 28 } else { 29 obj[prop] = "foo"; 30 } 31 } 32}; 33 34var monster = new Monster(); 35var proxy = new Proxy(monster, handler); 36 37proxy.eyeCount = 1; 38proxy.foo = "bar"; 39 40assert(monster.eyeCount === 1); 41assert(monster.foo === "foo"); 42 43var target = { foo: "foo"}; 44var handler = { 45 set: function(obj, prop, value) { 46 obj[prop] = ""; 47 } 48}; 49var proxy = new Proxy(target, handler); 50 51proxy.foo = 12; 52assert(target.foo === ""); 53 54var properties = ["bla", "0", 1, Symbol(), {[Symbol.toPrimitive]() {return "a"}}]; 55 56var target = {}; 57var handler = {}; 58var proxy = new Proxy(target, handler); 59 60// test when property does not exist on target 61/* TODO: Enable these tests when Proxy.[[GetOwnProperty]] has been implemented 62for (var p of properties) { 63 proxy.p = 42; 64 assert(target.p === 42); 65} 66 67// test when property exists as writable data on target 68for (var p of properties) { 69 Object.defineProperty(target, p, { 70 writable: true, 71 value: 24 72 }); 73 74 proxy.p = 42; 75 assert(target.p === 42); 76} 77*/ 78// test when target is a proxy 79var target = {}; 80var handler = { 81 set(obj, prop, value) { 82 obj[prop] = value; 83 } 84}; 85 86var proxy = new Proxy(target, handler); 87var proxy2 = new Proxy(proxy, handler); 88 89proxy2.prop = "foo"; 90 91assert(target.prop === "foo"); 92 93// test when handler is null 94var target = {}; 95var handler = { 96 set(obj, prop, value) { 97 obj[prop] = value; 98 } 99}; 100 101var revocable = Proxy.revocable (target, {}); 102var proxy = revocable.proxy; 103revocable.revoke(); 104 105try { 106 proxy.prop = 42; 107 assert(false); 108} catch (e) { 109 assert(e instanceof TypeError); 110} 111 112// test when invariants gets violated 113var target = {}; 114var handler = { set() {return 42} }; 115var proxy = new Proxy(target, handler); 116 117Object.defineProperty(target, "key", { 118 configurable: false, 119 writable: false, 120 value: 0 121}); 122 123try { 124 proxy.key = 600; 125 assert(false); 126} catch (e) { 127 assert(e instanceof TypeError); 128} 129 130Object.defineProperty(target, "key2", { 131 configurable: false, 132 set: undefined 133}); 134 135try { 136 proxy.key2 = 500; 137 assert(false); 138} catch (e) { 139 assert(e instanceof TypeError); 140} 141