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 "js_util.h"
16
17 #include <securec.h>
18
19 #include "logger.h"
20
21 namespace OHOS::ObjectStore {
22 constexpr int32_t STR_MAX_LENGTH = 4096;
23 constexpr size_t STR_TAIL_LENGTH = 1;
24
25 /* napi_value <-> bool */
GetValue(napi_env env,napi_value in,bool & out)26 napi_status JSUtil::GetValue(napi_env env, napi_value in, bool &out)
27 {
28 LOG_DEBUG("napi_value <- bool");
29 return napi_get_value_bool(env, in, &out);
30 }
31
SetValue(napi_env env,const bool & in,napi_value & out)32 napi_status JSUtil::SetValue(napi_env env, const bool &in, napi_value &out)
33 {
34 LOG_DEBUG("napi_value -> bool");
35 return napi_get_boolean(env, in, &out);
36 }
37
38 /* napi_value <-> double */
GetValue(napi_env env,napi_value in,double & out)39 napi_status JSUtil::GetValue(napi_env env, napi_value in, double &out)
40 {
41 LOG_DEBUG("napi_value -> double");
42 return napi_get_value_double(env, in, &out);
43 }
44
SetValue(napi_env env,const double & in,napi_value & out)45 napi_status JSUtil::SetValue(napi_env env, const double &in, napi_value &out)
46 {
47 LOG_DEBUG("napi_value <- double");
48 return napi_create_double(env, in, &out);
49 }
50
51 /* napi_value <-> int64_t */
GetValue(napi_env env,napi_value in,int64_t & out)52 napi_status JSUtil::GetValue(napi_env env, napi_value in, int64_t& out)
53 {
54 return napi_get_value_int64(env, in, &out);
55 }
56
SetValue(napi_env env,const int64_t & in,napi_value & out)57 napi_status JSUtil::SetValue(napi_env env, const int64_t& in, napi_value& out)
58 {
59 return napi_create_int64(env, in, &out);
60 }
61
62 /* napi_value <-> uint32_t */
GetValue(napi_env env,napi_value in,uint32_t & out)63 napi_status JSUtil::GetValue(napi_env env, napi_value in, uint32_t& out)
64 {
65 return napi_get_value_uint32(env, in, &out);
66 }
67
SetValue(napi_env env,const uint32_t & in,napi_value & out)68 napi_status JSUtil::SetValue(napi_env env, const uint32_t& in, napi_value& out)
69 {
70 return napi_create_uint32(env, in, &out);
71 }
72
73 /* napi_value <-> std::string */
GetValue(napi_env env,napi_value in,std::string & out)74 napi_status JSUtil::GetValue(napi_env env, napi_value in, std::string &out)
75 {
76 size_t maxLen = STR_MAX_LENGTH;
77 napi_status status = napi_get_value_string_utf8(env, in, NULL, 0, &maxLen);
78 if (status != napi_ok || maxLen <= 0) {
79 return napi_generic_failure;
80 }
81 char *buf = new (std::nothrow) char[maxLen + STR_TAIL_LENGTH];
82 if (buf != nullptr) {
83 size_t len = 0;
84 status = napi_get_value_string_utf8(env, in, buf, maxLen + STR_TAIL_LENGTH, &len);
85 if (status == napi_ok) {
86 out = std::string(buf);
87 }
88 delete[] buf;
89 } else {
90 status = napi_generic_failure;
91 }
92 return status;
93 }
94
SetValue(napi_env env,const std::string & in,napi_value & out)95 napi_status JSUtil::SetValue(napi_env env, const std::string &in, napi_value &out)
96 {
97 LOG_DEBUG("napi_value <- std::string %{public}d", (int)in.length());
98 return napi_create_string_utf8(env, in.c_str(), in.size(), &out);
99 }
100
101 /* napi_value <-> std::vector<std::string> */
GetValue(napi_env env,napi_value in,std::vector<std::string> & out)102 napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector<std::string> &out)
103 {
104 LOG_DEBUG("napi_value -> std::vector<std::string>");
105 bool isArray = false;
106 napi_is_array(env, in, &isArray);
107 LOG_ERROR_RETURN(isArray, "not an array", napi_invalid_arg);
108
109 uint32_t length = 0;
110 napi_status status = napi_get_array_length(env, in, &length);
111 LOG_ERROR_RETURN((status == napi_ok) && (length > 0), "get_array failed!", napi_invalid_arg);
112 for (uint32_t i = 0; i < length; ++i) {
113 napi_value item = nullptr;
114 status = napi_get_element(env, in, i, &item);
115 LOG_ERROR_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg);
116 std::string value;
117 status = GetValue(env, item, value);
118 LOG_ERROR_RETURN(status == napi_ok, "not a string", napi_invalid_arg);
119 out.push_back(value);
120 }
121 return status;
122 }
123
SetValue(napi_env env,const std::vector<std::string> & in,napi_value & out)124 napi_status JSUtil::SetValue(napi_env env, const std::vector<std::string> &in, napi_value &out)
125 {
126 LOG_DEBUG("napi_value <- std::vector<std::string>");
127 napi_status status = napi_create_array_with_length(env, in.size(), &out);
128 LOG_ERROR_RETURN(status == napi_ok, "create array failed!", status);
129 int index = 0;
130 for (auto &item : in) {
131 napi_value element = nullptr;
132 SetValue(env, item, element);
133 status = napi_set_element(env, out, index++, element);
134 LOG_ERROR_RETURN((status == napi_ok), "napi_set_element failed!", status);
135 }
136 return status;
137 }
138
139 /* napi_value <-> std::vector<uint8_t> */
GetValue(napi_env env,napi_value in,std::vector<uint8_t> & out)140 napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector<uint8_t> &out)
141 {
142 out.clear();
143 LOG_DEBUG("napi_value -> std::vector<uint8_t> ");
144 napi_typedarray_type type = napi_biguint64_array;
145 size_t length = 0;
146 napi_value buffer = nullptr;
147 size_t offset = 0;
148 void *data = nullptr;
149 napi_status status = napi_get_typedarray_info(env, in, &type, &length, &data, &buffer, &offset);
150 LOG_DEBUG("array type=%{public}d length=%{public}d offset=%{public}d status=%{public}d", (int)type, (int)length,
151 (int)offset, status);
152 LOG_ERROR_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
153 LOG_ERROR_RETURN(type == napi_uint8_array, "is not Uint8Array!", napi_invalid_arg);
154 LOG_ERROR_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
155 out.assign(static_cast<uint8_t *>(data), static_cast<uint8_t *>(data) + length);
156 return status;
157 }
158
SetValue(napi_env env,const std::vector<uint8_t> & in,napi_value & out)159 napi_status JSUtil::SetValue(napi_env env, const std::vector<uint8_t> &in, napi_value &out)
160 {
161 LOG_DEBUG("napi_value <- std::vector<uint8_t> ");
162 LOG_ERROR_RETURN(in.size() > 0, "invalid std::vector<uint8_t>", napi_invalid_arg);
163 void *data = nullptr;
164 napi_value buffer = nullptr;
165 napi_status status = napi_create_arraybuffer(env, in.size(), &data, &buffer);
166 LOG_ERROR_RETURN((status == napi_ok), "create array buffer failed!", status);
167
168 if (memcpy_s(data, in.size(), in.data(), in.size()) != EOK) {
169 LOG_ERROR("memcpy_s not EOK");
170 return napi_invalid_arg;
171 }
172 status = napi_create_typedarray(env, napi_uint8_array, in.size(), buffer, 0, &out);
173 LOG_ERROR_RETURN((status == napi_ok), "napi_value <- std::vector<uint8_t> invalid value", status);
174 return status;
175 }
176
GetValue(napi_env env,napi_value in,AssetBindInfo & bindInfo)177 napi_status JSUtil::GetValue(napi_env env, napi_value in, AssetBindInfo &bindInfo)
178 {
179 napi_status status = napi_invalid_arg;
180 status = GetNamedProperty(env, in, "storeName", bindInfo.storeName);
181 LOG_ERROR_RETURN(status == napi_ok, "get store param failed", status);
182 status = GetNamedProperty(env, in, "tableName", bindInfo.tableName);
183 LOG_ERROR_RETURN(status == napi_ok, "get table param failed", status);
184 status = GetNamedProperty(env, in, "primaryKey", bindInfo.primaryKey);
185
186 LOG_ERROR_RETURN(status == napi_ok, "get primary param failed", status);
187 status = GetNamedProperty(env, in, "field", bindInfo.field);
188 LOG_ERROR_RETURN(status == napi_ok, "get field param failed", status);
189 status = GetNamedProperty(env, in, "assetName", bindInfo.assetName);
190 LOG_ERROR_RETURN(status == napi_ok, "get assetName param failed", status);
191 return status;
192 }
193
GetValue(napi_env env,napi_value in,Asset & asset)194 napi_status JSUtil::GetValue(napi_env env, napi_value in, Asset &asset)
195 {
196 napi_valuetype type;
197 napi_status status = napi_typeof(env, in, &type);
198 LOG_ERROR_RETURN((status == napi_ok) && (type == napi_object), "Asset type invalid", napi_invalid_arg);
199 status = GetNamedProperty(env, in, "name", asset.name);
200 LOG_ERROR_RETURN(status == napi_ok, "get name param failed", status);
201 status = GetNamedProperty(env, in, "uri", asset.uri);
202 LOG_ERROR_RETURN(status == napi_ok, "get uri param failed", status);
203 status = GetNamedProperty(env, in, "path", asset.path);
204 LOG_ERROR_RETURN(status == napi_ok, "get path param failed", status);
205 status = GetNamedProperty(env, in, "createTime", asset.createTime);
206 LOG_ERROR_RETURN(status == napi_ok, "get createTime param failed", status);
207 status = GetNamedProperty(env, in, "modifyTime", asset.modifyTime);
208 LOG_ERROR_RETURN(status == napi_ok, "get modifyTime param failed", status);
209 status = GetNamedProperty(env, in, "size", asset.size);
210 LOG_ERROR_RETURN(status == napi_ok, "get size param failed", status);
211 status = GetNamedProperty(env, in, "status", asset.status, true);
212 LOG_ERROR_RETURN(status == napi_ok, "get status param failed", status);
213 return status;
214 }
215
GetValue(napi_env env,napi_value in,Assets & assets)216 napi_status JSUtil::GetValue(napi_env env, napi_value in, Assets &assets)
217 {
218 assets.clear();
219 bool isArray = false;
220 napi_is_array(env, in, &isArray);
221 LOG_ERROR_RETURN(isArray, "not an array", napi_generic_failure);
222
223 uint32_t arrLen = 0;
224 napi_status status = napi_get_array_length(env, in, &arrLen);
225 LOG_ERROR_RETURN((status == napi_ok) && (arrLen > 0), "get_array failed!", status);
226 for (uint32_t i = 0; i < arrLen; ++i) {
227 napi_value item = nullptr;
228 status = napi_get_element(env, in, i, &item);
229 LOG_ERROR_RETURN((item != nullptr) && (status == napi_ok), "no element", status);
230 Asset asset;
231 status = GetValue(env, item, asset);
232 LOG_ERROR_RETURN(status == napi_ok, "not a asset object", status);
233 assets.push_back(asset);
234 }
235 return status;
236 }
237
GetValue(napi_env env,napi_value in,ValuesBucket & out)238 napi_status JSUtil::GetValue(napi_env env, napi_value in, ValuesBucket &out)
239 {
240 out.clear();
241 napi_value values = nullptr;
242 uint32_t count = 0;
243 napi_get_all_property_names(env, in, napi_key_own_only,
244 static_cast<napi_key_filter>(napi_key_enumerable | napi_key_skip_symbols),
245 napi_key_numbers_to_strings, &values);
246 napi_status status = napi_get_array_length(env, values, &count);
247 LOG_ERROR_RETURN(status == napi_ok && count > 0, "get ValuesBucket failed", napi_invalid_arg);
248 napi_value key = nullptr;
249 napi_value val = nullptr;
250 for (uint32_t index = 0; index < count; index++) {
251 status = napi_get_element(env, values, index, &key);
252 LOG_ERROR_RETURN(status == napi_ok && key != nullptr, "no element", napi_invalid_arg);
253 std::string keyStr;
254 status = GetValue(env, key, keyStr);
255 LOG_ERROR_RETURN(status == napi_ok, "get key failed", status);
256 status = napi_get_property(env, in, key, &val);
257 LOG_ERROR_RETURN(status == napi_ok && val != nullptr, "no element", napi_invalid_arg);
258 ValueObject value;
259 status = GetValue(env, val, value);
260 if (status == napi_ok) {
261 out.insert(std::pair<std::string, ValueObject>(keyStr, value));
262 } else if (status != napi_generic_failure) {
263 LOG_ERROR("The value type %{public}s is invalid: ", keyStr.c_str());
264 return status;
265 }
266 }
267 return napi_ok;
268 }
269
GetValue(napi_env env,napi_value jsValue,std::monostate & out)270 napi_status JSUtil::GetValue(napi_env env, napi_value jsValue, std::monostate &out)
271 {
272 napi_value tempValue;
273 napi_get_null(env, &tempValue);
274 bool equal = false;
275 napi_strict_equals(env, jsValue, tempValue, &equal);
276 if (equal) {
277 out = std::monostate();
278 return napi_ok;
279 }
280 LOG_DEBUG("jsValue is not null");
281 return napi_invalid_arg;
282 }
283
IsNull(napi_env env,napi_value value)284 bool JSUtil::IsNull(napi_env env, napi_value value)
285 {
286 napi_valuetype type = napi_undefined;
287 napi_status status = napi_typeof(env, value, &type);
288 if (status == napi_ok && (type == napi_undefined || type == napi_null)) {
289 return true;
290 }
291 return false;
292 }
293 } // namespace OHOS::ObjectStore
294