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. Test GC effect. 24 */ 25 26// Modify the value of property 'x' on a specific level in the prototype chain of the object. 27function ChangePrototypeValue(obj, shouldChange) { 28 print("ChangeProto start."); 29 if (shouldChange) { 30 // Change the property 'x' at the second level of the prototype chain, 31 // triggering lazy deoptimization of the JIT-compiled 'Test2' function. 32 obj.__proto__.__proto__.x = 2; 33 } 34 ArkTools.forceFullGC(); 35 let arr = new Array(100000); 36 let sum = 0; 37 for (let i = 0; i < 100000; i++) { 38 arr[i] = 233; 39 } 40 for (let i = 0; i < 100000; i++) { 41 sum += arr[i]; 42 } 43 print("sum:", sum); 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; 62 63const c = new C(); 64 65// Initial call to test without changing the prototype's property. 66Test2(c, false); 67 68ArkTools.jitCompileAsync(Test2); 69print(ArkTools.waitJitCompileFinish(Test2)); 70 71print("------------------------------------------------------"); 72// Call test with the flag set to true to modify the prototype property, triggering lazy deoptimization. 73Test2(c, true);