1 /**
2 * Copyright (c) 2021-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 #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 "libpandafile/file.h"
20 #include "libpandafile/file_items.h"
21
22 namespace ark::ets {
23
24 // Must be synchronized with
25 // plugins/ecmascript/es2panda/compiler/scripts/signatures.yaml
26 static constexpr char ARRAY_TYPE_PREFIX = '[';
27 static constexpr char CLASS_TYPE_PREFIX = 'L';
28 static constexpr char METHOD_PREFIX = 'M';
29 static constexpr const char *NULL_TYPE_DESC = "Null";
30 static constexpr const char *LAMBDA_PREFIX = "LambdaObject";
31 static constexpr const char *STD_CORE_FUNCTION_PREFIX = "std.core.Function";
32 static constexpr const char *LAMBDA_METHOD_NAME = "invoke";
33 static constexpr const char *CONSTRUCTOR_NAME = "constructor";
34 static constexpr char TYPE_DESC_DELIMITER = ';';
35 static constexpr const char *GETTER_BEGIN = "<get>";
36 static constexpr const char *SETTER_BEGIN = "<set>";
37
38 enum class EtsType { BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, OBJECT, UNKNOWN, VOID };
39
ConvertPandaTypeToEtsType(panda_file::Type type)40 inline EtsType ConvertPandaTypeToEtsType(panda_file::Type type)
41 {
42 switch (type.GetId()) {
43 case panda_file::Type::TypeId::INVALID:
44 return EtsType::UNKNOWN;
45 case panda_file::Type::TypeId::VOID:
46 return EtsType::VOID;
47 case panda_file::Type::TypeId::U1:
48 return EtsType::BOOLEAN;
49 case panda_file::Type::TypeId::I8:
50 return EtsType::BYTE;
51 case panda_file::Type::TypeId::U16:
52 return EtsType::CHAR;
53 case panda_file::Type::TypeId::I16:
54 return EtsType::SHORT;
55 case panda_file::Type::TypeId::I32:
56 return EtsType::INT;
57 case panda_file::Type::TypeId::I64:
58 return EtsType::LONG;
59 case panda_file::Type::TypeId::F32:
60 return EtsType::FLOAT;
61 case panda_file::Type::TypeId::F64:
62 return EtsType::DOUBLE;
63 case panda_file::Type::TypeId::REFERENCE:
64 return EtsType::OBJECT;
65 case panda_file::Type::TypeId::TAGGED:
66 return EtsType::UNKNOWN;
67 default:
68 return EtsType::UNKNOWN;
69 }
70 UNREACHABLE();
71 }
72
ConvertEtsTypeToPandaType(const EtsType type)73 inline panda_file::Type ConvertEtsTypeToPandaType(const EtsType type)
74 {
75 switch (type) {
76 case EtsType::VOID:
77 return panda_file::Type(panda_file::Type::TypeId::VOID);
78 case EtsType::BOOLEAN:
79 return panda_file::Type(panda_file::Type::TypeId::U1);
80 case EtsType::BYTE:
81 return panda_file::Type(panda_file::Type::TypeId::I8);
82 case EtsType::CHAR:
83 return panda_file::Type(panda_file::Type::TypeId::U16);
84 case EtsType::SHORT:
85 return panda_file::Type(panda_file::Type::TypeId::I16);
86 case EtsType::INT:
87 return panda_file::Type(panda_file::Type::TypeId::I32);
88 case EtsType::LONG:
89 return panda_file::Type(panda_file::Type::TypeId::I64);
90 case EtsType::FLOAT:
91 return panda_file::Type(panda_file::Type::TypeId::F32);
92 case EtsType::DOUBLE:
93 return panda_file::Type(panda_file::Type::TypeId::F64);
94 case EtsType::OBJECT:
95 return panda_file::Type(panda_file::Type::TypeId::REFERENCE);
96 default:
97 return panda_file::Type(panda_file::Type::TypeId::INVALID);
98 }
99 }
100
ConvertEtsPrimitiveTypeToString(const EtsType type)101 inline std::string ConvertEtsPrimitiveTypeToString(const EtsType type)
102 {
103 switch (type) {
104 case EtsType::VOID:
105 return "void";
106 case EtsType::BOOLEAN:
107 return "ets_boolean";
108 case EtsType::BYTE:
109 return "ets_byte";
110 case EtsType::CHAR:
111 return "ets_char";
112 case EtsType::SHORT:
113 return "ets_short";
114 case EtsType::INT:
115 return "ets_int";
116 case EtsType::LONG:
117 return "ets_long";
118 case EtsType::FLOAT:
119 return "ets_float";
120 case EtsType::DOUBLE:
121 return "ets_double";
122 default:
123 UNREACHABLE();
124 }
125 }
126
127 } // namespace ark::ets
128
129 #endif // PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_TYPE_H_
130