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