1 /**
2 * Copyright (c) 2023 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 #include "intrinsics.h"
17 #include "plugins/ets/runtime/interop_js/intrinsics_api.h"
18 #include "plugins/ets/runtime/types/ets_string.h"
19 #include "plugins/ets/runtime/types/ets_void.h"
20
21 namespace panda::ets::interop::js {
22
23 namespace notimpl {
24
NotImplementedHook()25 [[noreturn]] static void NotImplementedHook()
26 {
27 LOG(FATAL, ETS) << "NotImplementedHook called for JSRuntime intrinsic";
28 UNREACHABLE();
29 }
30
31 template <typename R, typename... Args>
NotImplementedAdapter(Args...args)32 static R NotImplementedAdapter([[maybe_unused]] Args... args)
33 {
34 NotImplementedHook();
35 }
36
37 static const IntrinsicsAPI S_INTRINSICS_API = {
38 // clang-format off
39 NotImplementedAdapter,
40 NotImplementedAdapter,
41 NotImplementedAdapter,
42 NotImplementedAdapter,
43 NotImplementedAdapter,
44 NotImplementedAdapter,
45 NotImplementedAdapter,
46 NotImplementedAdapter,
47 NotImplementedAdapter,
48 NotImplementedAdapter,
49 NotImplementedAdapter,
50 NotImplementedAdapter,
51 NotImplementedAdapter,
52 NotImplementedAdapter,
53 NotImplementedAdapter,
54 NotImplementedAdapter,
55 NotImplementedAdapter,
56 NotImplementedAdapter,
57 NotImplementedAdapter,
58 NotImplementedAdapter,
59 NotImplementedAdapter,
60 NotImplementedAdapter,
61 NotImplementedAdapter,
62 NotImplementedAdapter,
63 NotImplementedAdapter,
64 NotImplementedAdapter,
65 NotImplementedAdapter,
66 NotImplementedAdapter,
67 NotImplementedAdapter,
68 NotImplementedAdapter,
69 NotImplementedAdapter,
70 NotImplementedAdapter,
71 NotImplementedAdapter,
72 NotImplementedAdapter,
73 NotImplementedAdapter,
74 NotImplementedAdapter,
75 NotImplementedAdapter,
76 NotImplementedAdapter,
77 NotImplementedAdapter,
78 NotImplementedAdapter,
79 NotImplementedAdapter,
80 NotImplementedAdapter,
81 NotImplementedAdapter,
82 NotImplementedAdapter,
83 NotImplementedAdapter,
84 NotImplementedAdapter,
85 NotImplementedAdapter,
86 NotImplementedAdapter,
87 NotImplementedAdapter,
88 NotImplementedAdapter,
89 NotImplementedAdapter,
90 NotImplementedAdapter,
91 NotImplementedAdapter,
92 NotImplementedAdapter,
93 NotImplementedAdapter,
94 NotImplementedAdapter,
95 NotImplementedAdapter,
96 NotImplementedAdapter,
97 NotImplementedAdapter,
98 NotImplementedAdapter,
99 NotImplementedAdapter,
100 NotImplementedAdapter,
101 // clang-format on
102 };
103
104 } // namespace notimpl
105
106 static const IntrinsicsAPI *S_INTRINSICS_API = ¬impl::S_INTRINSICS_API;
107
JSRuntimeIntrinsicsSetIntrinsicsAPI(const IntrinsicsAPI * intrinsicsApi)108 PANDA_PUBLIC_API EtsVoid *JSRuntimeIntrinsicsSetIntrinsicsAPI(const IntrinsicsAPI *intrinsicsApi)
109 {
110 S_INTRINSICS_API = intrinsicsApi;
111 return EtsVoid::GetInstance();
112 }
113
114 namespace intrinsics {
115
JSRuntimeFinalizationQueueCallbackIntrinsic(EtsObject * obj)116 EtsVoid *JSRuntimeFinalizationQueueCallbackIntrinsic(EtsObject *obj)
117 {
118 S_INTRINSICS_API->JSRuntimeFinalizationQueueCallback(obj);
119 return EtsVoid::GetInstance();
120 }
121
JSRuntimeNewJSValueDoubleIntrinsic(double v)122 JSValue *JSRuntimeNewJSValueDoubleIntrinsic(double v)
123 {
124 return S_INTRINSICS_API->JSRuntimeNewJSValueDouble(v);
125 }
126
JSRuntimeNewJSValueStringIntrinsic(EtsString * v)127 JSValue *JSRuntimeNewJSValueStringIntrinsic(EtsString *v)
128 {
129 return S_INTRINSICS_API->JSRuntimeNewJSValueString(v);
130 }
131
JSRuntimeNewJSValueObjectIntrinsic(EtsObject * v)132 JSValue *JSRuntimeNewJSValueObjectIntrinsic(EtsObject *v)
133 {
134 return S_INTRINSICS_API->JSRuntimeNewJSValueObject(v);
135 }
136
JSRuntimeGetValueDoubleIntrinsic(JSValue * etsJsValue)137 double JSRuntimeGetValueDoubleIntrinsic(JSValue *etsJsValue)
138 {
139 return S_INTRINSICS_API->JSRuntimeGetValueDouble(etsJsValue);
140 }
141
JSRuntimeGetValueBooleanIntrinsic(JSValue * etsJsValue)142 uint8_t JSRuntimeGetValueBooleanIntrinsic(JSValue *etsJsValue)
143 {
144 return S_INTRINSICS_API->JSRuntimeGetValueBoolean(etsJsValue);
145 }
146
JSRuntimeGetValueStringIntrinsic(JSValue * etsJsValue)147 EtsString *JSRuntimeGetValueStringIntrinsic(JSValue *etsJsValue)
148 {
149 return S_INTRINSICS_API->JSRuntimeGetValueString(etsJsValue);
150 }
151
JSRuntimeGetValueObjectIntrinsic(JSValue * etsJsValue,EtsObject * cls)152 EtsObject *JSRuntimeGetValueObjectIntrinsic(JSValue *etsJsValue, EtsObject *cls)
153 {
154 return S_INTRINSICS_API->JSRuntimeGetValueObject(etsJsValue, cls);
155 }
156
JSRuntimeGetPropertyJSValueIntrinsic(JSValue * etsJsValue,EtsString * etsPropName)157 JSValue *JSRuntimeGetPropertyJSValueIntrinsic(JSValue *etsJsValue, EtsString *etsPropName)
158 {
159 return S_INTRINSICS_API->JSRuntimeGetPropertyJSValue(etsJsValue, etsPropName);
160 }
161
JSRuntimeGetPropertyDoubleIntrinsic(JSValue * etsJsValue,EtsString * etsPropName)162 double JSRuntimeGetPropertyDoubleIntrinsic(JSValue *etsJsValue, EtsString *etsPropName)
163 {
164 return S_INTRINSICS_API->JSRuntimeGetPropertyDouble(etsJsValue, etsPropName);
165 }
166
JSRuntimeGetPropertyStringIntrinsic(JSValue * etsJsValue,EtsString * etsPropName)167 EtsString *JSRuntimeGetPropertyStringIntrinsic(JSValue *etsJsValue, EtsString *etsPropName)
168 {
169 return S_INTRINSICS_API->JSRuntimeGetPropertyString(etsJsValue, etsPropName);
170 }
171
JSRuntimeSetPropertyJSValueIntrinsic(JSValue * etsJsValue,EtsString * etsPropName,JSValue * value)172 EtsVoid *JSRuntimeSetPropertyJSValueIntrinsic(JSValue *etsJsValue, EtsString *etsPropName, JSValue *value)
173 {
174 S_INTRINSICS_API->JSRuntimeSetPropertyJSValue(etsJsValue, etsPropName, value);
175 return EtsVoid::GetInstance();
176 }
177
JSRuntimeSetPropertyDoubleIntrinsic(JSValue * etsJsValue,EtsString * etsPropName,double value)178 EtsVoid *JSRuntimeSetPropertyDoubleIntrinsic(JSValue *etsJsValue, EtsString *etsPropName, double value)
179 {
180 S_INTRINSICS_API->JSRuntimeSetPropertyDouble(etsJsValue, etsPropName, value);
181 return EtsVoid::GetInstance();
182 }
183
JSRuntimeSetPropertyStringIntrinsic(JSValue * etsJsValue,EtsString * etsPropName,EtsString * value)184 EtsVoid *JSRuntimeSetPropertyStringIntrinsic(JSValue *etsJsValue, EtsString *etsPropName, EtsString *value)
185 {
186 S_INTRINSICS_API->JSRuntimeSetPropertyString(etsJsValue, etsPropName, value);
187 return EtsVoid::GetInstance();
188 }
189
JSRuntimeGetElementJSValueIntrinsic(JSValue * etsJsValue,int32_t index)190 JSValue *JSRuntimeGetElementJSValueIntrinsic(JSValue *etsJsValue, int32_t index)
191 {
192 return S_INTRINSICS_API->JSRuntimeGetElementJSValue(etsJsValue, index);
193 }
194
JSRuntimeGetElementDoubleIntrinsic(JSValue * etsJsValue,int32_t index)195 double JSRuntimeGetElementDoubleIntrinsic(JSValue *etsJsValue, int32_t index)
196 {
197 return S_INTRINSICS_API->JSRuntimeGetElementDouble(etsJsValue, index);
198 }
199
JSRuntimeGetUndefinedIntrinsic()200 JSValue *JSRuntimeGetUndefinedIntrinsic()
201 {
202 return S_INTRINSICS_API->JSRuntimeGetUndefined();
203 }
204
JSRuntimeGetNullIntrinsic()205 JSValue *JSRuntimeGetNullIntrinsic()
206 {
207 return S_INTRINSICS_API->JSRuntimeGetNull();
208 }
209
JSRuntimeGetGlobalIntrinsic()210 JSValue *JSRuntimeGetGlobalIntrinsic()
211 {
212 return S_INTRINSICS_API->JSRuntimeGetGlobal();
213 }
214
JSRuntimeCreateObjectIntrinsic()215 JSValue *JSRuntimeCreateObjectIntrinsic()
216 {
217 return S_INTRINSICS_API->JSRuntimeCreateObject();
218 }
219
JSRuntimeInstanceOfIntrinsic(JSValue * object,JSValue * ctor)220 uint8_t JSRuntimeInstanceOfIntrinsic(JSValue *object, JSValue *ctor)
221 {
222 return S_INTRINSICS_API->JSRuntimeInstanceOf(object, ctor);
223 }
224
JSRuntimeInitJSCallClassIntrinsic(EtsString * clsName)225 uint8_t JSRuntimeInitJSCallClassIntrinsic(EtsString *clsName)
226 {
227 return S_INTRINSICS_API->JSRuntimeInitJSCallClass(clsName);
228 }
229
JSRuntimeInitJSNewClassIntrinsic(EtsString * clsName)230 uint8_t JSRuntimeInitJSNewClassIntrinsic(EtsString *clsName)
231 {
232 return S_INTRINSICS_API->JSRuntimeInitJSNewClass(clsName);
233 }
234
JSRuntimeCreateLambdaProxyIntrinsic(EtsObject * lambda)235 JSValue *JSRuntimeCreateLambdaProxyIntrinsic(EtsObject *lambda)
236 {
237 return S_INTRINSICS_API->JSRuntimeCreateLambdaProxy(lambda);
238 }
239
JSRuntimeLoadModuleIntrinsic(EtsString * module)240 JSValue *JSRuntimeLoadModuleIntrinsic(EtsString *module)
241 {
242 return S_INTRINSICS_API->JSRuntimeLoadModule(module);
243 }
244
JSRuntimeStrictEqualIntrinsic(JSValue * lhs,JSValue * rhs)245 uint8_t JSRuntimeStrictEqualIntrinsic(JSValue *lhs, JSValue *rhs)
246 {
247 return S_INTRINSICS_API->JSRuntimeStrictEqual(lhs, rhs);
248 }
249
250 // Compiler intrinsics for fast interop
CompilerGetJSNamedPropertyIntrinsic(void * val,void * propName)251 void *CompilerGetJSNamedPropertyIntrinsic(void *val, void *propName)
252 {
253 return S_INTRINSICS_API->CompilerGetJSNamedProperty(val, reinterpret_cast<char *>(propName));
254 }
255
CompilerResolveQualifiedJSCallIntrinsic(void * val,EtsString * qnameStr)256 void *CompilerResolveQualifiedJSCallIntrinsic(void *val, EtsString *qnameStr)
257 {
258 return S_INTRINSICS_API->CompilerResolveQualifiedJSCall(val, qnameStr);
259 }
260
CompilerJSCallCheckIntrinsic(void * fn)261 void *CompilerJSCallCheckIntrinsic(void *fn)
262 {
263 return S_INTRINSICS_API->CompilerJSCallCheck(fn);
264 }
265
CompilerJSCallFunctionIntrinsic(void * obj,void * fn,uint32_t argc,void * args)266 void *CompilerJSCallFunctionIntrinsic(void *obj, void *fn, uint32_t argc, void *args)
267 {
268 return S_INTRINSICS_API->CompilerJSCallFunction(obj, fn, argc, args);
269 }
270
CompilerJSCallVoidFunctionIntrinsic(void * obj,void * fn,uint32_t argc,void * args)271 void CompilerJSCallVoidFunctionIntrinsic(void *obj, void *fn, uint32_t argc, void *args)
272 {
273 S_INTRINSICS_API->CompilerJSCallVoidFunction(obj, fn, argc, args);
274 }
275
CompilerJSNewInstanceIntrinsic(void * fn,uint32_t argc,void * args)276 void *CompilerJSNewInstanceIntrinsic(void *fn, uint32_t argc, void *args)
277 {
278 return S_INTRINSICS_API->CompilerJSNewInstance(fn, argc, args);
279 }
280
CompilerConvertVoidToLocalIntrinsic()281 void *CompilerConvertVoidToLocalIntrinsic()
282 {
283 return S_INTRINSICS_API->CompilerConvertVoidToLocal();
284 }
285
286 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
287 #define CONVERT_LOCAL_VALUE(type, cpptype) \
288 void *CompilerConvert##type##ToLocalIntrinsic(cpptype etsVal) \
289 { \
290 return S_INTRINSICS_API->CompilerConvert##type##ToLocal(etsVal); \
291 } \
292 \
293 cpptype CompilerConvertLocalTo##type##Intrinsic(void *val) \
294 { \
295 return S_INTRINSICS_API->CompilerConvertLocalTo##type(val); \
296 }
297
CONVERT_LOCAL_VALUE(U1,uint8_t)298 CONVERT_LOCAL_VALUE(U1, uint8_t)
299 CONVERT_LOCAL_VALUE(U8, uint8_t)
300 CONVERT_LOCAL_VALUE(I8, int8_t)
301 CONVERT_LOCAL_VALUE(U16, uint16_t)
302 CONVERT_LOCAL_VALUE(I16, int16_t)
303 CONVERT_LOCAL_VALUE(U32, uint32_t)
304 CONVERT_LOCAL_VALUE(I32, int32_t)
305 CONVERT_LOCAL_VALUE(U64, uint64_t)
306 CONVERT_LOCAL_VALUE(I64, int64_t)
307 CONVERT_LOCAL_VALUE(F32, float)
308 CONVERT_LOCAL_VALUE(F64, double)
309 CONVERT_LOCAL_VALUE(JSValue, JSValue *)
310
311 #undef CONVERT_LOCAL_VALUE
312
313 void *CompilerConvertRefTypeToLocalIntrinsic(EtsObject *etsVal)
314 {
315 return S_INTRINSICS_API->CompilerConvertRefTypeToLocal(etsVal);
316 }
CompilerConvertLocalToStringIntrinsic(void * val)317 EtsString *CompilerConvertLocalToStringIntrinsic(void *val)
318 {
319 return S_INTRINSICS_API->CompilerConvertLocalToString(val);
320 }
321
CompilerConvertLocalToRefTypeIntrinsic(void * klassPtr,void * val)322 EtsObject *CompilerConvertLocalToRefTypeIntrinsic(void *klassPtr, void *val)
323 {
324 return S_INTRINSICS_API->CompilerConvertLocalToRefType(klassPtr, val);
325 }
326
CompilerCreateLocalScopeIntrinsic()327 void CompilerCreateLocalScopeIntrinsic()
328 {
329 S_INTRINSICS_API->CreateLocalScope();
330 }
331
CompilerDestroyLocalScopeIntrinsic()332 void CompilerDestroyLocalScopeIntrinsic()
333 {
334 S_INTRINSICS_API->CompilerDestroyLocalScope();
335 }
336
337 } // namespace intrinsics
338 } // namespace panda::ets::interop::js
339