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
16 #ifndef PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JSVALUE_CALL_H
17 #define PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JSVALUE_CALL_H
18
19 #include <node_api.h>
20 #include "utils/span.h"
21 #include "libpandabase/mem/mem.h"
22 namespace panda {
23 class Method;
24 } // namespace panda
25
26 namespace panda::mem {
27 class Reference;
28 } // namespace panda::mem
29
30 namespace panda::ets {
31 class EtsObject;
32 class EtsString;
33 class EtsCoroutine;
34 } // namespace panda::ets
35
36 namespace panda::ets::interop::js {
37 class InteropCtx;
38
39 napi_value CallEtsFunctionImpl(napi_env env, Span<napi_value> jsargv);
40 napi_value EtsLambdaProxyInvoke(napi_env env, napi_callback_info cbinfo);
41
42 uint8_t JSRuntimeInitJSCallClass(EtsString *clsStr);
43 uint8_t JSRuntimeInitJSNewClass(EtsString *clsStr);
44
45 template <bool IS_STATIC>
46 napi_value EtsCallImpl(EtsCoroutine *coro, InteropCtx *ctx, Method *method, Span<napi_value> jsargv,
47 EtsObject *thisObj);
48
EtsCallImplInstance(EtsCoroutine * coro,InteropCtx * ctx,Method * method,Span<napi_value> jsargv,EtsObject * thisObj)49 inline napi_value EtsCallImplInstance(EtsCoroutine *coro, InteropCtx *ctx, Method *method, Span<napi_value> jsargv,
50 EtsObject *thisObj)
51 {
52 return EtsCallImpl<false>(coro, ctx, method, jsargv, thisObj);
53 }
54
EtsCallImplStatic(EtsCoroutine * coro,InteropCtx * ctx,Method * method,Span<napi_value> jsargv)55 inline napi_value EtsCallImplStatic(EtsCoroutine *coro, InteropCtx *ctx, Method *method, Span<napi_value> jsargv)
56 {
57 return EtsCallImpl<true>(coro, ctx, method, jsargv, nullptr);
58 }
59
60 template <char DELIM = '.', typename F>
WalkQualifiedName(std::string_view name,F const & f)61 static bool WalkQualifiedName(std::string_view name, F const &f)
62 {
63 for (const char *p = name.data(); *p == DELIM;) {
64 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
65 auto e = ++p;
66 while (*e != '\0' && *e != DELIM) {
67 e++;
68 }
69 std::string substr(p, ToUintPtr(e) - ToUintPtr(p));
70 if (UNLIKELY(!f(substr))) {
71 return false;
72 }
73 p = e;
74 }
75 return true;
76 }
77
78 } // namespace panda::ets::interop::js
79
80 #endif // !PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JSVALUE_CALL_H
81