1 /*
2 * Copyright (c) 2023 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 #ifndef ECMASCRIPT_TS_TYPES_BUILTIN_TYPE_ID_H
17 #define ECMASCRIPT_TS_TYPES_BUILTIN_TYPE_ID_H
18
19 #include <cstdint>
20 #include <string_view>
21
22 #define TS_BUILTIN_TYPE_LIST(V) \
23 V(FUNCTION) \
24 V(RANGE_ERROR) \
25 V(ERROR) \
26 V(OBJECT) \
27 V(SYNTAX_ERROR) \
28 V(TYPE_ERROR) \
29 V(REFERENCE_ERROR) \
30 V(URI_ERROR) \
31 V(SYMBOL) \
32 V(EVAL_ERROR) \
33 V(NUMBER) \
34 V(PARSE_FLOAT) \
35 V(DATE) \
36 V(BOOLEAN) \
37 V(BIG_INT) \
38 V(PARSE_INT) \
39 V(WEAK_MAP) \
40 V(REG_EXP) \
41 V(SET) \
42 V(MAP) \
43 V(WEAK_REF) \
44 V(WEAK_SET) \
45 V(FINALIZATION_REGISTRY) \
46 V(ARRAY) \
47 V(UINT8_CLAMPED_ARRAY) \
48 V(UINT8_ARRAY) \
49 V(TYPED_ARRAY) \
50 V(INT8_ARRAY) \
51 V(UINT16_ARRAY) \
52 V(UINT32_ARRAY) \
53 V(INT16_ARRAY) \
54 V(INT32_ARRAY) \
55 V(FLOAT32_ARRAY) \
56 V(FLOAT64_ARRAY) \
57 V(BIGINT64_ARRAY) \
58 V(BIGUINT64_ARRAY) \
59 V(SHARED_ARRAY_BUFFER) \
60 V(DATA_VIEW) \
61 V(STRING) \
62 V(ARRAY_BUFFER) \
63 V(EVAL) \
64 V(IS_FINITE) \
65 V(ARK_PRIVATE) \
66 V(PRINT) \
67 V(DECODE_URI) \
68 V(DECODE_URI_COMPONENT) \
69 V(IS_NAN) \
70 V(ENCODE_URI) \
71 V(JS_NAN) \
72 V(GLOBAL_THIS) \
73 V(ENCODE_URI_COMPONENT) \
74 V(JS_INFINITY) \
75 V(MATH) \
76 V(JSON) \
77 V(ATOMICS) \
78 V(UNDEFINED) \
79 V(REFLECT) \
80 V(PROMISE) \
81 V(PROXY) \
82 V(GENERATOR_FUNCTION) \
83 V(ITERATOR) \
84 V(ARRAY_ITERATOR) \
85 V(INTL)
86
87 namespace panda::ecmascript {
88 #define TS_BUILTIN_TYPE_ENUM_ITEM(TYPE) TYPE,
89
90 /* Since AOT allows loading lib_ark_builtins.d.ts optionally, the localId of the GlobalTSTypeRef
91 * (abbreviated as GT) of one builtin object will be different in different cases.
92 *
93 * In case where AOT does not load lib_ark_builtins.d.ts, builtin objects will be assigned localIds
94 * according to the following enum order. For example, the GT of FUNCTION will be (1, 21), where 1
95 * is the index of builtin TSTypeTable. Note that in this case, it is prohibited to get TSType from
96 * builtin TSTypeTable.
97 *
98 * In case where AOT has loaded lib_ark_builtins.d.ts, builtin objects will be assigned localIds in
99 * the order in which they appear in bytecodes. To identify types of builtin objects, the following
100 * enum is required as a parameter to the TSManager::GetBuiltinOffset to help to get the GT of some
101 * builtin object.
102 */
103 enum class BuiltinTypeId : uint8_t { // keep the same with enum BuiltinType in ets_frontend
104 NUM_INDEX_IN_SUMMARY = 0,
105 BUILTIN_OFFSET = 20,
106 TS_BUILTIN_TYPE_LIST(TS_BUILTIN_TYPE_ENUM_ITEM)
107 NUM_OF_BUILTIN_TYPES = INTL - BUILTIN_OFFSET,
108 TYPED_ARRAY_FIRST = UINT8_CLAMPED_ARRAY,
109 TYPED_ARRAY_LAST = BIGUINT64_ARRAY,
110 };
111 #undef TS_BUILTIN_TYPE_ENUM_ITEM
112
ToString(BuiltinTypeId type)113 inline constexpr std::string_view ToString(BuiltinTypeId type)
114 {
115 #define TS_BUILTIN_TYPE_SWITCH_CASE(TYPE) \
116 case BuiltinTypeId::TYPE: \
117 return #TYPE;
118
119 switch (type) {
120 TS_BUILTIN_TYPE_LIST(TS_BUILTIN_TYPE_SWITCH_CASE)
121 default:
122 return "(Invalid BuiltinTypeId)";
123 }
124 }
125
IsTypedArrayType(BuiltinTypeId type)126 inline constexpr bool IsTypedArrayType(BuiltinTypeId type)
127 {
128 size_t index = static_cast<size_t>(type);
129 size_t first = static_cast<size_t>(BuiltinTypeId::TYPED_ARRAY_FIRST);
130 size_t last = static_cast<size_t>(BuiltinTypeId::TYPED_ARRAY_LAST);
131 return first <= index && index <= last;
132 }
133 } // namespace panda::ecmascript
134 #endif // ECMASCRIPT_TS_TYPES_BUILTIN_TYPE_ID_H
135