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
16 #ifndef PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_ARRAYBUFFER_INL_H
17 #define PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_ARRAYBUFFER_INL_H
18
19 #include "runtime/include/thread_scopes.h"
20 #include "plugins/ets/runtime/types/ets_arraybuffer.h"
21
22 namespace ark::ets {
23
24 // CC-OFFNXT(G.PRE.02, G.PRE.06) code generation
25 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
26 #define EXECUTE_VOID_METHOD_DEPENDS_ON_TYPE(methodName, type, obj, ...) \
27 do { \
28 if constexpr (std::is_same_v<type, int8_t>) { \
29 reinterpret_cast<EtsByteArray *>(obj)->methodName(__VA_ARGS__); \
30 } else if constexpr (std::is_same_v<type, int16_t>) { \
31 reinterpret_cast<EtsShortArray *>(obj)->methodName(__VA_ARGS__); \
32 } else if constexpr (std::is_same_v<type, int32_t>) { \
33 reinterpret_cast<EtsIntArray *>(obj)->methodName(__VA_ARGS__); \
34 } else if constexpr (std::is_same_v<type, int64_t>) { \
35 reinterpret_cast<EtsLongArray *>(obj)->methodName(__VA_ARGS__); \
36 } else if constexpr (std::is_same_v<type, uint8_t>) { \
37 reinterpret_cast<EtsBooleanArray *>(obj)->methodName(__VA_ARGS__); \
38 } else if constexpr (std::is_same_v<type, uint16_t>) { \
39 reinterpret_cast<EtsCharArray *>(obj)->methodName(__VA_ARGS__); \
40 } else if constexpr (std::is_same_v<type, uint32_t>) { \
41 reinterpret_cast<EtsUintArray *>(obj)->methodName(__VA_ARGS__); \
42 } else if constexpr (std::is_same_v<type, uint64_t>) { \
43 reinterpret_cast<EtsUlongArray *>(obj)->methodName(__VA_ARGS__); \
44 } else { \
45 UNREACHABLE(); \
46 } \
47 } while (false)
48
49 // CC-OFFNXT(G.PRE.02, G.PRE.06) code generation
50 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
51 #define EXECUTE_METHOD_DEPENDS_ON_TYPE(varToStore, methodName, type, obj, ...) \
52 do { \
53 if constexpr (std::is_same_v<type, int8_t>) { \
54 varToStore = reinterpret_cast<EtsByteArray *>(obj)->methodName(__VA_ARGS__); \
55 } else if constexpr (std::is_same_v<type, int16_t>) { \
56 varToStore = reinterpret_cast<EtsShortArray *>(obj)->methodName(__VA_ARGS__); \
57 } else if constexpr (std::is_same_v<type, int32_t>) { \
58 varToStore = reinterpret_cast<EtsIntArray *>(obj)->methodName(__VA_ARGS__); \
59 } else if constexpr (std::is_same_v<type, int64_t>) { \
60 varToStore = reinterpret_cast<EtsLongArray *>(obj)->methodName(__VA_ARGS__); \
61 } else if constexpr (std::is_same_v<type, uint8_t>) { \
62 varToStore = reinterpret_cast<EtsBooleanArray *>(obj)->methodName(__VA_ARGS__); \
63 } else if constexpr (std::is_same_v<type, uint16_t>) { \
64 varToStore = reinterpret_cast<EtsCharArray *>(obj)->methodName(__VA_ARGS__); \
65 } else if constexpr (std::is_same_v<type, uint32_t>) { \
66 varToStore = reinterpret_cast<EtsUintArray *>(obj)->methodName(__VA_ARGS__); \
67 } else if constexpr (std::is_same_v<type, uint64_t>) { \
68 varToStore = reinterpret_cast<EtsUlongArray *>(obj)->methodName(__VA_ARGS__); \
69 } else { \
70 UNREACHABLE(); \
71 } \
72 } while (false)
73
74 template <typename T>
GetElement(uint32_t index,uint32_t byteOffset)75 T EtsEscompatArrayBuffer::GetElement(uint32_t index, uint32_t byteOffset)
76 {
77 auto *currentCoro = EtsCoroutine::GetCurrent();
78 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
79 T val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
80 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, GetVolatile, T, obj, index, byteOffset);
81 return val;
82 }
83
84 template <typename T>
SetElement(uint32_t index,uint32_t byteOffset,T element)85 void EtsEscompatArrayBuffer::SetElement(uint32_t index, uint32_t byteOffset, T element)
86 {
87 auto *currentCoro = EtsCoroutine::GetCurrent();
88 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
89 EXECUTE_VOID_METHOD_DEPENDS_ON_TYPE(SetVolatile, T, obj, index, byteOffset, element);
90 }
91
92 template <typename T>
GetVolatileElement(uint32_t index,uint32_t byteOffset)93 T EtsEscompatArrayBuffer::GetVolatileElement(uint32_t index, uint32_t byteOffset)
94 {
95 auto *currentCoro = EtsCoroutine::GetCurrent();
96 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
97 T val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
98 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, GetVolatile, T, obj, index, byteOffset);
99 return val;
100 }
101
102 template <typename T>
SetVolatileElement(uint32_t index,uint32_t byteOffset,T element)103 void EtsEscompatArrayBuffer::SetVolatileElement(uint32_t index, uint32_t byteOffset, T element)
104 {
105 auto *currentCoro = EtsCoroutine::GetCurrent();
106 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
107 EXECUTE_VOID_METHOD_DEPENDS_ON_TYPE(SetVolatile, T, obj, index, byteOffset, element);
108 }
109
110 template <typename T>
CompareAndExchangeElement(uint32_t index,uint32_t byteOffset,T oldElement,T newElement,bool strong)111 std::pair<bool, T> EtsEscompatArrayBuffer::CompareAndExchangeElement(uint32_t index, uint32_t byteOffset, T oldElement,
112 T newElement, bool strong)
113 {
114 auto *currentCoro = EtsCoroutine::GetCurrent();
115 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
116 std::pair<bool, T> val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
117 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, CompareAndExchange, T, obj, index, byteOffset, oldElement, newElement, strong);
118 return val;
119 }
120 template <typename T>
ExchangeElement(uint32_t index,uint32_t byteOffset,T element)121 T EtsEscompatArrayBuffer::ExchangeElement(uint32_t index, uint32_t byteOffset, T element)
122 {
123 auto *currentCoro = EtsCoroutine::GetCurrent();
124 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
125 T val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
126 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, Exchange, T, obj, index, byteOffset, element);
127 return val;
128 }
129
130 template <typename T>
GetAndAdd(uint32_t index,uint32_t byteOffset,T element)131 T EtsEscompatArrayBuffer::GetAndAdd(uint32_t index, uint32_t byteOffset, T element)
132 {
133 auto *currentCoro = EtsCoroutine::GetCurrent();
134 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
135 T val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
136 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, GetAndAdd, T, obj, index, byteOffset, element);
137 return val;
138 }
139
140 template <typename T>
GetAndSub(uint32_t index,uint32_t byteOffset,T element)141 T EtsEscompatArrayBuffer::GetAndSub(uint32_t index, uint32_t byteOffset, T element)
142 {
143 auto *currentCoro = EtsCoroutine::GetCurrent();
144 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
145 T val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
146 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, GetAndSub, T, obj, index, byteOffset, element);
147 return val;
148 }
149
150 template <typename T>
GetAndBitwiseOr(uint32_t index,uint32_t byteOffset,T element)151 T EtsEscompatArrayBuffer::GetAndBitwiseOr(uint32_t index, uint32_t byteOffset, T element)
152 {
153 auto *currentCoro = EtsCoroutine::GetCurrent();
154 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
155 T val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
156 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, GetAndBitwiseOr, T, obj, index, byteOffset, element);
157 return val;
158 }
159
160 template <typename T>
GetAndBitwiseAnd(uint32_t index,uint32_t byteOffset,T element)161 T EtsEscompatArrayBuffer::GetAndBitwiseAnd(uint32_t index, uint32_t byteOffset, T element)
162 {
163 auto *currentCoro = EtsCoroutine::GetCurrent();
164 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
165 T val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
166 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, GetAndBitwiseAnd, T, obj, index, byteOffset, element);
167 return val;
168 }
169
170 template <typename T>
GetAndBitwiseXor(uint32_t index,uint32_t byteOffset,T element)171 T EtsEscompatArrayBuffer::GetAndBitwiseXor(uint32_t index, uint32_t byteOffset, T element)
172 {
173 auto *currentCoro = EtsCoroutine::GetCurrent();
174 auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsEscompatArrayBuffer, managedData_));
175 T val; // CC-OFF(G.EXP.09-CPP) variable is used in code gen macro
176 EXECUTE_METHOD_DEPENDS_ON_TYPE(val, GetAndBitwiseXor, T, obj, index, byteOffset, element);
177 return val;
178 }
179
180 #undef EXECUTE_VOID_METHOD_DEPENDS_ON_TYPE
181 #undef EXECUTE_METHOD_DEPENDS_ON_TYPE
182
183 } // namespace ark::ets
184
185 #endif // PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_ARRAYBUFFER_INL_H
186