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