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 = { defineProperty (target) { 21 throw 42; 22}, construct () { 23 return {}; 24}}; 25 26var proxy = new Proxy(target, handler); 27 28// 22.1.2.3.8.c 29Array.of.call(proxy, 5) 30 31// test basic functionality 32var g_target, g_name; 33 34var handler = { 35 defineProperty: function(target, name, desc) { 36 g_target = target; 37 g_name = name; 38 return true; 39 } 40} 41 42var target = {}; 43var proxy = new Proxy(target, handler); 44var desc = { value: 1, writable: true, configurable: true }; 45 46Object.defineProperty(proxy, "foo", desc); 47 48assert(target === g_target); 49assert("foo" === g_name); 50 51var handler = { 52 defineProperty: function(target, name, desc) { 53 Object.defineProperty(target, name, desc); 54 } 55} 56 57var proxy = new Proxy(target, handler); 58 59Object.defineProperty(proxy, "bar", desc); 60 61assert(proxy.bar === 1); 62 63/* TODO - remove this comment when [[GetOwnProperty]] is implemented 64proxy.bar = 2; 65assert(proxy.bar === 2); 66*/ 67 68delete proxy.bar; 69assert(proxy.bar === undefined); 70 71/* TODO - remove this comment when [[GetOwnProperty]] is implemented 72Object.defineProperty(proxy, "name", { 73 get() { 74 return this._name; 75 }, 76 set(value) { 77 this._name = value; 78 } 79}); 80 81proxy.name = "foo"; 82 83assert(proxy.name === "foo"); 84assert(target.name === "foo"); 85*/ 86 87// test when trap is not callable 88var target = {}; 89var handler = { 90 defineProperty: 1 91} 92 93var proxy = new Proxy(target, handler); 94 95try { 96 Object.defineProperty(proxy, "foo", {value: "foo"}); 97 assert(false); 98} catch (e) { 99 assert(e instanceof TypeError); 100} 101 102// test when trap is undefined 103var target = {}; 104var handler = { 105 defineProperty: undefined 106} 107 108var proxy = new Proxy(target, handler); 109var desc = { value: 1 }; 110 111Object.defineProperty(proxy, "prop1", desc); 112assert(proxy.prop1 === 1); 113 114var target2 = {}; 115var proxy2 = new Proxy(target2, {}); 116 117Object.defineProperty(proxy2, "prop2", desc); 118assert(proxy2.prop2 === 1); 119 120// test when invariants gets violated 121var target = {}; 122var handler = { 123 defineProperty: function(target, name, desc) { 124 return true; 125 } 126} 127 128var proxy = new Proxy(target, handler); 129 130Object.preventExtensions(target); 131 132try { 133 Object.defineProperty(proxy, "foo", {value: 1}); 134 assert(false); 135} catch (e) { 136 assert(e instanceof TypeError); 137} 138 139var target = {}; 140var desc = {value: 1, writable: true, configurable: false, enumerable: true}; 141 142var proxy = new Proxy(target, handler); 143 144try { 145 Object.defineProperty(proxy, "foo", desc); 146 assert(false); 147} catch (e) { 148 assert(e instanceof TypeError); 149} 150 151var target = {}; 152var handler = { 153 defineProperty: function(target, name, desc) { 154 return true; 155 } 156} 157 158var proxy = new Proxy(target, handler); 159 160Object.defineProperty(target, "foo", {value: 1, writable: false, configurable: false}); 161 162try { 163 Object.defineProperty(proxy, 'foo', {value: 2}); 164 assert(false); 165} catch (e) { 166 assert(e instanceof TypeError); 167} 168 169target.bar = "baz"; 170 171try { 172 Object.defineProperty(proxy, 'bar', {value: 2, configurable: false}); 173 assert(false); 174} catch (e) { 175 assert(e instanceof TypeError); 176} 177