• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 "libpandabase/macros.h"
17 #include "interop_js/napi_impl/napi_impl.h"
18 #include "interop_js/napi_impl/detail/enumerate_napi.h"
19 #include "utils/logger.h"
20 #include "interop_js/logger.h"
21 
22 #include <iostream>
23 
24 namespace ark::ets::interop::js {
25 
26 static NapiImpl gNapiImpl;
27 
Init(NapiImpl impl)28 void NapiImpl::Init(NapiImpl impl)
29 {
30     gNapiImpl = impl;
31 }
32 
33 #ifdef PANDA_TARGET_OHOS
34 
35 #include <node_api.h>
36 #include <node_api_types.h>
37 
38 // NOTE: napi_fatal_exception() is not public in libace_napi.z.so.
39 extern "C" napi_status __attribute__((weak))  // CC-OFF(G.FMT.10) project code style
napi_fatal_exception(napi_env env,napi_value err)40 napi_fatal_exception([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value err)
41 {
42     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__ << " will be implemented in OHOS 5.0" << std::endl;
43     return napi_ok;
44 }
45 
46 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)47 napi_add_env_cleanup_hook([[maybe_unused]] napi_env env, [[maybe_unused]] void (*fun)(void *arg),
48                           [[maybe_unused]] void *arg)
49 {
50     // NOTE: Empty stub. In CI currently used OHOS 4.0.8, but `napi_add_env_cleanup_hook`
51     // is public since 4.1.0. Remove this method after CI upgrade.
52     INTEROP_LOG(ERROR) << "napi_add_env_cleanup_hook is implemented in OHOS since 4.1.0, please update" << std::endl;
53     return napi_ok;
54 }
55 
56 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)57 napi_get_value_string_utf16([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value value,
58                             [[maybe_unused]] char16_t *buf, [[maybe_unused]] size_t bufsize,
59                             [[maybe_unused]] size_t *result)
60 {
61     INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
62                        << " is implemented in later versions of OHOS, please update." << std::endl;
63     return napi_ok;
64 }
65 
66 #else
67 
68 /**
69  * Since libarkruntime don't link with napi directly on host we should provide
70  * implementation for all used napi symbols.
71  */
72 
73 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
74 #define WEAK_SYMBOL(name, ...)                              \
75     extern "C" napi_status name(PARAMS_PAIR(__VA_ARGS__))   \
76     {                                                       \
77         /* CC-OFFNXT(G.PRE.05, G.PRE.09) code generation */ \
78         return gNapiImpl.name(EVERY_SECOND(__VA_ARGS__));   \
79     }
80 
81 ENUMERATE_NAPI(WEAK_SYMBOL)
82 // NOLINTEND(cppcoreguidelines-macro-usage)
83 
84 #endif
85 }  // namespace ark::ets::interop::js
86