1 /**
2 * Copyright (c) 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 <array>
17 #include <iostream>
18 #include <node_api.h>
19 #include "interop_timer_helper.h"
20 #include "timer.h"
21
22 // NOLINTBEGIN(readability-identifier-naming, readability-redundant-declaration)
23 // CC-OFFNXT(G.FMT.10-CPP) project code style
24 napi_status __attribute__((weak)) napi_define_properties(napi_env env, napi_value object, size_t property_count,
25 const napi_property_descriptor *properties);
26 // NOLINTEND(readability-identifier-naming, readability-redundant-declaration)
27
28 namespace ark::ets::interop::js::helper {
29
30 static constexpr const char *MODULE_PREFIX = "[INTEROP_TIMER_HELPER] ";
31
SetTimeout(napi_env env,napi_callback_info info)32 static napi_value SetTimeout(napi_env env, napi_callback_info info)
33 {
34 return SetTimeoutImpl(env, info, false);
35 }
36
SetInterval(napi_env env,napi_callback_info info)37 static napi_value SetInterval(napi_env env, napi_callback_info info)
38 {
39 return SetTimeoutImpl(env, info, true);
40 }
41
ClearTimer(napi_env env,napi_callback_info info)42 static napi_value ClearTimer(napi_env env, napi_callback_info info)
43 {
44 return ClearTimerImpl(env, info);
45 }
46
Init(napi_env env,napi_value exports)47 napi_value Init(napi_env env, napi_value exports)
48 {
49 const std::array desc = {
50 napi_property_descriptor {"setTimeout", 0, SetTimeout, 0, 0, 0, napi_enumerable, 0},
51 napi_property_descriptor {"setInterval", 0, SetInterval, 0, 0, 0, napi_enumerable, 0},
52 napi_property_descriptor {"clearTimeout", 0, ClearTimer, 0, 0, 0, napi_enumerable, 0},
53 napi_property_descriptor {"clearInterval", 0, ClearTimer, 0, 0, 0, napi_enumerable, 0},
54 };
55
56 if (napi_define_properties(env, exports, desc.size(), desc.data()) != napi_ok) {
57 std::cerr << MODULE_PREFIX << "Failed to define properties" << std::endl;
58 std::abort(); // CC-OFF(G.STD.16-CPP) fatal error
59 }
60
61 return exports;
62 }
63
64 } // namespace ark::ets::interop::js::helper
65