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 "napi_helper.h"
17
18 #include "native_engine/native_value.h"
19
20 namespace Commonlibrary::Concurrent::Common::Helper {
21 static constexpr uint32_t MAX_CHAR_LENGTH = 1024;
22
IsString(napi_value value)23 bool NapiHelper::IsString(napi_value value)
24 {
25 auto valNative = reinterpret_cast<NativeValue*>(value);
26 return valNative == nullptr ? false : valNative->TypeOf() == NATIVE_STRING;
27 }
28
IsNotUndefined(napi_value value)29 bool NapiHelper::IsNotUndefined(napi_value value)
30 {
31 auto valNative = reinterpret_cast<NativeValue*>(value);
32 return valNative == nullptr ? false : valNative->TypeOf() != NATIVE_UNDEFINED;
33 }
34
IsArray(napi_value value)35 bool NapiHelper::IsArray(napi_value value)
36 {
37 auto valNative = reinterpret_cast<NativeValue*>(value);
38 return valNative == nullptr ? false : valNative->IsArray();
39 }
40
IsFunction(napi_value object)41 bool NapiHelper::IsFunction(napi_value object)
42 {
43 auto valNative = reinterpret_cast<NativeValue*>(object);
44 return valNative == nullptr ? false : valNative->TypeOf() == NATIVE_FUNCTION;
45 }
46
IsArrayBuffer(napi_value value)47 bool NapiHelper::IsArrayBuffer(napi_value value)
48 {
49 auto valNative = reinterpret_cast<NativeValue*>(value);
50 return valNative == nullptr ? false : valNative->IsArrayBuffer();
51 }
52
IsNumber(napi_value value)53 bool NapiHelper::IsNumber(napi_value value)
54 {
55 auto valNative = reinterpret_cast<NativeValue*>(value);
56 return valNative == nullptr ? false : valNative->TypeOf() == NATIVE_NUMBER;
57 }
58
GetCallbackInfoArgc(napi_env env,napi_callback_info cbInfo)59 size_t NapiHelper::GetCallbackInfoArgc(napi_env env, napi_callback_info cbInfo)
60 {
61 size_t argc = 0;
62 napi_get_cb_info(env, cbInfo, &argc, nullptr, nullptr, nullptr);
63 return argc;
64 }
65
GetNamePropertyInParentPort(napi_env env,napi_ref parentPort,const char * name)66 napi_value NapiHelper::GetNamePropertyInParentPort(napi_env env, napi_ref parentPort, const char* name)
67 {
68 napi_value obj = nullptr;
69 napi_get_reference_value(env, parentPort, &obj);
70
71 napi_value value = nullptr;
72 napi_get_named_property(env, obj, name, &value);
73
74 return value;
75 }
76
GetUndefinedValue(napi_env env)77 napi_value NapiHelper::GetUndefinedValue(napi_env env)
78 {
79 napi_value result = nullptr;
80 napi_get_undefined(env, &result);
81 return result;
82 }
83
IsCallable(napi_env env,napi_value value)84 bool NapiHelper::IsCallable(napi_env env, napi_value value)
85 {
86 bool result = false;
87 napi_is_callable(env, value, &result);
88 return result;
89 }
90
IsCallable(napi_env env,napi_ref value)91 bool NapiHelper::IsCallable(napi_env env, napi_ref value)
92 {
93 napi_value obj = nullptr;
94 napi_get_reference_value(env, value, &obj);
95 if (obj == nullptr) {
96 return false;
97 }
98 return IsCallable(env, obj);
99 }
100
SetNamePropertyInGlobal(napi_env env,const char * name,napi_value value)101 void NapiHelper::SetNamePropertyInGlobal(napi_env env, const char* name, napi_value value)
102 {
103 napi_value object = nullptr;
104 napi_get_global(env, &object);
105 napi_set_named_property(env, object, name, value);
106 }
107
IsObject(napi_value value)108 bool NapiHelper::IsObject(napi_value value)
109 {
110 auto nativeValue = reinterpret_cast<NativeValue*>(value);
111 return nativeValue->TypeOf() == NATIVE_OBJECT;
112 }
113
GetString(napi_env env,napi_value value)114 char* NapiHelper::GetString(napi_env env, napi_value value)
115 {
116 size_t bufferSize = 0;
117 size_t strLength = 0;
118 napi_get_value_string_utf8(env, value, nullptr, 0, &bufferSize);
119 if (bufferSize > MAX_CHAR_LENGTH) {
120 bufferSize = MAX_CHAR_LENGTH;
121 }
122 char* buffer = new char[bufferSize + 1] { 0 };
123 napi_get_value_string_utf8(env, value, buffer, bufferSize + 1, &strLength);
124 return buffer;
125 }
126
CreateBooleanValue(napi_env env,bool value)127 napi_value NapiHelper::CreateBooleanValue(napi_env env, bool value)
128 {
129 napi_value result = nullptr;
130 napi_get_boolean(env, value, &result);
131 return result;
132 }
133
GetGlobalObject(napi_env env)134 napi_value NapiHelper::GetGlobalObject(napi_env env)
135 {
136 napi_value object = nullptr;
137 napi_get_global(env, &object);
138 return object;
139 }
140
CreateReference(napi_env env,napi_value value,uint32_t refcount)141 napi_ref NapiHelper::CreateReference(napi_env env, napi_value value, uint32_t refcount)
142 {
143 napi_ref callback = nullptr;
144 napi_create_reference(env, value, refcount, &callback);
145 return callback;
146 }
147
CreateUint32(napi_env env,uint32_t value)148 napi_value NapiHelper::CreateUint32(napi_env env, uint32_t value)
149 {
150 napi_value result = nullptr;
151 napi_create_uint32(env, value, &result);
152 return result;
153 }
154
GetLibUV(napi_env env)155 uv_loop_t* NapiHelper::GetLibUV(napi_env env)
156 {
157 uv_loop_t* loop;
158 napi_get_uv_event_loop(env, &loop);
159 return loop;
160 }
161
GetReferenceValue(napi_env env,napi_ref ref)162 napi_value NapiHelper::GetReferenceValue(napi_env env, napi_ref ref)
163 {
164 napi_value result = nullptr;
165 napi_get_reference_value(env, ref, &result);
166 return result;
167 }
168
DeleteReference(napi_env env,napi_ref ref)169 void NapiHelper::DeleteReference(napi_env env, napi_ref ref)
170 {
171 napi_delete_reference(env, ref);
172 }
173
GetNameProperty(napi_env env,napi_value obj,const char * name)174 napi_value NapiHelper::GetNameProperty(napi_env env, napi_value obj, const char* name)
175 {
176 napi_value result = nullptr;
177 napi_get_named_property(env, obj, name, &result);
178 return result;
179 }
180
HasNameProperty(napi_env env,napi_value obj,const char * name)181 bool NapiHelper::HasNameProperty(napi_env env, napi_value obj, const char* name)
182 {
183 bool result = false;
184 napi_has_named_property(env, obj, name, &result);
185 return result;
186 }
187
GetBooleanValue(napi_env env,napi_value value)188 bool NapiHelper::GetBooleanValue(napi_env env, napi_value value)
189 {
190 bool result = false;
191 napi_get_value_bool(env, value, &result);
192 return result;
193 }
194
StrictEqual(napi_env env,napi_value value,napi_value cmpValue)195 bool NapiHelper::StrictEqual(napi_env env, napi_value value, napi_value cmpValue)
196 {
197 bool isEqual = false;
198 napi_strict_equals(env, value, cmpValue, &isEqual);
199 return isEqual;
200 }
201
GetConstructorName(napi_env env,napi_value object)202 napi_value NapiHelper::GetConstructorName(napi_env env, napi_value object)
203 {
204 while (IsNotUndefined(object)) {
205 napi_value func = nullptr;
206 napi_get_own_property_descriptor(env, object, "constructor", &func);
207 bool isInstanceof = false;
208 napi_instanceof(env, object, func, &isInstanceof);
209 if (IsNotUndefined(func) && isInstanceof) {
210 napi_value ctorName = nullptr;
211 napi_get_own_property_descriptor(env, func, "name", &ctorName);
212 std::string name = GetString(env, ctorName);
213 if (name.size() > 0) {
214 return ctorName;
215 }
216 }
217 napi_value result = nullptr;
218 napi_get_prototype(env, object, &result);
219 object = result;
220 }
221 return nullptr;
222 }
223
CreateObject(napi_env env)224 napi_value NapiHelper::CreateObject(napi_env env)
225 {
226 napi_value obj = nullptr;
227 napi_create_object(env, &obj);
228 return obj;
229 }
230
CreatePromise(napi_env env,napi_deferred * deferred)231 napi_value NapiHelper::CreatePromise(napi_env env, napi_deferred* deferred)
232 {
233 napi_value promise = nullptr;
234 napi_create_promise(env, deferred, &promise);
235 return promise;
236 }
237
GetArrayLength(napi_env env,napi_value array)238 uint32_t NapiHelper::GetArrayLength(napi_env env, napi_value array)
239 {
240 uint32_t arrayLength = 0;
241 napi_get_array_length(env, array, &arrayLength);
242 return arrayLength;
243 }
244
GetUint32Value(napi_env env,napi_value value)245 uint32_t NapiHelper::GetUint32Value(napi_env env, napi_value value)
246 {
247 uint32_t result = 0;
248 napi_get_value_uint32(env, value, &result);
249 return result;
250 }
251
IsExceptionPending(napi_env env)252 bool NapiHelper::IsExceptionPending(napi_env env)
253 {
254 bool isExceptionPending = false;
255 napi_is_exception_pending(env, &isExceptionPending);
256 return isExceptionPending;
257 }
258 } // namespace Commonlibrary::Concurrent::Common::Helper