• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *FN_INVOKE_METHOD_NAME = "invoke0";
34 static constexpr const char *CONSTRUCTOR_NAME = "constructor";
35 static constexpr char TYPE_DESC_DELIMITER = ';';
36 static constexpr const char *GETTER_BEGIN = "<get>";
37 static constexpr const char *SETTER_BEGIN = "<set>";
38 
39 enum class EtsType { BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, OBJECT, UNKNOWN, VOID };
40 
41 // CC-OFFNXT(G.FUD.06) switch-case, ODR
ConvertPandaTypeToEtsType(panda_file::Type type)42 inline EtsType ConvertPandaTypeToEtsType(panda_file::Type type)
43 {
44     switch (type.GetId()) {
45         case panda_file::Type::TypeId::INVALID:
46             return EtsType::UNKNOWN;
47         case panda_file::Type::TypeId::VOID:
48             return EtsType::VOID;
49         case panda_file::Type::TypeId::U1:
50             return EtsType::BOOLEAN;
51         case panda_file::Type::TypeId::I8:
52             return EtsType::BYTE;
53         case panda_file::Type::TypeId::U16:
54             return EtsType::CHAR;
55         case panda_file::Type::TypeId::I16:
56             return EtsType::SHORT;
57         case panda_file::Type::TypeId::I32:
58             return EtsType::INT;
59         case panda_file::Type::TypeId::I64:
60             return EtsType::LONG;
61         case panda_file::Type::TypeId::F32:
62             return EtsType::FLOAT;
63         case panda_file::Type::TypeId::F64:
64             return EtsType::DOUBLE;
65         case panda_file::Type::TypeId::REFERENCE:
66             return EtsType::OBJECT;
67         case panda_file::Type::TypeId::TAGGED:
68             return EtsType::UNKNOWN;
69         default:
70             return EtsType::UNKNOWN;
71     }
72     UNREACHABLE();
73 }
74 
75 // CC-OFFNXT(G.FUD.06) switch-case, ODR
ConvertEtsTypeToPandaType(const EtsType type)76 inline panda_file::Type ConvertEtsTypeToPandaType(const EtsType type)
77 {
78     switch (type) {
79         case EtsType::VOID:
80             return panda_file::Type(panda_file::Type::TypeId::VOID);
81         case EtsType::BOOLEAN:
82             return panda_file::Type(panda_file::Type::TypeId::U1);
83         case EtsType::BYTE:
84             return panda_file::Type(panda_file::Type::TypeId::I8);
85         case EtsType::CHAR:
86             return panda_file::Type(panda_file::Type::TypeId::U16);
87         case EtsType::SHORT:
88             return panda_file::Type(panda_file::Type::TypeId::I16);
89         case EtsType::INT:
90             return panda_file::Type(panda_file::Type::TypeId::I32);
91         case EtsType::LONG:
92             return panda_file::Type(panda_file::Type::TypeId::I64);
93         case EtsType::FLOAT:
94             return panda_file::Type(panda_file::Type::TypeId::F32);
95         case EtsType::DOUBLE:
96             return panda_file::Type(panda_file::Type::TypeId::F64);
97         case EtsType::OBJECT:
98             return panda_file::Type(panda_file::Type::TypeId::REFERENCE);
99         default:
100             return panda_file::Type(panda_file::Type::TypeId::INVALID);
101     }
102 }
103 
104 // CC-OFFNXT(G.FUD.06) switch-case, ODR
ConvertEtsPrimitiveTypeToString(const EtsType type)105 inline std::string ConvertEtsPrimitiveTypeToString(const EtsType type)
106 {
107     switch (type) {
108         case EtsType::VOID:
109             return "void";
110         case EtsType::BOOLEAN:
111             return "ets_boolean";
112         case EtsType::BYTE:
113             return "ets_byte";
114         case EtsType::CHAR:
115             return "ets_char";
116         case EtsType::SHORT:
117             return "ets_short";
118         case EtsType::INT:
119             return "ets_int";
120         case EtsType::LONG:
121             return "ets_long";
122         case EtsType::FLOAT:
123             return "ets_float";
124         case EtsType::DOUBLE:
125             return "ets_double";
126         default:
127             UNREACHABLE();
128     }
129 }
130 
131 }  // namespace ark::ets
132 
133 #endif  // PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_TYPE_H_
134