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 = {}; 20var handler = { preventExtensions (target) { 21 throw 42; 22}}; 23 24var proxy = new Proxy(target, handler); 25 26try { 27 // 7.3.14 28 Object.freeze(proxy) 29 assert(false); 30} catch (e) { 31 assert(e === 42); 32} 33 34try { 35 // 7.3.14 36 Object.seal(proxy) 37 assert(false); 38} catch (e) { 39 assert(e === 42); 40} 41 42// test with no trap 43var target = {}; 44var handler = {}; 45var proxy = new Proxy(target, handler); 46 47assert(Object.isExtensible(target) === true); 48assert(Object.isExtensible(proxy) === true); 49Object.preventExtensions(proxy); 50assert(Object.isExtensible(target) === false); 51assert(Object.isExtensible(proxy) === false); 52 53// test with "undefined" trap 54var target = {}; 55var handler = { preventExtensions: null }; 56var proxy = new Proxy(target, handler); 57 58assert(Object.isExtensible(target) === true); 59assert(Object.isExtensible(proxy) === true); 60Object.preventExtensions(proxy); 61assert(Object.isExtensible(target) === false); 62assert(Object.isExtensible(proxy) === false); 63 64// test with invalid trap 65var target = {}; 66var handler = { preventExtensions: 42 }; 67var proxy = new Proxy(target, handler); 68 69try { 70 Object.preventExtensions(proxy); 71 assert(false); 72} catch (e) { 73 assert(e instanceof TypeError); 74} 75 76// test with valid trap 77var target = { foo: "bar" }; 78var handler = { 79 preventExtensions(target) { 80 target.foo = "foo" 81 Object.preventExtensions(target); 82 return true; 83 } 84} 85 86var proxy = new Proxy(target, handler); 87assert(Object.isExtensible(target) === true); 88assert(Object.isExtensible(proxy) === true); 89assert(target.foo === "bar"); 90Object.preventExtensions(proxy); 91assert(Object.isExtensible(target) === false); 92assert(Object.isExtensible(proxy) === false); 93assert(target.foo === "foo"); 94 95// test when invariants gets violated 96var target = {}; 97var handler = { 98 preventExtensions(target) { 99 return true; 100 } 101} 102 103var proxy = new Proxy(target, handler); 104 105try { 106 Object.preventExtensions(proxy); 107 assert(false); 108} catch (e) { 109 assert(e instanceof TypeError); 110} 111 112// test when target is proxy 113var target = {}; 114var handler = { 115 preventExtensions(target) { 116 Object.preventExtensions(target); 117 return true; 118 } 119} 120 121var proxy1 = new Proxy(target, handler); 122var proxy2 = new Proxy(proxy1, handler); 123 124assert(Object.isExtensible(target) === true); 125assert(Object.isExtensible(proxy1) === true); 126assert(Object.isExtensible(proxy2) === true); 127Object.preventExtensions(proxy2); 128assert(Object.isExtensible(target) === false); 129assert(Object.isExtensible(proxy1) === false); 130assert(Object.isExtensible(proxy2) === false); 131 132var target = {}; 133var handler = { 134 preventExtensions(target) { 135 return true; 136 } 137} 138 139var proxy1 = new Proxy(target, handler); 140var proxy2 = new Proxy(proxy1, handler); 141 142try { 143 Object.preventExtensions(proxy2); 144 assert(false); 145} catch (e) { 146 assert(e instanceof TypeError); 147} 148