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 "js_util.h"
16
17 #include <functional>
18 #include <memory>
19 #include <string>
20 #include <vector>
21
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24 #include "napi/native_node_api.h"
25 namespace OHOS::MiscServices {
26 constexpr size_t MAX_ARGC = 2;
27 struct Property {
28 std::string name;
29 std::string id;
30 std::string icon;
31 uint32_t iconId = 0;
32 };
33 struct JsProperty {
WriteOHOS::MiscServices::JsProperty34 static napi_value Write(napi_env env, const std::shared_ptr<Property> &nativeObject)
35 {
36 if (nativeObject == nullptr) {
37 return JsUtil::Const::Null(env);
38 }
39 return Write(env, *nativeObject);
40 }
WriteOHOS::MiscServices::JsProperty41 static napi_value Write(napi_env env, const Property &nativeObject)
42 {
43 napi_value object = nullptr;
44 napi_create_object(env, &object);
45 auto ret = JsUtil::Object::WriteProperty(env, object, "name", nativeObject.name);
46 ret = ret && JsUtil::Object::WriteProperty(env, object, "id", nativeObject.id);
47 ret = ret && JsUtil::Object::WriteProperty(env, object, "icon", nativeObject.icon);
48 ret = ret && JsUtil::Object::WriteProperty(env, object, "iconId", nativeObject.iconId);
49 return ret ? object : JsUtil::Const::Null(env);
50 }
ReadOHOS::MiscServices::JsProperty51 static bool Read(napi_env env, napi_value object, Property &nativeObject)
52 {
53 auto ret = JsUtil::Object::ReadProperty(env, object, "name", nativeObject.name);
54 ret = ret && JsUtil::Object::ReadProperty(env, object, "id", nativeObject.id);
55 ret = ret && JsUtil::Object::ReadProperty(env, object, "icon", nativeObject.icon);
56 ret = ret && JsUtil::Object::ReadProperty(env, object, "iconId", nativeObject.iconId);
57 return ret;
58 }
59 };
60
61 using Action = std::function<napi_value(napi_value in)>;
Handle(napi_env env,napi_callback_info info,const Action & action)62 static napi_value Handle(napi_env env, napi_callback_info info, const Action &action)
63 {
64 size_t argc = MAX_ARGC;
65 napi_value args[MAX_ARGC];
66 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
67 return action(args[0]);
68 }
GetInt32(napi_env env,napi_callback_info info)69 static napi_value GetInt32(napi_env env, napi_callback_info info)
70 {
71 return Handle(env, info, [env](napi_value in) -> napi_value {
72 int32_t test = 0;
73 JsUtil::GetValue(env, in, test);
74 return JsUtil::GetValue(env, test);
75 });
76 }
GetInt64(napi_env env,napi_callback_info info)77 static napi_value GetInt64(napi_env env, napi_callback_info info)
78 {
79 return Handle(env, info, [env](napi_value in) -> napi_value {
80 int64_t test = 0;
81 JsUtil::GetValue(env, in, test);
82 return JsUtil::GetValue(env, test);
83 });
84 }
GetUint32(napi_env env,napi_callback_info info)85 static napi_value GetUint32(napi_env env, napi_callback_info info)
86 {
87 return Handle(env, info, [env](napi_value in) -> napi_value {
88 uint32_t test = 0;
89 JsUtil::GetValue(env, in, test);
90 return JsUtil::GetValue(env, test);
91 });
92 }
GetBool(napi_env env,napi_callback_info info)93 static napi_value GetBool(napi_env env, napi_callback_info info)
94 {
95 return Handle(env, info, [env](napi_value in) -> napi_value {
96 bool test = false;
97 JsUtil::GetValue(env, in, test);
98 return JsUtil::GetValue(env, test);
99 });
100 }
GetString(napi_env env,napi_callback_info info)101 static napi_value GetString(napi_env env, napi_callback_info info)
102 {
103 return Handle(env, info, [env](napi_value in) -> napi_value {
104 std::string test;
105 JsUtil::GetValue(env, in, test);
106 return JsUtil::GetValue(env, test);
107 });
108 }
GetObject(napi_env env,napi_callback_info info)109 static napi_value GetObject(napi_env env, napi_callback_info info)
110 {
111 return Handle(env, info, [env](napi_value in) -> napi_value {
112 Property prop{};
113 JsProperty::Read(env, in, prop);
114 return JsProperty::Write(env, prop);
115 });
116 }
GetArrayString(napi_env env,napi_callback_info info)117 static napi_value GetArrayString(napi_env env, napi_callback_info info)
118 {
119 return Handle(env, info, [env](napi_value in) -> napi_value {
120 std::vector<std::string> test;
121 JsUtil::GetValue(env, in, test);
122 return JsUtil::GetValue(env, test);
123 });
124 }
GetArrayInt32(napi_env env,napi_callback_info info)125 static napi_value GetArrayInt32(napi_env env, napi_callback_info info)
126 {
127 return Handle(env, info, [env](napi_value in) -> napi_value {
128 std::vector<std::int32_t> test;
129 JsUtil::GetValue(env, in, test);
130 return JsUtil::GetValue(env, test);
131 });
132 }
GetArrayBool(napi_env env,napi_callback_info info)133 static napi_value GetArrayBool(napi_env env, napi_callback_info info)
134 {
135 return Handle(env, info, [env](napi_value in) -> napi_value {
136 std::vector<bool> test;
137 JsUtil::GetValue(env, in, test);
138 return JsUtil::GetValue(env, test);
139 });
140 }
GetNull(napi_env env,napi_callback_info info)141 static napi_value GetNull(napi_env env, napi_callback_info info)
142 {
143 return JsUtil::Const::Null(env);
144 }
GetUndefined(napi_env env,napi_callback_info info)145 static napi_value GetUndefined(napi_env env, napi_callback_info info)
146 {
147 return JsUtil::Const::Undefined(env);
148 }
149
150 EXTERN_C_START
Init(napi_env env,napi_value exports)151 static napi_value Init(napi_env env, napi_value exports)
152 {
153 napi_property_descriptor properties[] = {
154 DECLARE_NAPI_FUNCTION("getInt32", GetInt32),
155 DECLARE_NAPI_FUNCTION("getUint32", GetUint32),
156 DECLARE_NAPI_FUNCTION("getInt64", GetInt64),
157 DECLARE_NAPI_FUNCTION("getBool", GetBool),
158 DECLARE_NAPI_FUNCTION("getString", GetString),
159 DECLARE_NAPI_FUNCTION("getObject", GetObject),
160 DECLARE_NAPI_FUNCTION("getArrayString", GetArrayString),
161 DECLARE_NAPI_FUNCTION("getArrayInt32", GetArrayInt32),
162 DECLARE_NAPI_FUNCTION("getArrayBool", GetArrayBool),
163 DECLARE_NAPI_FUNCTION("getNull", GetNull),
164 DECLARE_NAPI_FUNCTION("getUndefined", GetUndefined),
165 };
166 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
167
168 return exports;
169 }
170 EXTERN_C_END
171
172 /*
173 * module define
174 */
175 static napi_module _module = { .nm_version = 1,
176 .nm_flags = 0,
177 .nm_filename = nullptr,
178 .nm_register_func = Init,
179 .nm_modname = "ohos_js_util",
180 .nm_priv = ((void *)0),
181 .reserved = { 0 } };
182 /*
183 * module register
184 */
Register()185 extern "C" __attribute__((constructor)) void Register()
186 {
187 napi_module_register(&_module);
188 }
189 } // namespace OHOS::MiscServices