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