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