• 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/*
18 * Description:
19 * 1. This code tests the lazy deoptimization that occurs in ldobjbyname.
20 *    After the JIT code for function 'test' is compiled,
21 *    modifying an HClass invalidates the function,
22 *    and subsequent accesses to it will detect this invalidation.
23 * 2. Test2 call ChangePrototypeValue not inlined.
24 * 3. Test call Test2 not inlined.
25 */
26
27
28function ChangePrototypeValue(obj, shouldChange) {
29    print("ChangeProto start.");
30    if (shouldChange) {
31        // Update the 'x' property at the second level of the prototype chain,
32        // triggering lazy deoptimization of the JIT-compiled 'test' function.
33        obj.__proto__.__proto__.x = 2;
34    }
35    print("ChangeProto end.");
36}
37
38function Test2(obj, shouldChange) {
39    print("Test2 start.");
40    ChangePrototypeValue(obj, shouldChange);
41    ChangePrototypeValue(obj, shouldChange);
42    print("Test2 end.");
43
44    // Additional code to prevent aggressive inlining.
45    let test = {};
46    test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x;
47    test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x;
48    test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x;
49    test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x; test.x;
50}
51
52function Test(obj, shouldChange) {
53    print("Test start.");
54    // Execute inline notification testing.
55    Test2(obj, shouldChange);
56    // Output the current value of the property 'x'.
57    print("Test obj.x :", obj.x);
58    print("Test end.");
59}
60
61class A {}
62class B extends A {}
63class C extends B {}
64
65// Set the initial value of 'x' on A's prototype.
66A.prototype.x = 1;
67
68const c = new C();
69
70// First test call: without modifying the prototype. Expected output: property 'x' remains unchanged.
71Test(c, false);
72
73ArkTools.jitCompileAsync(Test);
74print(ArkTools.waitJitCompileFinish(Test));
75
76print("------------------------------------------------------");
77// Call test with the flag set to true to modify the prototype property, triggering lazy deoptimization.
78Test(c, true);