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