• 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 
16 #include "n_val.h"
17 
18 #include <string>
19 
20 #include "n_error.h"
21 #include "filemgmt_libhilog.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace LibN {
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 
ToArraybuffer() const127 tuple<bool, void *, size_t> NVal::ToArraybuffer() const
128 {
129     void *buf = nullptr;
130     size_t bufLen = 0;
131     bool status = napi_get_arraybuffer_info(env_, val_, &buf, &bufLen);
132     return make_tuple(status == napi_ok, buf, bufLen);
133 }
134 
ToTypedArray() const135 tuple<bool, void *, size_t> NVal::ToTypedArray() const
136 {
137     napi_typedarray_type type;
138     napi_value in_array_buffer = nullptr;
139     size_t byte_offset;
140     size_t length;
141     void *data = nullptr;
142     napi_status status =
143         napi_get_typedarray_info(env_, val_, &type, &length, (void **)&data, &in_array_buffer, &byte_offset);
144     return make_tuple(status == napi_ok, data, length);
145 }
146 
HasProp(string propName) const147 bool NVal::HasProp(string propName) const
148 {
149     bool res = false;
150 
151     if (!env_ || !val_ || !TypeIs(napi_object))
152         return false;
153     napi_status status = napi_has_named_property(env_, val_, propName.c_str(), &res);
154     return (status == napi_ok) && res;
155 }
156 
GetProp(string propName) const157 NVal NVal::GetProp(string propName) const
158 {
159     if (!HasProp(propName)) {
160         return {env_, nullptr};
161     }
162     napi_value prop = nullptr;
163     napi_status status = napi_get_named_property(env_, val_, propName.c_str(), &prop);
164     if (status != napi_ok) {
165         return {env_, nullptr};
166     }
167     return NVal(env_, prop);
168 }
169 
AddProp(vector<napi_property_descriptor> && propVec) const170 bool NVal::AddProp(vector<napi_property_descriptor> &&propVec) const
171 {
172     if (!TypeIs(napi_valuetype::napi_object)) {
173         HILOGE("INNER BUG. Prop should only be added to objects");
174         return false;
175     }
176     napi_status status = napi_define_properties(env_, val_, propVec.size(), propVec.data());
177     if (status != napi_ok) {
178         HILOGE("INNER BUG. Cannot define properties because of %{public}d", status);
179         return false;
180     }
181     return true;
182 }
183 
AddProp(string propName,napi_value val) const184 bool NVal::AddProp(string propName, napi_value val) const
185 {
186     if (!TypeIs(napi_valuetype::napi_object) || HasProp(propName)) {
187         HILOGE("INNER BUG. Prop should only be added to objects");
188         return false;
189     }
190 
191     napi_status status = napi_set_named_property(env_, val_, propName.c_str(), val);
192     if (status != napi_ok) {
193         HILOGE("INNER BUG. Cannot set named property because of %{public}d", status);
194         return false;
195     }
196     return true;
197 }
198 
CreateUndefined(napi_env env)199 NVal NVal::CreateUndefined(napi_env env)
200 {
201     napi_value res = nullptr;
202     napi_get_undefined(env, &res);
203     return {env, res};
204 }
205 
CreateInt64(napi_env env,int64_t val)206 NVal NVal::CreateInt64(napi_env env, int64_t val)
207 {
208     napi_value res = nullptr;
209     napi_create_int64(env, val, &res);
210     return {env, res};
211 }
212 
CreateInt32(napi_env env,int32_t val)213 NVal NVal::CreateInt32(napi_env env, int32_t val)
214 {
215     napi_value res = nullptr;
216     napi_create_int32(env, val, &res);
217     return {env, res};
218 }
219 
CreateObject(napi_env env)220 NVal NVal::CreateObject(napi_env env)
221 {
222     napi_value res = nullptr;
223     napi_create_object(env, &res);
224     return {env, res};
225 }
226 
CreateBool(napi_env env,bool val)227 NVal NVal::CreateBool(napi_env env, bool val)
228 {
229     napi_value res = nullptr;
230     napi_get_boolean(env, val, &res);
231     return {env, res};
232 }
233 
CreateUTF8String(napi_env env,std::string str)234 NVal NVal::CreateUTF8String(napi_env env, std::string str)
235 {
236     napi_value res = nullptr;
237     napi_create_string_utf8(env, str.c_str(), str.length(), &res);
238     return {env, res};
239 }
240 
CreateUTF8String(napi_env env,const char * str,ssize_t len)241 NVal NVal::CreateUTF8String(napi_env env, const char *str, ssize_t len)
242 {
243     napi_value res = nullptr;
244     napi_create_string_utf8(env, str, len, &res);
245     return {env, res};
246 }
247 
CreateUint8Array(napi_env env,void * buf,size_t bufLen)248 NVal NVal::CreateUint8Array(napi_env env, void *buf, size_t bufLen)
249 {
250     napi_value output_buffer = nullptr;
251     napi_create_external_arraybuffer(
252         env, buf, bufLen, [](napi_env env, void *finalize_data, void *finalize_hint) { free(finalize_data); }, NULL,
253         &output_buffer);
254     napi_value output_array = nullptr;
255     napi_create_typedarray(env, napi_uint8_array, bufLen, output_buffer, 0, &output_array);
256     return {env, output_array};
257 }
258 
CreateArrayBuffer(napi_env env,size_t len)259 tuple<NVal, void *> NVal::CreateArrayBuffer(napi_env env, size_t len)
260 {
261     napi_value val;
262     void *buf = nullptr;
263     napi_create_arraybuffer(env, len, &buf, &val);
264     return {{env, val}, {buf}};
265 }
266 
DeclareNapiProperty(const char * name,napi_value val)267 napi_property_descriptor NVal::DeclareNapiProperty(const char *name, napi_value val)
268 {
269     return {(name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr};
270 }
271 
DeclareNapiStaticProperty(const char * name,napi_value val)272 napi_property_descriptor NVal::DeclareNapiStaticProperty(const char *name, napi_value val)
273 {
274     return {(name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr};
275 }
276 
DeclareNapiFunction(const char * name,napi_callback func)277 napi_property_descriptor NVal::DeclareNapiFunction(const char *name, napi_callback func)
278 {
279     return {(name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr};
280 }
281 
DeclareNapiStaticFunction(const char * name,napi_callback func)282 napi_property_descriptor NVal::DeclareNapiStaticFunction(const char *name, napi_callback func)
283 {
284     return {(name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr};
285 }
286 
DeclareNapiGetter(const char * name,napi_callback getter)287 napi_property_descriptor NVal::DeclareNapiGetter(const char *name, napi_callback getter)
288 {
289     return {(name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr};
290 }
291 
DeclareNapiSetter(const char * name,napi_callback setter)292 napi_property_descriptor NVal::DeclareNapiSetter(const char *name, napi_callback setter)
293 {
294     return {(name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr};
295 }
296 
DeclareNapiGetterSetter(const char * name,napi_callback getter,napi_callback setter)297 napi_property_descriptor NVal::DeclareNapiGetterSetter(const char *name, napi_callback getter, napi_callback setter)
298 {
299     return {(name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr};
300 }
301 } // namespace LibN
302 } // namespace FileManagement
303 } // namespace OHOS