1 /**
2 * Copyright (c) 2023 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 #include "napi/native_api.h"
16 
Add(napi_env env, napi_callback_info info)17 static napi_value Add(napi_env env, napi_callback_info info)
18 {
19     size_t requireArgc = 2;
20     size_t argc = 2;
21     napi_value args[2] = {nullptr};
22 
23     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
24 
25     napi_valuetype valuetype0;
26     napi_typeof(env, args[0], &valuetype0);
27 
28     napi_valuetype valuetype1;
29     napi_typeof(env, args[1], &valuetype1);
30 
31     double value0;
32     napi_get_value_double(env, args[0], &value0);
33 
34     double value1;
35     napi_get_value_double(env, args[1], &value1);
36 
37     napi_value sum;
38     napi_create_double(env, value0 + value1, &sum);
39 
40     return sum;
41 }
42 
43 EXTERN_C_START
Init(napi_env env, napi_value exports)44 static napi_value Init(napi_env env, napi_value exports)
45 {
46     napi_property_descriptor desc[] = {
47         { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr }
48     };
49     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
50     return exports;
51 }
52 EXTERN_C_END
53 
54 static napi_module demoModule = {
55     .nm_version =1,
56     .nm_flags = 0,
57     .nm_filename = nullptr,
58     .nm_register_func = Init,
59     .nm_modname = "entry",
60     .nm_priv = ((void*)0),
61     .reserved = { 0 },
62 };
63 
RegisterEntryModule(void)64 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
65 {
66     napi_module_register(&demoModule);
67 }
68