• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-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 #include "libpandabase/macros.h"
17 #include "interop_js/napi_impl/napi_impl.h"
18 #include "interop_js/napi_impl/detail/enumerate_napi.h"
19 #include "interop_js/interop_common.h"
20 #include "utils/logger.h"
21 #include "interop_js/logger.h"
22 
23 #include <iostream>
24 
25 #if defined(PANDA_JS_ETS_HYBRID_MODE)
26 // NOLINTBEGIN(readability-identifier-naming)
27 napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_wrap_with_xref(napi_env env,napi_value js_object,void * native_object,napi_finalize finalize_cb,napi_ref * result)28 napi_wrap_with_xref([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value js_object,
29                     [[maybe_unused]] void *native_object, [[maybe_unused]] napi_finalize finalize_cb,
30                     [[maybe_unused]] napi_ref *result)
31 {
32     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
33                        << " is implemented in later versions of OHOS, please update." << std::endl;
34     return napi_ok;
35 }
36 
37 napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_xref_unwrap(napi_env env,napi_value js_object,void ** result)38 napi_xref_unwrap([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value js_object, [[maybe_unused]] void **result)
39 {
40     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
41                        << " is implemented in later versions of OHOS, please update." << std::endl;
42     return napi_ok;
43 }
44 
45 napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_create_xref(napi_env env,napi_value value,uint32_t initial_refcount,napi_ref * result)46 napi_create_xref([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value value,
47                  [[maybe_unused]] uint32_t initial_refcount, [[maybe_unused]] napi_ref *result)
48 {
49     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
50                        << " is implemented in later versions of OHOS, please update." << std::endl;
51     return napi_ok;
52 }
53 
54 napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_mark_from_object(napi_env env,napi_ref ref)55 napi_mark_from_object([[maybe_unused]] napi_env env, [[maybe_unused]] napi_ref ref)
56 {
57     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
58                        << " is implemented in later versions of OHOS, please update." << std::endl;
59     return napi_ok;
60 }
61 
62 napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_is_alive_object(napi_env env,napi_ref ref,bool * result)63 napi_is_alive_object([[maybe_unused]] napi_env env, [[maybe_unused]] napi_ref ref, [[maybe_unused]] bool *result)
64 {
65     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
66                        << " is implemented in later versions of OHOS, please update." << std::endl;
67     return napi_ok;
68 }
69 
70 napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_is_contain_object(napi_env env,napi_ref ref,bool * result)71 napi_is_contain_object([[maybe_unused]] napi_env env, [[maybe_unused]] napi_ref ref, [[maybe_unused]] bool *result)
72 {
73     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
74                        << " is implemented in later versions of OHOS, please update." << std::endl;
75     return napi_ok;
76 }
77 
78 napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_is_xref_type(napi_env env,napi_value js_object,bool * result)79 napi_is_xref_type([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value js_object, [[maybe_unused]] bool *result)
80 {
81     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
82                        << " is implemented in later versions of OHOS, please update." << std::endl;
83     return napi_ok;
84 }
85 // NOLINTEND(readability-identifier-naming)
86 #endif  // PANDA_JS_ETS_HYBRID_MODE
87 
88 #ifdef PANDA_TARGET_OHOS
89 // NOLINTBEGIN(readability-identifier-naming)
90 napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_register_appstate_callback(napi_env env,void (* f)(int a1,int64_t a2))91 napi_register_appstate_callback([[maybe_unused]] napi_env env, [[maybe_unused]] void (*f)(int a1, int64_t a2))
92 {
93     return napi_ok;
94 }
95 // NOLINTEND(readability-identifier-naming)
96 #endif  // PANDA_TARGET_OHOS
97 
98 namespace ark::ets::interop::js {
99 
100 static NapiImpl gNapiImpl;
101 
Init(NapiImpl impl)102 void NapiImpl::Init(NapiImpl impl)
103 {
104     gNapiImpl = impl;
105 }
106 
107 #ifdef PANDA_TARGET_OHOS
108 
109 #include <node_api.h>
110 #include <node_api_types.h>
111 
112 // NOTE: napi_fatal_exception() is not public in libace_napi.z.so.
113 extern "C" napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_fatal_exception(napi_env env,napi_value err)114 napi_fatal_exception([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value err)
115 {
116     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__ << " will be implemented in OHOS 5.0" << std::endl;
117     return napi_ok;
118 }
119 
120 extern "C" napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_add_env_cleanup_hook(napi_env env,void (* fun)(void * arg),void * arg)121 napi_add_env_cleanup_hook([[maybe_unused]] napi_env env, [[maybe_unused]] void (*fun)(void *arg),
122                           [[maybe_unused]] void *arg)
123 {
124     // NOTE: Empty stub. In CI currently used OHOS 4.0.8, but `napi_add_env_cleanup_hook`
125     // is public since 4.1.0. Remove this method after CI upgrade.
126     INTEROP_LOG(ERROR) << "napi_add_env_cleanup_hook is implemented in OHOS since 4.1.0, please update" << std::endl;
127     return napi_ok;
128 }
129 
130 extern "C" napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_get_value_string_utf16(napi_env env,napi_value value,char16_t * buf,size_t bufsize,size_t * result)131 napi_get_value_string_utf16([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value value,
132                             [[maybe_unused]] char16_t *buf, [[maybe_unused]] size_t bufsize,
133                             [[maybe_unused]] size_t *result)
134 {
135     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
136                        << " is implemented in later versions of OHOS, please update." << std::endl;
137     return napi_ok;
138 }
139 
140 #else
141 
142 /**
143  * Since libarkruntime don't link with napi directly on host we should provide
144  * implementation for all used napi symbols.
145  */
146 
147 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
148 #define WEAK_SYMBOL(name, ...)                              \
149     extern "C" napi_status name(PARAMS_PAIR(__VA_ARGS__))   \
150     {                                                       \
151         /* CC-OFFNXT(G.PRE.05, G.PRE.09) code generation */ \
152         return gNapiImpl.name(EVERY_SECOND(__VA_ARGS__));   \
153     }
154 
155 ENUMERATE_NAPI(WEAK_SYMBOL)
156 // NOLINTEND(cppcoreguidelines-macro-usage)
157 
158 #endif
159 }  // namespace ark::ets::interop::js
160