1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 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 16// test1: normal trap. 17function test1() 18{ 19 const target = { price: 99 }; 20 const handler = { 21 get(target, prop) { 22 return prop === 'price' ? `$${target[prop]}` : target[prop]; 23 } 24 }; 25 const proxy = new Proxy(target, handler); 26 27 assert_equal(proxy.price, "$99"); // "$99" 28} 29test1(); 30 31// test2: handle is not callable, throw TypeError. 32function test2() 33{ 34 try { 35 const invalidHandler = { get: 'not a function' }; 36 const proxy = new Proxy({}, invalidHandler); // throw TypeError 37 assert_equal(proxy.price, undefined); 38 } catch (error) { 39 assert_equal(error instanceof TypeError, true); // true 40 } 41} 42test2(); 43 44// test3: CheckGetTrapResult, check data. 45function test3() 46{ 47 const target = {}; 48 Object.defineProperty(target, 'key', { 49 value: 100, 50 writable: false, 51 configurable: false 52 }); 53 const proxy = new Proxy(target, { 54 get(target, prop) { 55 return 200; 56 } 57 }); 58 try { 59 proxy.key; 60 } catch (error) { 61 assert_equal(error instanceof TypeError, true); // true 62 } 63} 64test3(); 65 66// test4: CheckGetTrapResult, check accessor. 67function test4() 68{ 69 const target = {}; 70 Object.defineProperty(target, 'key', { 71 set() { 72 }, 73 configurable: false 74 }); 75 const proxy = new Proxy(target, { 76 get(target, prop) { 77 return 200; 78 } 79 }); 80 try { 81 proxy.key; 82 } catch (error) { 83 assert_equal(error instanceof TypeError, true); // true 84 } 85} 86test4(); 87 88// test5: CheckGetTrapResult, check accessor, configurable is true. 89function test5() 90{ 91 const target = {}; 92 Object.defineProperty(target, 'key', { 93 set() { 94 }, 95 configurable: true, 96 }); 97 const proxy = new Proxy(target, { 98 get(target, prop) { 99 return 200; 100 } 101 }); 102 try { 103 proxy.key; 104 } catch (error) { 105 assert_equal(error instanceof TypeError, false); // false 106 } 107} 108test5(); 109 110// test6: CheckGetTrapResult, check accessor, trap result is undefined. 111function test6() 112{ 113 const target = {}; 114 Object.defineProperty(target, 'key', { 115 set() { 116 }, 117 configurable: false, 118 }); 119 const proxy = new Proxy(target, {}); 120 try { 121 proxy.key; 122 } catch (error) { 123 assert_equal(error instanceof TypeError, false); // false 124 } 125} 126test6(); 127 128// test7: CheckGetTrapResult, check internal accessor. 129function test7() 130{ 131 const target = function f0(){}; 132 Object.defineProperty(target, 'length', { 133 value: 0, 134 configurable: false, 135 writable: false, 136 }); 137 const proxy = new Proxy(target, { 138 get(target, prop) { 139 return 200; 140 } 141 }); 142 try { 143 proxy.length; 144 } catch (error) { 145 assert_equal(error instanceof TypeError, true); // true 146 } 147} 148test7(); 149 150// test8: CheckGetTrapResult, check internal accessor, value is same. 151function test8() 152{ 153 const target = function f0(){}; 154 Object.defineProperty(target, 'length', { 155 value: 200, 156 configurable: false, 157 writable: false, 158 }); 159 const proxy = new Proxy(target, { 160 get(target, prop) { 161 return 200; 162 } 163 }); 164 try { 165 proxy.length; 166 } catch (error) { 167 assert_equal(error instanceof TypeError, false); // false 168 } 169} 170test8(); 171 172// test9: CheckGetTrapResult, check internal accessor, configurable is true. 173function test9() 174{ 175 const target = function f0(){}; 176 Object.defineProperty(target, 'length', { 177 value: 200, 178 }); 179 const proxy = new Proxy(target, { 180 get(target, prop) { 181 return 200; 182 } 183 }); 184 try { 185 proxy.length; 186 } catch (error) { 187 assert_equal(error instanceof TypeError, false); // false 188 } 189} 190test9(); 191 192function test10() 193{ 194 let target = { a: 1 }; 195 const handler = {}; 196 handler.__proto__ = new Proxy(target, handler); 197 try { 198 handler.__proto__ = new Proxy(target, handler); 199 } catch (error) { 200 assert_equal(error instanceof RangeError, true); 201 print(error); 202 } 203} 204test10(); 205 206test_end(); 207