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 "intell_voice_napi_util.h"
17 #include "intell_voice_log.h"
18 
19 #define LOG_TAG "IntellVoiceNapiUtil"
20 
21 using namespace std;
22 
23 namespace OHOS {
24 namespace IntellVoiceNapi {
25 static const int32_t MAX_STRING_BUFFSIZE = 1024;
26 
IntellVoiceRef(napi_env env,napi_ref ref)27 IntellVoiceRef::IntellVoiceRef(napi_env env, napi_ref ref) : env_(env), ref_(ref)
28 {
29     INTELL_VOICE_LOG_INFO("enter");
30 }
31 
~IntellVoiceRef()32 IntellVoiceRef::~IntellVoiceRef()
33 {
34     INTELL_VOICE_LOG_INFO("enter");
35     if (env_ != nullptr && ref_ != nullptr) {
36         napi_delete_reference(env_, ref_);
37     }
38     env_ = nullptr;
39     ref_ = nullptr;
40 }
41 
GetRefValue()42 napi_value IntellVoiceRef::GetRefValue()
43 {
44     napi_value value = nullptr;
45     napi_status status = napi_get_reference_value(env_, ref_, &value);
46     if (status != napi_ok || value == nullptr) {
47         INTELL_VOICE_LOG_ERROR("get reference value fail");
48         return nullptr;
49     }
50     return value;
51 }
52 
SetValue(napi_env env,const int32_t value)53 napi_value SetValue(napi_env env, const int32_t value)
54 {
55     napi_value result = nullptr;
56     napi_status status = napi_create_int32(env, value, &result);
57     if (status != napi_ok || result == nullptr) {
58         INTELL_VOICE_LOG_ERROR("get js value fail");
59         return nullptr;
60     }
61     return result;
62 }
63 
SetValue(napi_env env,const uint32_t value)64 napi_value SetValue(napi_env env, const uint32_t value)
65 {
66     napi_value result = nullptr;
67     napi_status status = napi_create_uint32(env, value, &result);
68     if (status != napi_ok || result == nullptr) {
69         INTELL_VOICE_LOG_ERROR("get js value fail");
70         return nullptr;
71     }
72     return result;
73 }
74 
SetValue(napi_env env,const string & value)75 napi_value SetValue(napi_env env, const string &value)
76 {
77     napi_value result = nullptr;
78     napi_status status = napi_create_string_utf8(env, value.c_str(), value.size(), &result);
79     if (status != napi_ok || result == nullptr) {
80         INTELL_VOICE_LOG_ERROR("get js value fail");
81         return nullptr;
82     }
83     return result;
84 }
85 
SetValue(napi_env env,const std::vector<uint8_t> & value)86 napi_value SetValue(napi_env env, const std::vector<uint8_t> &value)
87 {
88     napi_value result = nullptr;
89     void *buf = nullptr;
90     napi_status status = napi_create_arraybuffer(env, value.size(), &buf, &result);
91     if (status != napi_ok || result == nullptr) {
92         INTELL_VOICE_LOG_ERROR("get js array value fail");
93         return nullptr;
94     }
95     std::copy(value.begin(), value.end(), static_cast<uint8_t *>(buf));
96     return result;
97 }
98 
GetValue(napi_env env,napi_value jsValue,int32_t & value)99 napi_status GetValue(napi_env env, napi_value jsValue, int32_t &value)
100 {
101     napi_valuetype valueType = napi_undefined;
102     napi_typeof(env, jsValue, &valueType);
103     if (valueType != napi_number) {
104         return napi_generic_failure;
105     }
106     return napi_get_value_int32(env, jsValue, &value);
107 }
108 
GetValue(napi_env env,napi_value jsValue,bool & value)109 napi_status GetValue(napi_env env, napi_value jsValue, bool &value)
110 {
111     napi_valuetype valueType = napi_undefined;
112     napi_typeof(env, jsValue, &valueType);
113     if (valueType != napi_boolean) {
114         return napi_generic_failure;
115     }
116     return napi_get_value_bool(env, jsValue, &value);
117 }
118 
GetValue(napi_env env,napi_value jsValue,string & value)119 napi_status GetValue(napi_env env, napi_value jsValue, string &value)
120 {
121     size_t bufferSize = 0;
122     napi_valuetype valueType = napi_undefined;
123     napi_typeof(env, jsValue, &valueType);
124     if (valueType != napi_string) {
125         return napi_generic_failure;
126     }
127     napi_status status = napi_get_value_string_utf8(env, jsValue, nullptr, 0, &bufferSize);
128     if (status != napi_ok || bufferSize == 0 || bufferSize >= MAX_STRING_BUFFSIZE) {
129         INTELL_VOICE_LOG_ERROR("failed to get buffersize");
130         return napi_generic_failure;
131     }
132 
133     char *buffer = new (std::nothrow) char[(bufferSize + 1)];
134     if (buffer == nullptr) {
135         INTELL_VOICE_LOG_ERROR("no memory");
136         return napi_generic_failure;
137     }
138 
139     status = napi_get_value_string_utf8(env, jsValue, buffer, bufferSize + 1, &bufferSize);
140     value = buffer;
141 
142     delete[] buffer;
143     buffer = nullptr;
144 
145     return status;
146 }
147 
GetValue(napi_env env,napi_value jsValue,std::vector<uint8_t> & value)148 napi_status GetValue(napi_env env, napi_value jsValue, std::vector<uint8_t> &value)
149 {
150     napi_valuetype valueType = napi_undefined;
151     void *buf = nullptr;
152     size_t byteLength = 0;
153 
154     napi_typeof(env, jsValue, &valueType);
155     if (valueType != napi_object) {
156         INTELL_VOICE_LOG_ERROR("value is not object");
157         return napi_generic_failure;
158     }
159 
160     napi_status status = napi_get_arraybuffer_info(env, jsValue, &buf, &byteLength);
161     if (status != napi_ok) {
162         INTELL_VOICE_LOG_ERROR("failed to get arraybuf info");
163         return napi_generic_failure;
164     }
165 
166     if (buf == nullptr || byteLength == 0) {
167         INTELL_VOICE_LOG_ERROR("failed to get arraybuf length %zu", byteLength);
168         return napi_generic_failure;
169     }
170 
171     value.resize(byteLength);
172     std::copy(static_cast<uint8_t *>(buf), static_cast<uint8_t *>(buf) + byteLength, &value[0]);
173 
174     return status;
175 }
176 
177 }  // namespace IntellVoiceNapi
178 }  // namespace OHOS
179