• 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 { ClassSignature, FileSignature, MethodSignature, MethodSubSignature } from '../model/ArkSignature';
17import { ClassType, GenericType, StringType } from '../base/Type';
18
19export class Builtin {
20    // built-in classes
21    // TODO: Automatically obtain from the standard library
22    public static OBJECT = 'Object';
23    public static ARRAY = 'Array';
24    public static SET = 'Set';
25    public static MAP = 'Map';
26    public static REGEXP = 'RegExp';
27
28    public static BUILT_IN_CLASSES = this.buildBuiltInClasses();
29
30    // signature for built-in class
31    public static DUMMY_PROJECT_NAME = 'ES2015';
32    public static DUMMY_FILE_NAME = 'BuiltinClass';
33
34    public static BUILT_IN_CLASSES_FILE_SIGNATURE = Builtin.buildBuiltInClassesFileSignature();
35    public static OBJECT_CLASS_SIGNATURE = this.buildBuiltInClassSignature(this.OBJECT);
36    public static OBJECT_CLASS_TYPE = new ClassType(this.OBJECT_CLASS_SIGNATURE);
37    public static ARRAY_CLASS_SIGNATURE = this.buildBuiltInClassSignature(this.ARRAY);
38    public static SET_CLASS_SIGNATURE = this.buildBuiltInClassSignature(this.SET);
39    public static MAP_CLASS_SIGNATURE = this.buildBuiltInClassSignature(this.MAP);
40    public static REGEXP_CLASS_SIGNATURE = this.buildBuiltInClassSignature(this.REGEXP);
41    public static REGEXP_CLASS_TYPE = new ClassType(this.REGEXP_CLASS_SIGNATURE);
42    public static BUILT_IN_CLASS_SIGNATURE_MAP = this.buildBuiltInClassSignatureMap();
43
44    // constants for iterator
45    public static ITERATOR_FUNCTION = 'Symbol.iterator';
46    public static ITERATOR = 'IterableIterator';
47    public static ITERATOR_NEXT = 'next';
48    public static ITERATOR_RESULT = 'IteratorResult';
49    public static ITERATOR_RESULT_DONE = 'done';
50    public static ITERATOR_RESULT_VALUE = 'value';
51
52    public static ITERATOR_CLASS_SIGNATURE = this.buildBuiltInClassSignature(this.ITERATOR);
53    public static ITERATOR_RESULT_CLASS_SIGNATURE = this.buildBuiltInClassSignature(this.ITERATOR_RESULT);
54    public static ITERATOR_CLASS_TYPE = new ClassType(this.ITERATOR_CLASS_SIGNATURE, [new GenericType('T')]);
55    public static ITERATOR_RESULT_CLASS_TYPE = new ClassType(this.ITERATOR_RESULT_CLASS_SIGNATURE, [new GenericType('T')]);
56
57    // constants for string
58    public static TO_STRING = 'toString';
59    public static TO_STRING_METHOD_SIGNATURE = new MethodSignature(
60        ClassSignature.DEFAULT,
61        new MethodSubSignature(this.TO_STRING, [], StringType.getInstance(), false)
62    );
63
64    private static buildBuiltInClasses(): Set<string> {
65        const builtInClasses = new Set<string>();
66        builtInClasses.add(this.OBJECT);
67        builtInClasses.add(this.ARRAY);
68        builtInClasses.add(this.SET);
69        builtInClasses.add(this.MAP);
70        builtInClasses.add(this.REGEXP);
71        return builtInClasses;
72    }
73
74    private static buildBuiltInClassesFileSignature(): FileSignature {
75        return new FileSignature(this.DUMMY_PROJECT_NAME, this.DUMMY_FILE_NAME);
76    }
77
78    public static buildBuiltInClassSignature(className: string): ClassSignature {
79        return new ClassSignature(className, this.BUILT_IN_CLASSES_FILE_SIGNATURE);
80    }
81
82    private static buildBuiltInClassSignatureMap(): Map<string, ClassSignature> {
83        const builtInClassSignatureMap = new Map<string, ClassSignature>();
84        builtInClassSignatureMap.set(this.OBJECT, this.OBJECT_CLASS_SIGNATURE);
85        builtInClassSignatureMap.set(this.ARRAY, this.ARRAY_CLASS_SIGNATURE);
86        builtInClassSignatureMap.set(this.SET, this.SET_CLASS_SIGNATURE);
87        builtInClassSignatureMap.set(this.MAP, this.MAP_CLASS_SIGNATURE);
88        builtInClassSignatureMap.set(this.REGEXP, this.REGEXP_CLASS_SIGNATURE);
89        return builtInClassSignatureMap;
90    }
91
92    public static isBuiltinClass(className: string): boolean {
93        return this.BUILT_IN_CLASSES.has(className);
94    }
95}
96