• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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
16declare function print(str:string):string;
17
18function defineFunc(): Object {
19  let cnt: number = 10;
20  let result: Object = {};
21  let cb = () => {
22    // the profileTypeInfo of lambda is not bind on the function defineFunc.
23    // This leads to frequent "ic miss" when execute "result.year = 1"
24    result.year = 1;
25  };
26  for (let i: number = 0; i < cnt; ++i) {
27    cb();
28  }
29  return cb;
30}
31
32function defineClass(): Object {
33    class ReplaceClass{
34        constructor(value) {
35            // the profileTypeInfo of constructor is not bind on the function defineClass.
36            // This leads to frequent "ic miss" when execute "this.value=value"
37            this.value=value;
38        }
39    }
40    new ReplaceClass();
41    return ReplaceClass;
42}
43
44function defineMethod(): Object {
45    class MyClass {
46      static [Symbol.hasInstance](instance): boolean {
47           // the profileTypeInfo of constructor is not bind on the function defineMethod.
48           // This leads to frequent "ic miss" when execute "Array.isArray"
49        return Array.isArray(instance);
50      }
51    }
52
53    [] instanceof MyClass;
54    return MyClass[Symbol.hasInstance];
55}
56
57function run(defined: () => Object): Object[] {
58  let cnt: number = 100;
59  let cbArr: Object[] = [];
60  for (let i: number = 0; i < cnt; ++i) {
61    cbArr.push(defined());
62  }
63  return cbArr;
64}
65
66function assert_true(condition: boolean):void
67{
68    if (!condition) {
69        print("assert fail");
70    }
71}
72
73function CheckProfileTypeInfoNotChange(cbArr: Object[]): void {
74    let len: number = cbArr.length;
75    assert_true(len > 1);
76    let base = cbArr[0];
77    for (let i: number = 0; i < len; ++i) {
78        let checkTarget = cbArr[i];
79        assert_true(ArkTools.isProfileTypeInfoValid(checkTarget));
80        assert_true(ArkTools.isSameProfileTypeInfo(base, checkTarget));
81    }
82}
83
84CheckProfileTypeInfoNotChange(run(defineFunc));
85assert_true(ArkTools.isAOTCompiled(defineFunc));
86CheckProfileTypeInfoNotChange(run(defineMethod));
87assert_true(ArkTools.isAOTCompiled(defineMethod));
88CheckProfileTypeInfoNotChange(run(defineClass));
89assert_true(ArkTools.isAOTCompiled(defineClass));
90
91print("success");
92