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 "napi/native_api.h"
17 #include <complex>
18 #include <cstdio>
19 
InitArray(napi_env env, napi_callback_info info)20 static napi_value InitArray(napi_env env, napi_callback_info info)
21 {
22     int32_t array[2] = { [0] = 1 };
23     napi_value num1 = nullptr;
24     napi_create_int32(env, array[0], &num1);
25     napi_value num2 = nullptr;
26     napi_create_int32(env, array[1], &num2);
27     napi_value result = nullptr;
28     int length = 2;
29     napi_create_array_with_length(env, length, &result);
30     napi_set_element(env, result, 0, num1);
31     napi_set_element(env, result, 1, num2);
32     return result;
33 }
34 
GetBool(napi_env env, napi_callback_info info)35 static napi_value GetBool(napi_env env, napi_callback_info info)
36 {
37     bool res1 = true;
38     bool res2 = false;
39     napi_value num1 = nullptr;
40     napi_create_int32(env, res1 ? 0 : 1, &num1);
41     napi_value num2 = nullptr;
42     napi_create_int32(env, res2 ? 0 : 1, &num2);
43     napi_value result = nullptr;
44     int length = 2;
45     napi_create_array_with_length(env, length, &result);
46     napi_set_element(env, result, 0, num1);
47     napi_set_element(env, result, 1, num2);
48     return result;
49 }
50 
51 EXTERN_C_START
Init(napi_env env, napi_value exports)52 static napi_value Init(napi_env env, napi_value exports)
53 {
54     napi_property_descriptor desc[] = {
55         {"InitArray", nullptr, InitArray, nullptr, nullptr, nullptr, napi_default, nullptr},
56         {"GetBool", nullptr, GetBool, nullptr, nullptr, nullptr, napi_default, nullptr}
57     };
58     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
59     return exports;
60 }
61 EXTERN_C_END
62 
63 static napi_module demoModule = {
64     .nm_version = 1,
65     .nm_flags = 0,
66     .nm_filename = nullptr,
67     .nm_register_func = Init,
68     .nm_modname = "softwareNdk",
69     .nm_priv = ((void*)0),
70     .reserved = { 0 },
71 };
72 
RegisterEntryModule(void)73 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
74 {
75     napi_module_register(&demoModule);
76 }
77