• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-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 
16 #include "intrinsics.h"
17 #include "plugins/ets/runtime/interop_js/intrinsics_api_impl.h"
18 #include "plugins/ets/runtime/types/ets_string.h"
19 #include "interop_js/call/call.h"
20 
21 namespace ark::ets::interop::js::intrinsics {
22 
JSRuntimeFinalizationRegistryCallbackIntrinsic(EtsObject * obj)23 void JSRuntimeFinalizationRegistryCallbackIntrinsic(EtsObject *obj)
24 {
25     JSRuntimeFinalizationRegistryCallback(obj);
26 }
27 
JSRuntimeNewJSValueDoubleIntrinsic(double v)28 JSValue *JSRuntimeNewJSValueDoubleIntrinsic(double v)
29 {
30     return JSRuntimeNewJSValueDouble(v);
31 }
32 
JSRuntimeNewJSValueBooleanIntrinsic(uint8_t v)33 JSValue *JSRuntimeNewJSValueBooleanIntrinsic(uint8_t v)
34 {
35     return JSRuntimeNewJSValueBoolean(v);
36 }
37 
JSRuntimeNewJSValueStringIntrinsic(EtsString * v)38 JSValue *JSRuntimeNewJSValueStringIntrinsic(EtsString *v)
39 {
40     return JSRuntimeNewJSValueString(v);
41 }
42 
JSRuntimeNewJSValueObjectIntrinsic(EtsObject * v)43 JSValue *JSRuntimeNewJSValueObjectIntrinsic(EtsObject *v)
44 {
45     return JSRuntimeNewJSValueObject(v);
46 }
47 
JSRuntimeGetValueDoubleIntrinsic(JSValue * etsJsValue)48 double JSRuntimeGetValueDoubleIntrinsic(JSValue *etsJsValue)
49 {
50     return JSRuntimeGetValueDouble(etsJsValue);
51 }
52 
JSRuntimeGetValueBooleanIntrinsic(JSValue * etsJsValue)53 uint8_t JSRuntimeGetValueBooleanIntrinsic(JSValue *etsJsValue)
54 {
55     return JSRuntimeGetValueBoolean(etsJsValue);
56 }
57 
JSRuntimeGetValueStringIntrinsic(JSValue * etsJsValue)58 EtsString *JSRuntimeGetValueStringIntrinsic(JSValue *etsJsValue)
59 {
60     return JSRuntimeGetValueString(etsJsValue);
61 }
62 
JSRuntimeGetValueObjectIntrinsic(JSValue * etsJsValue,EtsObject * cls)63 EtsObject *JSRuntimeGetValueObjectIntrinsic(JSValue *etsJsValue, EtsObject *cls)
64 {
65     return JSRuntimeGetValueObject(etsJsValue, cls);
66 }
67 
JSRuntimeGetPropertyJSValueIntrinsic(JSValue * etsJsValue,EtsString * etsPropName)68 JSValue *JSRuntimeGetPropertyJSValueIntrinsic(JSValue *etsJsValue, EtsString *etsPropName)
69 {
70     return JSValueNamedGetter<JSConvertJSValue>(etsJsValue, etsPropName);
71 }
72 
JSRuntimeGetPropertyDoubleIntrinsic(JSValue * etsJsValue,EtsString * etsPropName)73 double JSRuntimeGetPropertyDoubleIntrinsic(JSValue *etsJsValue, EtsString *etsPropName)
74 {
75     return JSValueNamedGetter<JSConvertF64>(etsJsValue, etsPropName);
76 }
77 
JSRuntimeGetPropertyStringIntrinsic(JSValue * etsJsValue,EtsString * etsPropName)78 EtsString *JSRuntimeGetPropertyStringIntrinsic(JSValue *etsJsValue, EtsString *etsPropName)
79 {
80     return JSValueNamedGetter<JSConvertString>(etsJsValue, etsPropName);
81 }
82 
JSRuntimeGetPropertyBooleanIntrinsic(JSValue * etsJsValue,EtsString * etsPropName)83 uint8_t JSRuntimeGetPropertyBooleanIntrinsic(JSValue *etsJsValue, EtsString *etsPropName)
84 {
85     return static_cast<uint8_t>(JSValueNamedGetter<JSConvertU1>(etsJsValue, etsPropName));
86 }
87 
JSRuntimeSetPropertyJSValueIntrinsic(JSValue * etsJsValue,EtsString * etsPropName,JSValue * value)88 void JSRuntimeSetPropertyJSValueIntrinsic(JSValue *etsJsValue, EtsString *etsPropName, JSValue *value)
89 {
90     JSValueNamedSetter<JSConvertJSValue>(etsJsValue, etsPropName, value);
91 }
92 
JSRuntimeSetPropertyDoubleIntrinsic(JSValue * etsJsValue,EtsString * etsPropName,double value)93 void JSRuntimeSetPropertyDoubleIntrinsic(JSValue *etsJsValue, EtsString *etsPropName, double value)
94 {
95     JSValueNamedSetter<JSConvertF64>(etsJsValue, etsPropName, value);
96 }
97 
JSRuntimeSetPropertyStringIntrinsic(JSValue * etsJsValue,EtsString * etsPropName,EtsString * value)98 void JSRuntimeSetPropertyStringIntrinsic(JSValue *etsJsValue, EtsString *etsPropName, EtsString *value)
99 {
100     JSValueNamedSetter<JSConvertString>(etsJsValue, etsPropName, value);
101 }
102 
JSRuntimeSetPropertyBooleanIntrinsic(JSValue * etsJsValue,EtsString * etsPropName,uint8_t value)103 void JSRuntimeSetPropertyBooleanIntrinsic(JSValue *etsJsValue, EtsString *etsPropName, uint8_t value)
104 {
105     JSValueNamedSetter<JSConvertU1>(etsJsValue, etsPropName, static_cast<bool>(value));
106 }
107 
JSRuntimeGetElementJSValueIntrinsic(JSValue * etsJsValue,int32_t index)108 JSValue *JSRuntimeGetElementJSValueIntrinsic(JSValue *etsJsValue, int32_t index)
109 {
110     return JSValueIndexedGetter<JSConvertJSValue>(etsJsValue, index);
111 }
112 
JSRuntimeGetElementDoubleIntrinsic(JSValue * etsJsValue,int32_t index)113 double JSRuntimeGetElementDoubleIntrinsic(JSValue *etsJsValue, int32_t index)
114 {
115     return JSValueIndexedGetter<JSConvertF64>(etsJsValue, index);
116 }
117 
JSRuntimeGetUndefinedIntrinsic()118 JSValue *JSRuntimeGetUndefinedIntrinsic()
119 {
120     return JSRuntimeGetUndefined();
121 }
122 
JSRuntimeGetNullIntrinsic()123 JSValue *JSRuntimeGetNullIntrinsic()
124 {
125     return JSRuntimeGetNull();
126 }
127 
JSRuntimeGetGlobalIntrinsic()128 JSValue *JSRuntimeGetGlobalIntrinsic()
129 {
130     return JSRuntimeGetGlobal();
131 }
132 
JSRuntimeCreateObjectIntrinsic()133 JSValue *JSRuntimeCreateObjectIntrinsic()
134 {
135     return JSRuntimeCreateObject();
136 }
137 
JSRuntimeInstanceOfDynamicIntrinsic(JSValue * object,JSValue * ctor)138 uint8_t JSRuntimeInstanceOfDynamicIntrinsic(JSValue *object, JSValue *ctor)
139 {
140     return JSRuntimeInstanceOfDynamic(object, ctor);
141 }
142 
JSRuntimeInstanceOfStaticIntrinsic(JSValue * object,EtsObject * cls)143 uint8_t JSRuntimeInstanceOfStaticIntrinsic(JSValue *object, EtsObject *cls)
144 {
145     // NOTE(v.cherkashin):
146     //  Delete cast and use 'EtsClass *cls' instead of 'EtsObject *cls' when issue #15273 was resolved
147     ASSERT(cls->GetClass()->IsClassClass());
148     return JSRuntimeInstanceOfStatic(object, reinterpret_cast<EtsClass *>(cls));
149 }
150 
JSRuntimeInitJSCallClassIntrinsic(EtsString * clsName)151 uint8_t JSRuntimeInitJSCallClassIntrinsic(EtsString *clsName)
152 {
153     return JSRuntimeInitJSCallClass(clsName);
154 }
155 
JSRuntimeInitJSNewClassIntrinsic(EtsString * clsName)156 uint8_t JSRuntimeInitJSNewClassIntrinsic(EtsString *clsName)
157 {
158     return JSRuntimeInitJSNewClass(clsName);
159 }
160 
JSRuntimeLoadModuleIntrinsic(EtsString * module)161 JSValue *JSRuntimeLoadModuleIntrinsic(EtsString *module)
162 {
163     return JSRuntimeLoadModule(module);
164 }
165 
JSRuntimeStrictEqualIntrinsic(JSValue * lhs,JSValue * rhs)166 uint8_t JSRuntimeStrictEqualIntrinsic(JSValue *lhs, JSValue *rhs)
167 {
168     return JSRuntimeStrictEqual(lhs, rhs);
169 }
170 
JSValueToStringIntrinsic(JSValue * object)171 EtsString *JSValueToStringIntrinsic(JSValue *object)
172 {
173     return JSValueToString(object);
174 }
175 
JSONStringifyIntrinsic(JSValue * obj)176 EtsString *JSONStringifyIntrinsic(JSValue *obj)
177 {
178     return JSONStringify(obj);
179 }
180 
181 // Compiler intrinsics for fast interop
CompilerGetJSNamedPropertyIntrinsic(void * val,void * propName)182 void *CompilerGetJSNamedPropertyIntrinsic(void *val, void *propName)
183 {
184     return CompilerGetJSNamedProperty(val, reinterpret_cast<char *>(propName));
185 }
186 
CompilerGetJSPropertyIntrinsic(void * val,void * prop)187 void *CompilerGetJSPropertyIntrinsic(void *val, void *prop)
188 {
189     return CompilerGetJSProperty(val, prop);
190 }
191 
CompilerGetJSElementIntrinsic(void * val,int32_t index)192 void *CompilerGetJSElementIntrinsic(void *val, int32_t index)
193 {
194     return CompilerGetJSElement(val, index);
195 }
196 
CompilerJSCallCheckIntrinsic(void * fn)197 void *CompilerJSCallCheckIntrinsic(void *fn)
198 {
199     return CompilerJSCallCheck(fn);
200 }
201 
CompilerJSCallFunctionIntrinsic(void * obj,void * fn,uint32_t argc,void * args)202 void *CompilerJSCallFunctionIntrinsic(void *obj, void *fn, uint32_t argc, void *args)
203 {
204     return CompilerJSCallFunction<true>(obj, fn, argc, args);
205 }
206 
CompilerJSCallVoidFunctionIntrinsic(void * obj,void * fn,uint32_t argc,void * args)207 void CompilerJSCallVoidFunctionIntrinsic(void *obj, void *fn, uint32_t argc, void *args)
208 {
209     CompilerJSCallFunction<false>(obj, fn, argc, args);
210 }
211 
CompilerJSNewInstanceIntrinsic(void * fn,uint32_t argc,void * args)212 void *CompilerJSNewInstanceIntrinsic(void *fn, uint32_t argc, void *args)
213 {
214     return CompilerJSNewInstance(fn, argc, args);
215 }
216 
CompilerConvertVoidToLocalIntrinsic()217 void *CompilerConvertVoidToLocalIntrinsic()
218 {
219     return CompilerConvertVoidToLocal();
220 }
221 
222 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
223 #define CONVERT_LOCAL_VALUE(type, cpptype)                        \
224     void *CompilerConvert##type##ToLocalIntrinsic(cpptype etsVal) \
225     {                                                             \
226         return ConvertToLocal<JSConvert##type>(etsVal);           \
227     }                                                             \
228                                                                   \
229     cpptype CompilerConvertLocalTo##type##Intrinsic(void *val)    \
230     {                                                             \
231         return ConvertFromLocal<JSConvert##type>(val);            \
232     }
233 
CONVERT_LOCAL_VALUE(U1,uint8_t)234 CONVERT_LOCAL_VALUE(U1, uint8_t)
235 CONVERT_LOCAL_VALUE(U8, uint8_t)
236 CONVERT_LOCAL_VALUE(I8, int8_t)
237 CONVERT_LOCAL_VALUE(U16, uint16_t)
238 CONVERT_LOCAL_VALUE(I16, int16_t)
239 CONVERT_LOCAL_VALUE(U32, uint32_t)
240 CONVERT_LOCAL_VALUE(I32, int32_t)
241 CONVERT_LOCAL_VALUE(U64, uint64_t)
242 CONVERT_LOCAL_VALUE(I64, int64_t)
243 CONVERT_LOCAL_VALUE(F32, float)
244 CONVERT_LOCAL_VALUE(F64, double)
245 CONVERT_LOCAL_VALUE(JSValue, JSValue *)
246 
247 #undef CONVERT_LOCAL_VALUE
248 
249 void *CompilerConvertRefTypeToLocalIntrinsic(EtsObject *etsVal)
250 {
251     return CompilerConvertRefTypeToLocal(etsVal);
252 }
CompilerConvertLocalToStringIntrinsic(void * val)253 EtsString *CompilerConvertLocalToStringIntrinsic(void *val)
254 {
255     return CompilerConvertLocalToString(val);
256 }
257 
CompilerConvertLocalToRefTypeIntrinsic(void * klassPtr,void * val)258 EtsObject *CompilerConvertLocalToRefTypeIntrinsic(void *klassPtr, void *val)
259 {
260     return CompilerConvertLocalToRefType(klassPtr, val);
261 }
262 
CompilerCreateLocalScopeIntrinsic()263 void CompilerCreateLocalScopeIntrinsic()
264 {
265     CreateLocalScope();
266 }
267 
CompilerDestroyLocalScopeIntrinsic()268 void CompilerDestroyLocalScopeIntrinsic()
269 {
270     CompilerDestroyLocalScope();
271 }
272 
CompilerLoadJSConstantPoolIntrinsic()273 void *CompilerLoadJSConstantPoolIntrinsic()
274 {
275     return CompilerLoadJSConstantPool();
276 }
277 
CompilerInitJSCallClassForCtxIntrinsic(void * klass)278 void CompilerInitJSCallClassForCtxIntrinsic(void *klass)
279 {
280     return CompilerInitJSCallClassForCtx(klass);
281 }
282 
PromiseInteropResolveIntrinsic(EtsObject * value,EtsLong deferred)283 void PromiseInteropResolveIntrinsic(EtsObject *value, EtsLong deferred)
284 {
285     PromiseInteropResolve(value, deferred);
286 }
287 
PromiseInteropRejectIntrinsic(EtsObject * value,EtsLong deferred)288 void PromiseInteropRejectIntrinsic(EtsObject *value, EtsLong deferred)
289 {
290     PromiseInteropReject(value, deferred);
291 }
292 
293 }  // namespace ark::ets::interop::js::intrinsics
294