• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        Object.defineProperty(obj.__proto__.__proto__, 'x', {
33            value: 2,
34            writable: true,
35            enumerable: true,
36            configurable: true
37        });
38    }
39    ArkTools.forceFullGC();
40    let arr = new Array(100000);
41    let sum = 0;
42    for (let i = 0; i < 100000; i++) {
43        arr[i] = 233;
44    }
45    for (let i = 0; i < 100000; i++) {
46        sum += arr[i];
47    }
48    print("sum:", sum);
49    print("ChangeProto end.");
50}
51
52
53// Test function that calls ChangePrototypeValue and prints the value of obj.x.
54function Test2(obj, shouldChange) {
55    print("Test2 start.");
56    ChangePrototypeValue(obj, shouldChange);
57    print("Test2 obj.x :", obj.x);
58    print("Test2 end.");
59}
60
61class A {}
62class B extends A {}
63class C extends B {}
64
65// Set the initial value of property x through A.prototype.
66A.prototype.x = 1;
67
68const c = new C();
69
70// Initial call to test without changing the prototype's property.
71Test2(c, false);
72
73ArkTools.jitCompileAsync(Test2);
74print(ArkTools.waitJitCompileFinish(Test2));
75
76print("------------------------------------------------------");
77// Call test with the flag set to true to modify the prototype property, triggering lazy deoptimization.
78Test2(c, true);