1 /*
2 * Copyright (c) 2023 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/native_api.h"
17 #include "napi_helper.h"
18
19 static constexpr uint32_t MAX_CHAR_LENGTH = 1024;
20
GetString(napi_env env,napi_value value)21 char *NapiHelper::GetString(napi_env env, napi_value value)
22 {
23 size_t bufferSize = 0;
24 size_t strLength = 0;
25 napi_get_value_string_utf8(env, value, nullptr, 0, &bufferSize);
26 if (bufferSize > MAX_CHAR_LENGTH) {
27 bufferSize = MAX_CHAR_LENGTH;
28 }
29 char *buffer = new char[bufferSize + 1]{0};
30 napi_get_value_string_utf8(env, value, buffer, bufferSize + 1, &strLength);
31 return buffer;
32 }
33
CreateBooleanValue(napi_env env,bool value)34 napi_value NapiHelper::CreateBooleanValue(napi_env env, bool value)
35 {
36 napi_value result = nullptr;
37 napi_get_boolean(env, value, &result);
38 return result;
39 }
40
GetGlobalObject(napi_env env)41 napi_value NapiHelper::GetGlobalObject(napi_env env)
42 {
43 napi_value object = nullptr;
44 napi_get_global(env, &object);
45 return object;
46 }
47
CreateReference(napi_env env,napi_value value,uint32_t refcount)48 napi_ref NapiHelper::CreateReference(napi_env env, napi_value value, uint32_t refcount)
49 {
50 napi_ref callback = nullptr;
51 napi_create_reference(env, value, refcount, &callback);
52 return callback;
53 }
54
CreateUint32(napi_env env,uint32_t value)55 napi_value NapiHelper::CreateUint32(napi_env env, uint32_t value)
56 {
57 napi_value result = nullptr;
58 napi_create_uint32(env, value, &result);
59 return result;
60 }
61
GetBooleanValue(napi_env env,napi_value value)62 bool NapiHelper::GetBooleanValue(napi_env env, napi_value value)
63 {
64 bool result = false;
65 napi_get_value_bool(env, value, &result);
66 return result;
67 }
68
StrictEqual(napi_env env,napi_value value,napi_value cmpValue)69 bool NapiHelper::StrictEqual(napi_env env, napi_value value, napi_value cmpValue)
70 {
71 bool isEqual = false;
72 napi_strict_equals(env, value, cmpValue, &isEqual);
73 return isEqual;
74 }
75
CreateObject(napi_env env)76 napi_value NapiHelper::CreateObject(napi_env env)
77 {
78 napi_value obj = nullptr;
79 napi_create_object(env, &obj);
80 return obj;
81 }
82
CreatePromise(napi_env env,napi_deferred * deferred)83 napi_value NapiHelper::CreatePromise(napi_env env, napi_deferred *deferred)
84 {
85 napi_value promise = nullptr;
86 napi_create_promise(env, deferred, &promise);
87 return promise;
88 }
89
GetArrayLength(napi_env env,napi_value array)90 uint32_t NapiHelper::GetArrayLength(napi_env env, napi_value array)
91 {
92 uint32_t arrayLength = 0;
93 return arrayLength;
94 }
95
GetUint32Value(napi_env env,napi_value value)96 uint32_t NapiHelper::GetUint32Value(napi_env env, napi_value value)
97 {
98 uint32_t result = 0;
99 napi_get_value_uint32(env, value, &result);
100 return result;
101 }
102
IsExceptionPending(napi_env env)103 bool NapiHelper::IsExceptionPending(napi_env env)
104 {
105 bool isExceptionPending = false;
106 napi_is_exception_pending(env, &isExceptionPending);
107 return isExceptionPending;
108 }