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