1 /*
2 * Copyright (c) 2021 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 <string>
17 #include "napi/native_api.h"
18 #include "napi/native_node_api.h"
19
20 enum TestEnum {
21 ONE = 1,
22 TWO,
23 THREE,
24 FOUR
25 };
26
27 /*
28 * Constructor
29 */
NumberConstructor(napi_env env,napi_callback_info info)30 static napi_value NumberConstructor(napi_env env, napi_callback_info info)
31 {
32 napi_value thisArg = nullptr;
33 void* data = nullptr;
34 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
35 // Do nothing
36 return thisArg;
37 }
38
Export(napi_env env,napi_value exports)39 static napi_value Export(napi_env env, napi_value exports)
40 {
41 napi_value one = nullptr;
42 napi_value two = nullptr;
43 napi_value three = nullptr;
44 napi_value four = nullptr;
45
46 napi_create_int32(env, TestEnum::ONE, &one);
47 napi_create_int32(env, TestEnum::TWO, &two);
48 napi_create_int32(env, TestEnum::THREE, &three);
49 napi_create_int32(env, TestEnum::FOUR, &four);
50
51 napi_property_descriptor descriptors[] = {
52 DECLARE_NAPI_STATIC_PROPERTY("ONE", one),
53 DECLARE_NAPI_STATIC_PROPERTY("TWO", two),
54 DECLARE_NAPI_STATIC_PROPERTY("THREE", three),
55 DECLARE_NAPI_STATIC_PROPERTY("FOUR", four),
56 };
57
58 napi_value result = nullptr;
59 napi_define_class(env, "Number", NAPI_AUTO_LENGTH, NumberConstructor, nullptr,
60 sizeof(descriptors) / sizeof(*descriptors), descriptors, &result);
61
62 napi_set_named_property(env, exports, "Number", result);
63 return exports;
64 }
65
66 static napi_module numberModule = {
67 .nm_version = 1,
68 .nm_flags = 0,
69 .nm_filename = nullptr,
70 .nm_register_func = Export,
71 .nm_modname = "number",
72 .nm_priv = ((void*)0),
73 .reserved = { 0 },
74 };
75
NumberRegister()76 extern "C" __attribute__((constructor)) void NumberRegister()
77 {
78 napi_module_register(&numberModule);
79 }