1 /*
2 * Copyright (c) 2022 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 #include "napi/native_common.h"
17
18 namespace OHOS {
19 namespace AAFwk {
SetEnumItem(napi_env env,napi_value object,const char * name,int32_t value)20 static napi_status SetEnumItem(napi_env env, napi_value object, const char* name, int32_t value)
21 {
22 napi_status status;
23 napi_value itemName;
24 napi_value itemValue;
25
26 NAPI_CALL_BASE(env, status = napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &itemName), status);
27 NAPI_CALL_BASE(env, status = napi_create_int32(env, value, &itemValue), status);
28
29 NAPI_CALL_BASE(env, status = napi_set_property(env, object, itemName, itemValue), status);
30 NAPI_CALL_BASE(env, status = napi_set_property(env, object, itemValue, itemName), status);
31
32 return napi_ok;
33 }
34
InitAreaModeObject(napi_env env)35 static napi_value InitAreaModeObject(napi_env env)
36 {
37 napi_value object;
38 NAPI_CALL(env, napi_create_object(env, &object));
39
40 NAPI_CALL(env, SetEnumItem(env, object, "EL1", 0));
41 NAPI_CALL(env, SetEnumItem(env, object, "EL2", 1));
42
43 return object;
44 }
45
46 /*
47 * The module initialization.
48 */
ApplicationContextConstantInit(napi_env env,napi_value exports)49 static napi_value ApplicationContextConstantInit(napi_env env, napi_value exports)
50 {
51 napi_value areaMode = InitAreaModeObject(env);
52 NAPI_ASSERT(env, areaMode != nullptr, "failed to create AreaMode object");
53
54 napi_property_descriptor exportObjs[] = {
55 DECLARE_NAPI_PROPERTY("AreaMode", areaMode),
56 };
57
58 napi_status status = napi_define_properties(env, exports, sizeof(exportObjs) / sizeof(exportObjs[0]), exportObjs);
59 NAPI_ASSERT(env, status == napi_ok, "failed to define properties for exports");
60
61 return exports;
62 }
63
64 /*
65 * The module definition.
66 */
67 static napi_module _module = {
68 .nm_version = 1,
69 .nm_flags = 0,
70 .nm_filename = nullptr,
71 .nm_register_func = ApplicationContextConstantInit,
72 .nm_modname = "app.ability.contextConstant",
73 .nm_priv = (static_cast<void *>(0)),
74 .reserved = {0}
75 };
76
77 /*
78 * The module registration.
79 */
RegisterModule(void)80 extern "C" __attribute__((constructor)) void RegisterModule(void)
81 {
82 napi_module_register(&_module);
83 }
84 } // namespace AAFwk
85 } // namespace OHOS
86