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