• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-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
16import { BigIntConstant, BooleanConstant, Constant, NullConstant, NumberConstant, StringConstant, UndefinedConstant } from '../base/Constant';
17
18export const EMPTY_STRING = '';
19
20export class ValueUtil {
21    private static readonly NumberConstantCache: Map<number, Constant> = new Map();
22    public static readonly EMPTY_STRING_CONSTANT = new StringConstant(EMPTY_STRING);
23
24    /*
25     * Set static field to be null, then all related objects could be freed by GC.
26     * Class SdkUtils is only internally used by ArkAnalyzer, the dispose method should be called by users themselves before drop Scene.
27     */
28    public static dispose(): void {
29        this.NumberConstantCache.clear();
30    }
31
32    public static getOrCreateNumberConst(n: number): Constant {
33        let constant = this.NumberConstantCache.get(n);
34        if (constant === undefined) {
35            constant = new NumberConstant(n);
36            this.NumberConstantCache.set(n, constant);
37        }
38        return constant;
39    }
40
41    public static createBigIntConst(bigInt: bigint): BigIntConstant {
42        return new BigIntConstant(bigInt);
43    }
44
45    public static createStringConst(str: string): Constant {
46        if (str === EMPTY_STRING) {
47            return this.EMPTY_STRING_CONSTANT;
48        }
49        return new StringConstant(str);
50    }
51
52    public static createConst(str: string): Constant {
53        const n = Number(str);
54        if (!isNaN(n)) {
55            return this.getOrCreateNumberConst(n);
56        }
57        return new StringConstant(str);
58    }
59
60    public static getUndefinedConst(): Constant {
61        return UndefinedConstant.getInstance();
62    }
63
64    public static getNullConstant(): Constant {
65        return NullConstant.getInstance();
66    }
67
68    public static getBooleanConstant(value: boolean): Constant {
69        return BooleanConstant.getInstance(value);
70    }
71}
72