1 /**
2 * Copyright (c) 2021-2022 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 panda::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 *LAMBDA_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
37 enum class EtsType { BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, OBJECT, UNKNOWN, VOID };
38
ConvertPandaTypeToEtsType(panda_file::Type type)39 inline EtsType ConvertPandaTypeToEtsType(panda_file::Type type)
40 {
41 switch (type.GetId()) {
42 case panda_file::Type::TypeId::INVALID:
43 return EtsType::UNKNOWN;
44 case panda_file::Type::TypeId::VOID:
45 return EtsType::VOID;
46 case panda_file::Type::TypeId::U1:
47 return EtsType::BOOLEAN;
48 case panda_file::Type::TypeId::I8:
49 return EtsType::BYTE;
50 case panda_file::Type::TypeId::U16:
51 return EtsType::CHAR;
52 case panda_file::Type::TypeId::I16:
53 return EtsType::SHORT;
54 case panda_file::Type::TypeId::I32:
55 return EtsType::INT;
56 case panda_file::Type::TypeId::I64:
57 return EtsType::LONG;
58 case panda_file::Type::TypeId::F32:
59 return EtsType::FLOAT;
60 case panda_file::Type::TypeId::F64:
61 return EtsType::DOUBLE;
62 case panda_file::Type::TypeId::REFERENCE:
63 return EtsType::OBJECT;
64 case panda_file::Type::TypeId::TAGGED:
65 return EtsType::UNKNOWN;
66 default:
67 return EtsType::UNKNOWN;
68 }
69 UNREACHABLE();
70 }
71
ConvertEtsTypeToPandaType(const EtsType type)72 inline panda_file::Type ConvertEtsTypeToPandaType(const EtsType type)
73 {
74 switch (type) {
75 case EtsType::VOID:
76 return panda_file::Type(panda_file::Type::TypeId::VOID);
77 case EtsType::BOOLEAN:
78 return panda_file::Type(panda_file::Type::TypeId::U1);
79 case EtsType::BYTE:
80 return panda_file::Type(panda_file::Type::TypeId::I8);
81 case EtsType::CHAR:
82 return panda_file::Type(panda_file::Type::TypeId::U16);
83 case EtsType::SHORT:
84 return panda_file::Type(panda_file::Type::TypeId::I16);
85 case EtsType::INT:
86 return panda_file::Type(panda_file::Type::TypeId::I32);
87 case EtsType::LONG:
88 return panda_file::Type(panda_file::Type::TypeId::I64);
89 case EtsType::FLOAT:
90 return panda_file::Type(panda_file::Type::TypeId::F32);
91 case EtsType::DOUBLE:
92 return panda_file::Type(panda_file::Type::TypeId::F64);
93 case EtsType::OBJECT:
94 return panda_file::Type(panda_file::Type::TypeId::REFERENCE);
95 default:
96 return panda_file::Type(panda_file::Type::TypeId::INVALID);
97 }
98 }
99
ConvertEtsPrimitiveTypeToString(const EtsType type)100 inline std::string ConvertEtsPrimitiveTypeToString(const EtsType type)
101 {
102 switch (type) {
103 case EtsType::VOID:
104 return "void";
105 case EtsType::BOOLEAN:
106 return "ets_boolean";
107 case EtsType::BYTE:
108 return "ets_byte";
109 case EtsType::CHAR:
110 return "ets_char";
111 case EtsType::SHORT:
112 return "ets_short";
113 case EtsType::INT:
114 return "ets_int";
115 case EtsType::LONG:
116 return "ets_long";
117 case EtsType::FLOAT:
118 return "ets_float";
119 case EtsType::DOUBLE:
120 return "ets_double";
121 default:
122 UNREACHABLE();
123 }
124 }
125
126 } // namespace panda::ets
127
128 #endif // PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_TYPE_H
129