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/* 17 * Description: 18 * 1. This code tests the lazy deoptimization that occurs in ldobjbyname. 19 * After the JIT code for function 'Test2' is compiled, 20 * modifying an HClass invalidates the function, 21 * and subsequent accesses to it will detect this invalidation. 22 * 2. Test2 call ChangePrototypeValue which is not inlined. 23 */ 24 25// Modify the value of property 'x' on a specific level in the prototype chain of the object. 26function ChangePrototypeValue(obj, shouldChange) { 27 print("ChangeProto start."); 28 if (shouldChange) { 29 // Change the property 'x' at the second level of the prototype chain, 30 // triggering lazy deoptimization of the JIT-compiled 'Test2' function. 31 obj.__proto__.__proto__.x = 2; 32 } 33 print("ChangeProto end."); 34 35 // Additional code to prevent aggressive inlining. 36 let test = {}; 37 test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; 38 test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; 39 test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; 40 test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; 41} 42 43 44// Test function that calls ChangePrototypeValue and prints the value of obj.x. 45function Test2(obj, shouldChange) { 46 print("Test2 start."); 47 ChangePrototypeValue(obj, shouldChange); 48 print("Test2 obj.x :", obj.x); 49 print("Test2 end."); 50} 51 52class A {} 53class B extends A {} 54class C extends B {} 55 56// Set the initial value of property x through A.prototype. 57A.prototype.x = 1; 58 59const c = new C(); 60 61// Initial call to test without changing the prototype's property. 62Test2(c, false); 63 64ArkTools.jitCompileAsync(Test2); 65print(ArkTools.waitJitCompileFinish(Test2)); 66 67print("------------------------------------------------------"); 68// Call test with the flag set to true to modify the prototype property, triggering lazy deoptimization. 69Test2(c, true);