• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_TYPE_H_
16 #define PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_TYPE_H_
17 
18 #include "plugins/ets/runtime/napi/ets_napi.h"
19 #include "plugins/ets/runtime/types/ets_primitives.h"
20 #include "libpandafile/file.h"
21 #include "libpandafile/file_items.h"
22 
23 namespace ark::ets {
24 
25 // Must be synchronized with
26 // plugins/ecmascript/es2panda/compiler/scripts/signatures.yaml
27 static constexpr char ARRAY_TYPE_PREFIX = '[';
28 static constexpr char CLASS_TYPE_PREFIX = 'L';
29 static constexpr char METHOD_PREFIX = 'M';
30 static constexpr const char *TYPE_API_UNDEFINED_TYPE_DESC = "__TYPE_API_UNDEFINED";
31 static constexpr const char *INVOKE_METHOD_NAME = "$_invoke";
32 static constexpr const char *CONSTRUCTOR_NAME = "constructor";
33 static constexpr char TYPE_DESC_DELIMITER = ';';
34 static constexpr const char *GETTER_BEGIN = "<get>";
35 static constexpr const char *SETTER_BEGIN = "<set>";
36 static constexpr const char *PROPERTY = "<property>";
37 static constexpr const char *ITERATOR_METHOD = "$_iterator";
38 static constexpr const char *GET_INDEX_METHOD = "$_get";
39 static constexpr const char *SET_INDEX_METHOD = "$_set";
40 static constexpr const uint8_t SETTER_GETTER_PREFIX_LENGTH = 5;
41 static constexpr const uint8_t PROPERTY_PREFIX_LENGTH = 10;
42 
43 static constexpr const char *STD_CORE_FUNCTION_PREFIX = "std.core.Function";
44 static constexpr const char *STD_CORE_FUNCTION_UNSAFECALL_METHOD = "unsafeCall";
45 static constexpr const char *STD_CORE_FUNCTION_INVOKE_PREFIX = "invoke";
46 static constexpr const size_t STD_CORE_FUNCTION_MAX_ARITY = 15;
47 
48 enum class EtsType { BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, OBJECT, UNKNOWN, VOID };
49 
50 // CC-OFFNXT(G.FUD.06) switch-case, ODR
ConvertPandaTypeToEtsType(panda_file::Type type)51 inline EtsType ConvertPandaTypeToEtsType(panda_file::Type type)
52 {
53     switch (type.GetId()) {
54         case panda_file::Type::TypeId::INVALID:
55             return EtsType::UNKNOWN;
56         case panda_file::Type::TypeId::VOID:
57             return EtsType::VOID;
58         case panda_file::Type::TypeId::U1:
59             return EtsType::BOOLEAN;
60         case panda_file::Type::TypeId::I8:
61             return EtsType::BYTE;
62         case panda_file::Type::TypeId::U16:
63             return EtsType::CHAR;
64         case panda_file::Type::TypeId::I16:
65             return EtsType::SHORT;
66         case panda_file::Type::TypeId::I32:
67             return EtsType::INT;
68         case panda_file::Type::TypeId::I64:
69             return EtsType::LONG;
70         case panda_file::Type::TypeId::F32:
71             return EtsType::FLOAT;
72         case panda_file::Type::TypeId::F64:
73             return EtsType::DOUBLE;
74         case panda_file::Type::TypeId::REFERENCE:
75             return EtsType::OBJECT;
76         case panda_file::Type::TypeId::TAGGED:
77             return EtsType::UNKNOWN;
78         default:
79             return EtsType::UNKNOWN;
80     }
81     UNREACHABLE();
82 }
83 
84 // CC-OFFNXT(G.FUD.06) switch-case, ODR
ConvertEtsTypeToPandaType(const EtsType type)85 inline panda_file::Type ConvertEtsTypeToPandaType(const EtsType type)
86 {
87     switch (type) {
88         case EtsType::VOID:
89             return panda_file::Type(panda_file::Type::TypeId::VOID);
90         case EtsType::BOOLEAN:
91             return panda_file::Type(panda_file::Type::TypeId::U1);
92         case EtsType::BYTE:
93             return panda_file::Type(panda_file::Type::TypeId::I8);
94         case EtsType::CHAR:
95             return panda_file::Type(panda_file::Type::TypeId::U16);
96         case EtsType::SHORT:
97             return panda_file::Type(panda_file::Type::TypeId::I16);
98         case EtsType::INT:
99             return panda_file::Type(panda_file::Type::TypeId::I32);
100         case EtsType::LONG:
101             return panda_file::Type(panda_file::Type::TypeId::I64);
102         case EtsType::FLOAT:
103             return panda_file::Type(panda_file::Type::TypeId::F32);
104         case EtsType::DOUBLE:
105             return panda_file::Type(panda_file::Type::TypeId::F64);
106         case EtsType::OBJECT:
107             return panda_file::Type(panda_file::Type::TypeId::REFERENCE);
108         default:
109             return panda_file::Type(panda_file::Type::TypeId::INVALID);
110     }
111 }
112 
113 // CC-OFFNXT(G.FUD.06) switch-case, ODR
ConvertEtsPrimitiveTypeToString(const EtsType type)114 inline std::string ConvertEtsPrimitiveTypeToString(const EtsType type)
115 {
116     switch (type) {
117         case EtsType::VOID:
118             return "void";
119         case EtsType::BOOLEAN:
120             return "ets_boolean";
121         case EtsType::BYTE:
122             return "ets_byte";
123         case EtsType::CHAR:
124             return "ets_char";
125         case EtsType::SHORT:
126             return "ets_short";
127         case EtsType::INT:
128             return "ets_int";
129         case EtsType::LONG:
130             return "ets_long";
131         case EtsType::FLOAT:
132             return "ets_float";
133         case EtsType::DOUBLE:
134             return "ets_double";
135         default:
136             UNREACHABLE();
137     }
138 }
139 
140 template <typename T>
GetEtsTypeByPrimitive()141 constexpr EtsType GetEtsTypeByPrimitive()
142 {
143     if constexpr (std::is_same_v<T, EtsBoolean>) {
144         return EtsType::BOOLEAN;
145     } else if constexpr (std::is_same_v<T, EtsChar>) {
146         return EtsType::CHAR;
147     } else if constexpr (std::is_same_v<T, EtsByte>) {
148         return EtsType::BYTE;
149     } else if constexpr (std::is_same_v<T, EtsShort>) {
150         return EtsType::SHORT;
151     } else if constexpr (std::is_same_v<T, EtsInt>) {
152         return EtsType::INT;
153     } else if constexpr (std::is_same_v<T, EtsLong>) {
154         return EtsType::LONG;
155     } else if constexpr (std::is_same_v<T, EtsFloat>) {
156         return EtsType::FLOAT;
157     } else if constexpr (std::is_same_v<T, EtsDouble>) {
158         return EtsType::DOUBLE;
159     } else {
160         static_assert(true, "Unsupported Ets primitive");
161     }
162 }
163 
164 }  // namespace ark::ets
165 
166 #endif  // PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_TYPE_H_
167