• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "js_utils.h"
17 
18 #include "js_logger.h"
19 
20 #ifndef MAC_PLATFORM
21 #include "securec.h"
22 #endif
23 
24 namespace OHOS {
25 namespace PreferencesJsKit {
Convert2String(napi_env env,napi_value jsStr,std::string & output)26 int32_t JSUtils::Convert2String(napi_env env, napi_value jsStr, std::string &output)
27 {
28     char *str = new char[MAX_VALUE_LENGTH + 1];
29     size_t valueSize = 0;
30     napi_status status = napi_get_value_string_utf8(env, jsStr, str, MAX_VALUE_LENGTH, &valueSize);
31     if (status != napi_ok) {
32         LOG_ERROR("JSUtils::Convert2String get jsVal failed, status = %{public}d", status);
33         delete[] str;
34         return ERR;
35     }
36     output = std::string(str);
37     delete[] str;
38     return OK;
39 }
40 
Convert2Bool(napi_env env,napi_value jsBool,bool & output)41 int32_t JSUtils::Convert2Bool(napi_env env, napi_value jsBool, bool &output)
42 {
43     bool bValue = false;
44     napi_status status = napi_get_value_bool(env, jsBool, &bValue);
45     if (status != napi_ok) {
46         LOG_ERROR("JSUtils::Convert2Bool get jsVal failed, status = %{public}d", status);
47         return ERR;
48     }
49     output = bValue;
50     return OK;
51 }
52 
Convert2Double(napi_env env,napi_value jsNum,double & output)53 int32_t JSUtils::Convert2Double(napi_env env, napi_value jsNum, double &output)
54 {
55     double number = 0.0;
56     napi_status status = napi_get_value_double(env, jsNum, &number);
57     if (status != napi_ok) {
58         LOG_ERROR("JSUtils::Convert2Double get jsVal failed, status = %{public}d", status);
59         return ERR;
60     }
61     output = number;
62     return OK;
63 }
64 
Convert2StrVector(napi_env env,napi_value value,std::vector<std::string> & output)65 int32_t JSUtils::Convert2StrVector(napi_env env, napi_value value, std::vector<std::string> &output)
66 {
67     uint32_t arrLen = 0;
68     napi_status status = napi_get_array_length(env, value, &arrLen);
69     if (status != napi_ok) {
70         LOG_ERROR("JSUtils::Convert2StrVector get arrLength failed, status = %{public}d", status);
71         output = {};
72         return ERR;
73     }
74     napi_value element = nullptr;
75     for (size_t i = 0; i < arrLen; ++i) {
76         status = napi_get_element(env, value, i, &element);
77         if (status != napi_ok) {
78             LOG_ERROR("JSUtils::Convert2StrVector get element failed, status = %{public}d", status);
79             return ERR;
80         }
81         std::string str;
82         if (Convert2String(env, element, str) != OK) {
83             LOG_ERROR("JSUtils::Convert2StrVector convert element failed");
84             return ERR;
85         }
86         output.push_back(str);
87     }
88     return OK;
89 }
90 
Convert2BoolVector(napi_env env,napi_value value,std::vector<bool> & output)91 int32_t JSUtils::Convert2BoolVector(napi_env env, napi_value value, std::vector<bool> &output)
92 {
93     uint32_t arrLen = 0;
94     napi_status status = napi_get_array_length(env, value, &arrLen);
95     if (status != napi_ok) {
96         LOG_ERROR("JSUtils::Convert2BoolVector get arrLength failed, status = %{public}d", status);
97         output = {};
98         return ERR;
99     }
100     napi_value element = nullptr;
101     for (size_t i = 0; i < arrLen; ++i) {
102         status = napi_get_element(env, value, i, &element);
103         if (status != napi_ok) {
104             LOG_ERROR("JSUtils::Convert2BoolVector get element failed, status = %{public}d", status);
105             return ERR;
106         }
107         bool bVal = false;
108         if (Convert2Bool(env, element, bVal) != OK) {
109             LOG_ERROR("JSUtils::Convert2BoolVector convert element failed");
110             return ERR;
111         }
112         output.push_back(bVal);
113     }
114     return OK;
115 }
116 
Convert2DoubleVector(napi_env env,napi_value value,std::vector<double> & output)117 int32_t JSUtils::Convert2DoubleVector(napi_env env, napi_value value, std::vector<double> &output)
118 {
119     uint32_t arrLen = 0;
120     napi_status status = napi_get_array_length(env, value, &arrLen);
121     if (status != napi_ok) {
122         LOG_ERROR("JSUtils::Convert2DoubleVector get arrLength failed, status = %{public}d", status);
123         output = {};
124         return ERR;
125     }
126     napi_value element = nullptr;
127     for (size_t i = 0; i < arrLen; ++i) {
128         status = napi_get_element(env, value, i, &element);
129         if (status != napi_ok) {
130             LOG_ERROR("JSUtils::Convert2DoubleVector get element failed, status = %{public}d", status);
131             return ERR;
132         }
133         double number = 0.0;
134         if (Convert2Double(env, element, number) != OK) {
135             LOG_ERROR("JSUtils::Convert2Double convert element failed");
136             return ERR;
137         }
138         output.push_back(number);
139     }
140     return OK;
141 }
142 
Equals(napi_env env,napi_value value,napi_ref copy)143 bool JSUtils::Equals(napi_env env, napi_value value, napi_ref copy)
144 {
145     if (copy == nullptr) {
146         return (value == nullptr);
147     }
148 
149     napi_value copyValue = nullptr;
150     napi_get_reference_value(env, copy, &copyValue);
151 
152     bool isEquals = false;
153     napi_strict_equals(env, value, copyValue, &isEquals);
154     return isEquals;
155 }
156 
Convert2JSValue(napi_env env,std::string value,napi_value & output)157 int32_t JSUtils::Convert2JSValue(napi_env env, std::string value, napi_value &output)
158 {
159     if (napi_create_string_utf8(env, value.c_str(), value.size(), &output) != napi_ok) {
160         LOG_ERROR("Convert2JSValue create JS string failed");
161         return ERR;
162     }
163     return OK;
164 }
165 
Convert2JSValue(napi_env env,bool value,napi_value & output)166 int32_t JSUtils::Convert2JSValue(napi_env env, bool value, napi_value &output)
167 {
168     if (napi_get_boolean(env, value, &output) != napi_ok) {
169         LOG_ERROR("Convert2JSValue create JS bool failed");
170         return ERR;
171     }
172     return OK;
173 }
174 
Convert2JSValue(napi_env env,double value,napi_value & output)175 int32_t JSUtils::Convert2JSValue(napi_env env, double value, napi_value &output)
176 {
177     if (napi_create_double(env, value, &output) != napi_ok) {
178         LOG_ERROR("Convert2JSValue create JS double failed");
179         return ERR;
180     }
181     return OK;
182 }
183 
Convert2JSDoubleArr(napi_env env,std::vector<double> value,napi_value & output)184 int32_t JSUtils::Convert2JSDoubleArr(napi_env env, std::vector<double> value, napi_value &output)
185 {
186     size_t arrLen = value.size();
187     napi_status status = napi_create_array_with_length(env, arrLen, &output);
188     if (status != napi_ok) {
189         LOG_ERROR("JSUtils::Convert2JSValue get arrLength failed");
190         return ERR;
191     }
192     napi_value val = nullptr;
193     for (size_t i = 0; i < arrLen; i++) {
194         if (Convert2JSValue(env, value[i], val) != OK) {
195             LOG_ERROR("JSUtils::Convert2JSValue creat double failed");
196             return ERR;
197         }
198         status = napi_set_element(env, output, i, val);
199         if (status != napi_ok) {
200             LOG_ERROR("JSUtils::Convert2JSValue set element failed");
201             return ERR;
202         }
203     }
204     return OK;
205 }
206 
Convert2JSBoolArr(napi_env env,std::vector<bool> value,napi_value & output)207 int32_t JSUtils::Convert2JSBoolArr(napi_env env, std::vector<bool> value, napi_value &output)
208 {
209     size_t arrLen = value.size();
210     napi_status status = napi_create_array_with_length(env, arrLen, &output);
211     if (status != napi_ok) {
212         LOG_ERROR("JSUtils::Convert2JSValue get arrLength failed");
213         return ERR;
214     }
215     for (size_t i = 0; i < arrLen; i++) {
216         napi_value val = nullptr;
217         if (Convert2JSValue(env, value[i], val) != OK) {
218             LOG_ERROR("JSUtils::Convert2JSValue creat bool failed");
219             return ERR;
220         }
221         status = napi_set_element(env, output, i, val);
222         if (status != napi_ok) {
223             LOG_ERROR("JSUtils::Convert2JSValue set element failed");
224             return ERR;
225         }
226     }
227     return OK;
228 }
229 
Convert2JSStringArr(napi_env env,std::vector<std::string> value,napi_value & output)230 int32_t JSUtils::Convert2JSStringArr(napi_env env, std::vector<std::string> value, napi_value &output)
231 {
232     size_t arrLen = value.size();
233     napi_status status = napi_create_array_with_length(env, arrLen, &output);
234     if (status != napi_ok) {
235         LOG_ERROR("JSUtils::Convert2JSValue get arrLength failed");
236         return ERR;
237     }
238     for (size_t i = 0; i < arrLen; i++) {
239         napi_value val = nullptr;
240         if (Convert2JSValue(env, value[i], val) != OK) {
241             LOG_ERROR("JSUtils::Convert2JSValue creat string failed");
242             return ERR;
243         }
244         status = napi_set_element(env, output, i, val);
245         if (status != napi_ok) {
246             LOG_ERROR("JSUtils::Convert2JSValue set element failed");
247             return ERR;
248         }
249     }
250     return OK;
251 }
252 
Convert2JSValue(napi_env env,int32_t value)253 napi_value JSUtils::Convert2JSValue(napi_env env, int32_t value)
254 {
255     napi_value jsValue;
256     napi_status status = napi_create_int32(env, value, &jsValue);
257     if (status != napi_ok) {
258         return nullptr;
259     }
260     return jsValue;
261 }
262 } // namespace PreferencesJsKit
263 } // namespace OHOS
264