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/* 18 * Description: 19 * 1. This code tests the lazy deoptimization that occurs in ldobjbyname. 20 * After the JIT code for function 'test' is compiled, 21 * modifying an HClass invalidates the function, 22 * and subsequent accesses to it will detect this invalidation. 23 * 2. Test2 call ChangePrototypeValue inlined. 24 * 3. Test call Test2 inlined. 25 */ 26 27function MakeChange(obj, shouldChange) { 28 if (shouldChange) { 29 // Update the 'x' property at the second level of the prototype chain, 30 // triggering lazy deoptimization of the JIT-compiled 'test' function. 31 obj.__proto__.__proto__.x = 2; 32 } 33 // Additional code to prevent aggressive inlining. 34 let test = {}; 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 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} 40 41 42function ChangePrototypeValue(obj, shouldChange) { 43 print("ChangeProto start."); 44 MakeChange(obj, shouldChange); 45 print("ChangeProto end."); 46} 47 48function Test2(obj, shouldChange) { 49 print("Test2 start."); 50 ChangePrototypeValue(obj, shouldChange); 51 ChangePrototypeValue(obj, shouldChange); 52 print("Test2 end."); 53} 54 55function Test(obj, shouldChange) { 56 print("Test start."); 57 // Execute inline notification testing. 58 Test2(obj, shouldChange); 59 // Output the current value of the property 'x'. 60 print("Test obj.x :", obj.x); 61 print("Test end."); 62} 63 64class A {} 65class B extends A {} 66class C extends B {} 67 68// Set the initial value of 'x' on A's prototype. 69A.prototype.x = 1; 70 71const c = new C(); 72 73// First test call: without modifying the prototype. Expected output: property 'x' remains unchanged. 74Test(c, false); 75 76ArkTools.jitCompileAsync(Test); 77print(ArkTools.waitJitCompileFinish(Test)); 78 79print("------------------------------------------------------"); 80// Call test with the flag set to true to modify the prototype property, triggering lazy deoptimization. 81Test(c, true);