1 /*
2 * Copyright (c) 2021-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
16 #include "n_val.h"
17
18 #include <string>
19 #include <type_traits>
20
21 #include "../log.h"
22 #include "js_native_api.h"
23
24 namespace OHOS {
25 namespace DistributedFS {
26 using namespace std;
27
NVal(napi_env nEnv,napi_value nVal=nullptr)28 NVal::NVal(napi_env nEnv, napi_value nVal = nullptr) : env_(nEnv), val_(nVal) {}
29
operator bool() const30 NVal::operator bool() const
31 {
32 return env_ && val_;
33 }
34
TypeIs(napi_valuetype expType) const35 bool NVal::TypeIs(napi_valuetype expType) const
36 {
37 if (!*this) {
38 return false;
39 }
40 napi_valuetype valueType;
41 napi_typeof(env_, val_, &valueType);
42
43 if (expType != valueType) {
44 return false;
45 }
46 return true;
47 }
48
TypeIsError(bool checkErrno) const49 bool NVal::TypeIsError(bool checkErrno) const
50 {
51 if (!*this) {
52 return false;
53 }
54
55 bool res = false;
56 napi_is_error(env_, val_, &res);
57
58 return res;
59 }
60
ToUTF8String() const61 tuple<bool, unique_ptr<char[]>, size_t> NVal::ToUTF8String() const
62 {
63 size_t strLen = 0;
64 napi_status status = napi_get_value_string_utf8(env_, val_, nullptr, -1, &strLen);
65 if (status != napi_ok) {
66 return { false, nullptr, 0 };
67 }
68
69 size_t bufLen = strLen + 1;
70 unique_ptr<char[]> str = make_unique<char[]>(bufLen);
71 status = napi_get_value_string_utf8(env_, val_, str.get(), bufLen, &strLen);
72 return make_tuple(status == napi_ok, move(str), strLen);
73 }
74
ToUTF16String() const75 tuple<bool, unique_ptr<char[]>, size_t> NVal::ToUTF16String() const
76 {
77 #ifdef FILE_SUBSYSTEM_DEBUG_LOCAL
78 size_t strLen = 0;
79 napi_status status = napi_get_value_string_utf16(env_, val_, nullptr, -1, &strLen);
80 if (status != napi_ok) {
81 return { false, nullptr, 0 };
82 }
83
84 auto str = make_unique<char16_t[]>(++strLen);
85 status = napi_get_value_string_utf16(env_, val_, str.get(), strLen, nullptr);
86 if (status != napi_ok) {
87 return { false, nullptr, 0 };
88 }
89
90 strLen = reinterpret_cast<char *>(str.get() + strLen) - reinterpret_cast<char *>(str.get());
91 auto strRet = unique_ptr<char[]>(reinterpret_cast<char *>(str.release()));
92 return { true, move(strRet), strLen };
93 #else
94 // Note that quickjs doesn't support utf16
95 return ToUTF8String();
96 #endif
97 }
98
ToPointer() const99 tuple<bool, void *> NVal::ToPointer() const
100 {
101 void *res = nullptr;
102 napi_status status = napi_get_value_external(env_, val_, &res);
103 return make_tuple(status == napi_ok, res);
104 }
105
ToBool() const106 tuple<bool, bool> NVal::ToBool() const
107 {
108 bool flag = false;
109 napi_status status = napi_get_value_bool(env_, val_, &flag);
110 return make_tuple(status == napi_ok, flag);
111 }
112
ToInt32() const113 tuple<bool, int32_t> NVal::ToInt32() const
114 {
115 int32_t res = 0;
116 napi_status status = napi_get_value_int32(env_, val_, &res);
117 return make_tuple(status == napi_ok, res);
118 }
119
ToInt64() const120 tuple<bool, int64_t> NVal::ToInt64() const
121 {
122 int64_t res = 0;
123 napi_status status = napi_get_value_int64(env_, val_, &res);
124 return make_tuple(status == napi_ok, res);
125 }
126
ToDouble() const127 tuple<bool, double> NVal::ToDouble() const
128 {
129 double res = 0;
130 napi_status status = napi_get_value_double(env_, val_, &res);
131 return make_tuple(status == napi_ok, res);
132 }
133
ToUint64() const134 tuple<bool, uint64_t, bool> NVal::ToUint64() const
135 {
136 uint64_t res = 0;
137 bool lossless = false;
138 napi_status status = napi_get_value_bigint_uint64(env_, val_, &res, &lossless);
139 return make_tuple(status == napi_ok, res, lossless);
140 }
141
ToStringArray()142 tuple<bool, vector<string>, uint32_t> NVal::ToStringArray()
143 {
144 napi_status status;
145 uint32_t size;
146 status = napi_get_array_length(env_, val_, &size);
147 vector<string> stringArray;
148 napi_value result;
149 for (uint32_t i = 0; i < size; i++) {
150 status = napi_get_element(env_, val_, i, &result);
151 auto [succ, str, ignore] = NVal(env_, result).ToUTF8String();
152 stringArray.push_back(string(str.get()));
153 }
154 return make_tuple(status == napi_ok, stringArray, size);
155 }
156
ToArraybuffer() const157 tuple<bool, void *, size_t> NVal::ToArraybuffer() const
158 {
159 void *buf = nullptr;
160 size_t bufLen = 0;
161 bool status = napi_get_arraybuffer_info(env_, val_, &buf, &bufLen);
162 return make_tuple(status == napi_ok, buf, bufLen);
163 }
164
ToTypedArray() const165 tuple<bool, void *, size_t> NVal::ToTypedArray() const
166 {
167 napi_typedarray_type type;
168 napi_value in_array_buffer = nullptr;
169 size_t byte_offset;
170 size_t length;
171 void *data = nullptr;
172 napi_status status =
173 napi_get_typedarray_info(env_, val_, &type, &length, (void **)&data, &in_array_buffer, &byte_offset);
174 return make_tuple(status == napi_ok, data, length);
175 }
176
HasProp(string propName) const177 bool NVal::HasProp(string propName) const
178 {
179 bool res = false;
180
181 if (!env_ || !val_ || !TypeIs(napi_object))
182 return false;
183 napi_status status = napi_has_named_property(env_, val_, propName.c_str(), &res);
184 return (status == napi_ok) && res;
185 }
186
GetProp(string propName) const187 NVal NVal::GetProp(string propName) const
188 {
189 if (!HasProp(propName)) {
190 return { env_, nullptr };
191 }
192 napi_value prop = nullptr;
193 napi_status status = napi_get_named_property(env_, val_, propName.c_str(), &prop);
194 if (status != napi_ok) {
195 return { env_, nullptr };
196 }
197 return NVal(env_, prop);
198 }
199
AddProp(vector<napi_property_descriptor> && propVec) const200 bool NVal::AddProp(vector<napi_property_descriptor> &&propVec) const
201 {
202 if (!TypeIs(napi_valuetype::napi_object)) {
203 HILOGE("INNER BUG. Prop should only be added to objects");
204 return false;
205 }
206 napi_status status = napi_define_properties(env_, val_, propVec.size(), propVec.data());
207 if (status != napi_ok) {
208 HILOGE("INNER BUG. Cannot define properties because of %{public}d", status);
209 return false;
210 }
211 return true;
212 }
213
AddProp(string propName,napi_value val) const214 bool NVal::AddProp(string propName, napi_value val) const
215 {
216 if (!TypeIs(napi_valuetype::napi_object) || HasProp(propName)) {
217 HILOGE("INNER BUG. Prop should only be added to objects");
218 return false;
219 }
220
221 napi_status status = napi_set_named_property(env_, val_, propName.c_str(), val);
222 if (status != napi_ok) {
223 HILOGE("INNER BUG. Cannot set named property because of %{public}d", status);
224 return false;
225 }
226 return true;
227 }
228
CreateUndefined(napi_env env)229 NVal NVal::CreateUndefined(napi_env env)
230 {
231 napi_value res = nullptr;
232 napi_get_undefined(env, &res);
233 return { env, res };
234 }
235
CreateInt64(napi_env env,int64_t val)236 NVal NVal::CreateInt64(napi_env env, int64_t val)
237 {
238 napi_value res = nullptr;
239 napi_create_int64(env, val, &res);
240 return { env, res };
241 }
242
CreateInt32(napi_env env,int32_t val)243 NVal NVal::CreateInt32(napi_env env, int32_t val)
244 {
245 napi_value res = nullptr;
246 napi_create_int32(env, val, &res);
247 return { env, res };
248 }
249
CreateObject(napi_env env)250 NVal NVal::CreateObject(napi_env env)
251 {
252 napi_value res = nullptr;
253 napi_create_object(env, &res);
254 return { env, res };
255 }
256
CreateBool(napi_env env,bool val)257 NVal NVal::CreateBool(napi_env env, bool val)
258 {
259 napi_value res = nullptr;
260 napi_get_boolean(env, val, &res);
261 return { env, res };
262 }
263
CreateUTF8String(napi_env env,std::string str)264 NVal NVal::CreateUTF8String(napi_env env, std::string str)
265 {
266 napi_value res = nullptr;
267 napi_create_string_utf8(env, str.c_str(), str.length(), &res);
268 return { env, res };
269 }
270
CreateUTF8String(napi_env env,const char * str,ssize_t len)271 NVal NVal::CreateUTF8String(napi_env env, const char* str, ssize_t len)
272 {
273 napi_value res = nullptr;
274 napi_create_string_utf8(env, str, len, &res);
275 return { env, res };
276 }
277
CreateUint8Array(napi_env env,void * buf,size_t bufLen)278 NVal NVal::CreateUint8Array(napi_env env, void *buf, size_t bufLen)
279 {
280 napi_value output_buffer = nullptr;
281 napi_create_external_arraybuffer(
282 env,
283 buf,
284 bufLen,
285 [](napi_env env, void *finalize_data, void *finalize_hint) { free(finalize_data); },
286 NULL,
287 &output_buffer);
288 napi_value output_array = nullptr;
289 napi_create_typedarray(env, napi_uint8_array, bufLen, output_buffer, 0, &output_array);
290 return { env, output_array };
291 }
292
CreateArrayString(napi_env env,vector<string> strs)293 NVal NVal::CreateArrayString(napi_env env, vector<string> strs)
294 {
295 napi_value res = nullptr;
296 napi_create_array(env, &res);
297 for (size_t i = 0; i < strs.size(); i++) {
298 napi_value filename;
299 napi_create_string_utf8(env, strs[i].c_str(), strs[i].length(), &filename);
300 napi_set_element(env, res, i, filename);
301 }
302 return {env, res};
303 }
304
CreateArrayBuffer(napi_env env,size_t len)305 tuple<NVal, void *> NVal::CreateArrayBuffer(napi_env env, size_t len)
306 {
307 napi_value val;
308 void *buf = nullptr;
309 napi_create_arraybuffer(env, len, &buf, &val);
310 return { { env, val }, { buf } };
311 }
312
DeclareNapiProperty(const char * name,napi_value val)313 napi_property_descriptor NVal::DeclareNapiProperty(const char *name, napi_value val)
314 {
315 return { (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr };
316 }
317
DeclareNapiStaticProperty(const char * name,napi_value val)318 napi_property_descriptor NVal::DeclareNapiStaticProperty(const char *name, napi_value val)
319 {
320 return { (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr };
321 }
322
DeclareNapiFunction(const char * name,napi_callback func)323 napi_property_descriptor NVal::DeclareNapiFunction(const char *name, napi_callback func)
324 {
325 return { (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr };
326 }
327
DeclareNapiStaticFunction(const char * name,napi_callback func)328 napi_property_descriptor NVal::DeclareNapiStaticFunction(const char *name, napi_callback func)
329 {
330 return { (name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr };
331 }
332
DeclareNapiGetter(const char * name,napi_callback getter)333 napi_property_descriptor NVal::DeclareNapiGetter(const char *name, napi_callback getter)
334 {
335 return { (name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr };
336 }
337
DeclareNapiSetter(const char * name,napi_callback setter)338 napi_property_descriptor NVal::DeclareNapiSetter(const char *name, napi_callback setter)
339 {
340 return { (name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr };
341 }
342
DeclareNapiGetterSetter(const char * name,napi_callback getter,napi_callback setter)343 napi_property_descriptor NVal::DeclareNapiGetterSetter(const char *name, napi_callback getter, napi_callback setter)
344 {
345 return { (name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr };
346 }
347 } // namespace DistributedFS
348 } // namespace OHOS