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