1/* 2 * Copyright (c) 2022 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 16declare function assert_unreachable():void; 17declare function assert_true(condition: boolean):void; 18declare function assert_equal(a: Object, b: Object):void; 19declare function print(arg:any):string; 20 21{ 22 class A { 23 x?: number; 24 x2?: number; 25 constructor(x: number) { 26 this.x = x; 27 } 28 29 foo() { 30 return("xxxx"); 31 } 32 } 33 34 class B extends A { 35 y?: number; 36 constructor(x: number, y: number) { 37 super(x); 38 this.y = y; 39 } 40 } 41 42 // instance define Property 43 let b1 = new B(1, 2); 44 assert_true(!b1.hasOwnProperty("x1")); 45 Object.defineProperty(b1, "x1", {value:1}); 46 assert_true(b1.hasOwnProperty("x1")); 47 48 // instance delete and change Property 49 let b2 = new B(1, 2); 50 assert_true(b2.hasOwnProperty("y")); 51 assert_equal(b2.y, 2); 52 b2.y = 3; 53 assert_equal(b2.y, 3); 54 delete b2.y; 55 assert_true(!b2.hasOwnProperty("y")); 56 57 // prototype define Property 58 let p = A.prototype; 59 let b3 = new B(1, 2); 60 assert_equal(b3.x2, undefined); 61 assert_true(!Reflect.has(b3, "x2")); 62 Object.defineProperty(p, "x2", {value:1}); 63 assert_equal(b3.x2, 1); 64 assert_true(Reflect.has(b3, "x2")); 65 66 // prototype delete and change Property 67 let p2 = A.prototype; 68 let b4 = new B(1, 2); 69 assert_equal(b4.x, 1); 70 b4.x = 3; 71 assert_equal(b4.x, 3); 72 assert_true(b4.hasOwnProperty("x")); 73 delete p2.x; 74 assert_true(b4.hasOwnProperty("x")); 75 76 // prototype change and call function 77 let b5 = new B(1, 2); 78 assert_equal(b5.foo(), "xxxx"); 79 Object.setPrototypeOf(b5, {}) 80 try { 81 b5.foo(); 82 assert_unreachable(); 83 } catch(e) { 84 assert_equal(e.message, "CallObj is NonCallable"); 85 } 86} 87