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))
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(ERROR) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__ << " will be implemented in OHOS 5.0" << std::endl;
43 std::abort();
44 return napi_ok;
45 }
46
47 extern "C" napi_status __attribute__((weak))
napi_add_env_cleanup_hook(napi_env env,void (* fun)(void * arg),void * arg)48 napi_add_env_cleanup_hook([[maybe_unused]] napi_env env, [[maybe_unused]] void (*fun)(void *arg),
49 [[maybe_unused]] void *arg)
50 {
51 // NOTE: Empty stub. In CI currently used OHOS 4.0.8, but `napi_add_env_cleanup_hook`
52 // is public since 4.1.0. Remove this method after CI upgrade.
53 INTEROP_LOG(ERROR) << "napi_add_env_cleanup_hook is implemented in OHOS since 4.1.0, please update" << std::endl;
54 return napi_ok;
55 }
56
57 extern "C" napi_status __attribute__((weak))
napi_get_value_string_utf16(napi_env env,napi_value value,char16_t * buf,size_t bufsize,size_t * result)58 napi_get_value_string_utf16([[maybe_unused]] napi_env env, [[maybe_unused]] napi_value value,
59 [[maybe_unused]] char16_t *buf, [[maybe_unused]] size_t bufsize,
60 [[maybe_unused]] size_t *result)
61 {
62 INTEROP_LOG(ERROR) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
63 << " is implemented in later versions of OHOS, please update." << std::endl;
64 std::abort();
65 }
66
67 #else
68
69 /**
70 * Since libarkruntime don't link with napi directly on host we should provide
71 * implementation for all used napi symbols.
72 */
73
74 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
75 #define WEAK_SYMBOL(name, ...) \
76 extern "C" napi_status name(PARAMS_PAIR(__VA_ARGS__)) \
77 { \
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