• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "js_utils.h"
16 #include "utils.h"
17 
18 namespace OHOS {
19 namespace Global {
20 namespace I18n {
DefaultConstructor(napi_env env,napi_callback_info info)21 napi_value JSUtils::DefaultConstructor(napi_env env, napi_callback_info info)
22 {
23     return nullptr;
24 }
25 
CreateEmptyString(napi_env env)26 napi_value JSUtils::CreateEmptyString(napi_env env)
27 {
28     napi_value result;
29     std::string emptyStr = "";
30     napi_status status = napi_create_string_utf8(env, emptyStr.c_str(), NAPI_AUTO_LENGTH, &result);
31     if (status != napi_ok) {
32         HILOG_ERROR_I18N("JSUtils: create empty string failed.");
33     }
34     return result;
35 }
36 
CreateEmptyArray(napi_env env)37 napi_value JSUtils::CreateEmptyArray(napi_env env)
38 {
39     napi_value result = nullptr;
40     napi_status status = napi_create_array(env, &result);
41     if (status != napi_ok) {
42         HILOG_ERROR_I18N("JSUtils::CreateEmptyArray: Failed to create empty array");
43     }
44     return result;
45 }
46 
CreateEmptyObject(napi_env env)47 napi_value JSUtils::CreateEmptyObject(napi_env env)
48 {
49     napi_value object = nullptr;
50     napi_status status = napi_create_object(env, &object);
51     if (status != napi_ok) {
52         HILOG_ERROR_I18N("JSUtils::CreateEmptyObject: Create empty object failed.");
53     }
54     return object;
55 }
56 
GetNumberOptionValues(napi_env env,napi_value options,std::map<std::string,std::string> & map)57 void JSUtils::GetNumberOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map)
58 {
59     JSUtils::GetOptionValue(env, options, "currency", map);
60     JSUtils::GetOptionValue(env, options, "currencySign", map);
61     JSUtils::GetOptionValue(env, options, "currencyDisplay", map);
62     JSUtils::GetOptionValue(env, options, "unit", map);
63     JSUtils::GetOptionValue(env, options, "unitDisplay", map);
64     JSUtils::GetOptionValue(env, options, "compactDisplay", map);
65     JSUtils::GetOptionValue(env, options, "signDisplay", map);
66     JSUtils::GetOptionValue(env, options, "localeMatcher", map);
67     JSUtils::GetOptionValue(env, options, "style", map);
68     JSUtils::GetOptionValue(env, options, "numberingSystem", map);
69     JSUtils::GetOptionValue(env, options, "notation", map);
70     JSUtils::GetOptionValue(env, options, "unitUsage", map);
71     JSUtils::GetOptionValue(env, options, "roundingPriority", map);
72     JSUtils::GetOptionValue(env, options, "roundingMode", map);
73     JSUtils::GetBoolOptionValue(env, options, "useGrouping", map);
74     GetIntegerOptionValue(env, options, "minimumIntegerDigits", map);
75     int64_t minFd = GetIntegerOptionValue(env, options, "minimumFractionDigits", map);
76     int64_t maxFd = GetIntegerOptionValue(env, options, "maximumFractionDigits", map);
77     if (minFd != -1 && maxFd != -1 && minFd > maxFd) {
78         HILOG_ERROR_I18N(
79             "GetNumberOptionValues: Invalid parameter value: minimumFractionDigits > maximumFractionDigits");
80     }
81     GetIntegerOptionValue(env, options, "minimumSignificantDigits", map);
82     GetIntegerOptionValue(env, options, "maximumSignificantDigits", map);
83     GetIntegerOptionValue(env, options, "roundingIncrement", map);
84 }
85 
GetIntegerOptionValue(napi_env env,napi_value options,const std::string & optionName,std::map<std::string,std::string> & map)86 int64_t JSUtils::GetIntegerOptionValue(napi_env env, napi_value options, const std::string &optionName,
87     std::map<std::string, std::string> &map)
88 {
89     napi_value optionValue = nullptr;
90     int64_t integerValue = -1;
91     napi_valuetype type = napi_undefined;
92     napi_status status = napi_typeof(env, options, &type);
93     if (status != napi_ok && type != napi_object) {
94         HILOG_ERROR_I18N("GetIntegerOptionValue: Set option failed, option is not an object");
95         return integerValue;
96     }
97     bool hasProperty = false;
98     napi_status propStatus = napi_has_named_property(env, options, optionName.c_str(), &hasProperty);
99     if (propStatus == napi_ok && hasProperty) {
100         status = napi_get_named_property(env, options, optionName.c_str(), &optionValue);
101         if (status == napi_ok) {
102             status = napi_get_value_int64(env, optionValue, &integerValue);
103             if (status == napi_ok) {
104                 map.insert(make_pair(optionName, std::to_string(integerValue)));
105             }
106         }
107     }
108     return integerValue;
109 }
110 
SetOptionProperties(napi_env env,napi_value & result,std::map<std::string,std::string> & options,const std::string & option)111 void JSUtils::SetOptionProperties(napi_env env, napi_value &result, std::map<std::string, std::string> &options,
112     const std::string &option)
113 {
114     if (options.count(option) > 0) {
115         std::string optionValue = options[option];
116         napi_value optionJsValue = nullptr;
117         napi_status status = napi_create_string_utf8(env, optionValue.c_str(), NAPI_AUTO_LENGTH, &optionJsValue);
118         if (status != napi_ok) {
119             return;
120         }
121         status = napi_set_named_property(env, result, option.c_str(), optionJsValue);
122         if (status != napi_ok) {
123             HILOG_ERROR_I18N("JSUtils::SetOptionProperties: Set property failed.");
124             return;
125         }
126     } else {
127         napi_value undefined = nullptr;
128         napi_status status = napi_get_undefined(env, &undefined);
129         if (status != napi_ok) {
130             return;
131         }
132         status = napi_set_named_property(env, result, option.c_str(), undefined);
133         if (status != napi_ok) {
134             HILOG_ERROR_I18N("JSUtils::SetOptionProperties: Set property failed.");
135             return;
136         }
137     }
138 }
139 
SetBooleanOptionProperties(napi_env env,napi_value & result,std::map<std::string,std::string> & options,const std::string & option)140 void JSUtils::SetBooleanOptionProperties(napi_env env, napi_value &result, std::map<std::string, std::string> &options,
141     const std::string &option)
142 {
143     if (options.count(option) > 0) {
144         std::string optionValue = options[option];
145         bool optionBoolValue = (optionValue == "true");
146         napi_value optionJsValue = nullptr;
147         napi_status status = napi_get_boolean(env, optionBoolValue, &optionJsValue);
148         if (status != napi_ok) {
149             return;
150         }
151         status = napi_set_named_property(env, result, option.c_str(), optionJsValue);
152         if (status != napi_ok) {
153             HILOG_ERROR_I18N("JSUtils::SetBooleanOptionProperties: Set property failed.");
154             return;
155         }
156     } else {
157         napi_value undefined = nullptr;
158         napi_status status = napi_get_undefined(env, &undefined);
159         if (status != napi_ok) {
160             return;
161         }
162         status = napi_set_named_property(env, result, option.c_str(), undefined);
163         if (status != napi_ok) {
164             HILOG_ERROR_I18N("JSUtils::SetBooleanOptionProperties: Set property failed.");
165             return;
166         }
167     }
168 }
169 
SetIntegerOptionProperties(napi_env env,napi_value & result,std::map<std::string,std::string> & options,const std::string & option)170 void JSUtils::SetIntegerOptionProperties(napi_env env, napi_value &result, std::map<std::string, std::string> &options,
171     const std::string &option)
172 {
173     napi_status status;
174     if (options.count(option) > 0) {
175         std::string optionValue = options[option];
176         napi_value optionJsValue = nullptr;
177         int32_t status = 0;
178         int64_t integerValue = ConvertString2Int(optionValue, status);
179         if (status != -1) {
180             status = napi_create_int64(env, integerValue, &optionJsValue);
181             if (status != napi_ok) {
182                 return;
183             }
184             status = napi_set_named_property(env, result, option.c_str(), optionJsValue);
185             if (status != napi_ok) {
186                 HILOG_ERROR_I18N("JSUtils::SetIntegerOptionProperties: Set property failed.");
187                 return;
188             }
189             return;
190         }
191     }
192     napi_value undefined = nullptr;
193     status = napi_get_undefined(env, &undefined);
194     if (status != napi_ok) {
195         return;
196     }
197     status = napi_set_named_property(env, result, option.c_str(), undefined);
198     if (status != napi_ok) {
199         HILOG_ERROR_I18N("JSUtils::SetIntegerOptionProperties: Set property failed.");
200         return;
201     }
202 }
203 
SetNamedStringProperties(napi_env env,napi_value & result,const std::string & key,const std::string & value)204 void JSUtils::SetNamedStringProperties(napi_env env, napi_value &result, const std::string &key,
205     const std::string &value)
206 {
207     napi_value jsValue = nullptr;
208     napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &jsValue);
209     if (status != napi_ok) {
210         HILOG_ERROR_I18N("JSUtils::SetNamedStringProperties: string to js failed.");
211         return;
212     }
213     status = napi_set_named_property(env, result, key.c_str(), jsValue);
214     if (status != napi_ok) {
215         HILOG_ERROR_I18N("JSUtils::SetNamedStringProperties: Set property failed.");
216         return;
217     }
218     return;
219 }
220 
SetNamedIntegerProperties(napi_env env,napi_value & result,const std::string & key,int64_t value)221 void JSUtils::SetNamedIntegerProperties(napi_env env, napi_value &result, const std::string &key, int64_t value)
222 {
223     napi_value jsValue = nullptr;
224     napi_status status = napi_create_int64(env, value, &jsValue);
225     if (status != napi_ok) {
226         HILOG_ERROR_I18N("JSUtils::SetNamedIntegerProperties: int64_t to js failed.");
227         return;
228     }
229     status = napi_set_named_property(env, result, key.c_str(), jsValue);
230     if (status != napi_ok) {
231         HILOG_ERROR_I18N("JSUtils::SetNamedIntegerProperties: Set property failed.");
232         return;
233     }
234     return;
235 }
236 
SetNamedVectorProperties(napi_env env,napi_value & result,const std::string & key,const std::vector<std::string> & value)237 void JSUtils::SetNamedVectorProperties(napi_env env, napi_value &result, const std::string &key,
238     const std::vector<std::string> &value)
239 {
240     napi_value jsValue = CreateArray(env, value);
241     if (!jsValue) {
242         HILOG_ERROR_I18N("JSUtils::SetNamedVectorProperties: craete array failed.");
243         return;
244     }
245     napi_status status = napi_set_named_property(env, result, key.c_str(), jsValue);
246     if (status != napi_ok) {
247         HILOG_ERROR_I18N("JSUtils::SetNamedVectorProperties: Set property failed.");
248         return;
249     }
250     return;
251 }
252 
GetString(napi_env env,napi_value value,int32_t & code)253 std::string JSUtils::GetString(napi_env env, napi_value value, int32_t& code)
254 {
255     size_t len = 0;
256     napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &len);
257     if (status != napi_ok) {
258         code = 1;
259         HILOG_ERROR_I18N("JSUtils::GetString: Get string len failed.");
260         return "";
261     }
262     std::vector<char> buffer(len + 1);
263     status = napi_get_value_string_utf8(env, value, buffer.data(), len + 1, &len);
264     if (status != napi_ok) {
265         code = 1;
266         HILOG_ERROR_I18N("JSUtils::GetString: Get string value failed.");
267         return "";
268     }
269     std::string result(buffer.data());
270     code = 0;
271     return result;
272 }
273 
GetBigIntStr(napi_env env,napi_value value,int32_t & code)274 std::string JSUtils::GetBigIntStr(napi_env env, napi_value value, int32_t& code)
275 {
276     napi_value global;
277     napi_status status = napi_get_global(env, &global);
278     if (status != napi_ok) {
279         code = -1;
280         HILOG_ERROR_I18N("JSUtils::GetBigIntStr: Get global failed.");
281         return "";
282     }
283     napi_value stringClass;
284     status = napi_get_named_property(env, global, "String", &stringClass);
285     if (status != napi_ok) {
286         code = -1;
287         HILOG_ERROR_I18N("JSUtils::GetBigIntStr: Get bigint value failed.");
288         return "";
289     }
290     napi_value argv[1] = { value };
291     napi_value toStringResult;
292     status = napi_call_function(env, global, stringClass, 1, argv, &toStringResult);
293     if (status != napi_ok) {
294         code = -1;
295         HILOG_ERROR_I18N("JSUtils::GetBigIntStr: Get bigint value failed.");
296         return "";
297     }
298     return GetString(env, toStringResult, code);
299 }
300 
GetStringArray(napi_env env,napi_value value,int32_t & code)301 std::vector<std::string> JSUtils::GetStringArray(napi_env env, napi_value value, int32_t& code)
302 {
303     std::vector<std::string> result;
304     uint32_t arrayLength = 0;
305     napi_status status = napi_get_array_length(env, value, &arrayLength);
306     if (status != napi_ok) {
307         HILOG_ERROR_I18N("JSUtils::GetStringArray: Get array length failed.");
308         code = 1;
309         return result;
310     }
311     result.resize(arrayLength);
312     for (uint32_t i = 0; i < arrayLength; i++) {
313         napi_value element = nullptr;
314         status = napi_get_element(env, value, i, &element);
315         if (status != napi_ok) {
316             HILOG_ERROR_I18N("JSUtils::GetStringArray: Get element %{public}d failed.", i);
317             code = 1;
318             return result;
319         }
320         std::string locale = JSUtils::GetString(env, element, code);
321         if (code != 0) {
322             HILOG_ERROR_I18N("JSUtils::GetStringArray: Get string failed.");
323             return result;
324         }
325         result[i] = locale;
326     }
327     code = 0;
328     return result;
329 }
330 
GetLocaleArray(napi_env env,napi_value value,int32_t & code)331 std::vector<std::string> JSUtils::GetLocaleArray(napi_env env, napi_value value, int32_t& code)
332 {
333     std::vector<std::string> result;
334     napi_valuetype valueType = napi_valuetype::napi_undefined;
335     napi_status status = napi_typeof(env, value, &valueType);
336     if (status != napi_ok) {
337         code = 1;
338         HILOG_ERROR_I18N("JSUtils::GetLocaleVector: Get type of value failed.");
339         return result;
340     }
341     if (valueType == napi_valuetype::napi_undefined) {
342         return result;
343     }
344     if (valueType == napi_valuetype::napi_string) {
345         std::string locale = JSUtils::GetString(env, value, code);
346         if (code != 0) {
347             HILOG_ERROR_I18N("JSUtils::GetLocaleVector: Get string failed.");
348             return result;
349         }
350         result.emplace_back(locale);
351         code = 0;
352         return result;
353     }
354     bool isArray = false;
355     status = napi_is_array(env, value, &isArray);
356     if (status != napi_ok) {
357         HILOG_ERROR_I18N("JSUtils::GetLocaleVector: Check value is array failed.");
358         code = 1;
359         return result;
360     }
361     if (!isArray) {
362         HILOG_ERROR_I18N("JSUtils::GetLocaleVector: Type of value is invalid.");
363         code = 1;
364         return result;
365     }
366     return JSUtils::GetStringArray(env, value, code);
367 }
368 
CreateObject(napi_env env,std::unordered_map<std::string,napi_value> & propertys,int32_t & code)369 napi_value JSUtils::CreateObject(napi_env env, std::unordered_map<std::string, napi_value>& propertys, int32_t& code)
370 {
371     napi_value object = nullptr;
372     napi_status status = napi_create_object(env, &object);
373     if (status != napi_ok) {
374         code = 1;
375         HILOG_ERROR_I18N("JSUtils::CreateObject: Create object failed.");
376         return nullptr;
377     }
378     for (const auto& property : propertys) {
379         std::string key = property.first;
380         napi_value value = property.second;
381         status = napi_set_named_property(env, object, key.c_str(), value);
382         if (status != napi_ok) {
383             code = 1;
384             HILOG_ERROR_I18N("JSUtils::CreateObject: Set property failed, key %{public}s.", key.c_str());
385             return nullptr;
386         }
387     }
388     code = 0;
389     return object;
390 }
391 
CreateArrayItem(napi_env env,std::unordered_map<std::string,napi_value> & propertys,int32_t & code,std::vector<std::string> & keySequence)392 napi_value JSUtils::CreateArrayItem(napi_env env, std::unordered_map<std::string, napi_value>& propertys,
393     int32_t& code, std::vector<std::string> &keySequence)
394 {
395     napi_value object = nullptr;
396     napi_status status = napi_create_object(env, &object);
397     if (status != napi_ok) {
398         code = 1;
399         HILOG_ERROR_I18N("JSUtils::CreateArrayItem: Create object failed.");
400         return nullptr;
401     }
402     for (auto iter = keySequence.begin(); iter != keySequence.end(); ++iter) {
403         std::string key = *iter;
404         if (propertys.find(key) == propertys.end()) {
405             continue;
406         }
407         napi_value value = propertys[key];
408         if (value == nullptr) {
409             continue;
410         }
411         status = napi_set_named_property(env, object, key.c_str(), value);
412         if (status != napi_ok) {
413             code = 1;
414             HILOG_ERROR_I18N("JSUtils::CreateArrayItem: Set property failed, key %{public}s.", key.c_str());
415             return nullptr;
416         }
417     }
418     code = 0;
419     return object;
420 }
421 
CreateArray(napi_env env,const std::vector<std::string> & value)422 napi_value JSUtils::CreateArray(napi_env env, const std::vector<std::string>& value)
423 {
424     napi_value result = nullptr;
425     napi_status status = napi_create_array_with_length(env, value.size(), &result);
426     if (status != napi_ok) {
427         HILOG_ERROR_I18N("JSUtils::CreateArray: Failed to create array");
428         return nullptr;
429     }
430     size_t pos = 0;
431     for (const auto& language : value) {
432         napi_value value = nullptr;
433         status = napi_create_string_utf8(env, language.c_str(), NAPI_AUTO_LENGTH, &value);
434         if (status != napi_ok) {
435             HILOG_ERROR_I18N("JSUtils::CreateArray: Failed to create string item");
436             return nullptr;
437         }
438         status = napi_set_element(env, result, pos, value);
439         if (status != napi_ok) {
440             HILOG_ERROR_I18N("JSUtils::CreateArray: Failed to set array item");
441             return nullptr;
442         }
443         pos++;
444     }
445     return result;
446 }
447 
CreateString(napi_env env,const std::string & str)448 napi_value JSUtils::CreateString(napi_env env, const std::string &str)
449 {
450     napi_value result;
451     napi_status status = napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &result);
452     if (status != napi_ok) {
453         HILOG_ERROR_I18N("JSUtils::CreateString : create string js variable failed.");
454         return nullptr;
455     }
456     return result;
457 }
458 
GetPropertyFormObject(napi_env env,napi_value object,const std::string & property,napi_valuetype type,std::string & value)459 bool JSUtils::GetPropertyFormObject(napi_env env, napi_value object, const std::string& property,
460     napi_valuetype type, std::string& value)
461 {
462     int32_t errCode = 0;
463     napi_value propertyValue = JSUtils::GetNapiPropertyFormObject(env, object, property, errCode);
464     if (errCode != 0) {
465         return false;
466     }
467     napi_status status = napi_ok;
468     switch (type) {
469         case napi_valuetype::napi_boolean: {
470             bool boolValue = false;
471             status = napi_get_value_bool(env, propertyValue, &boolValue);
472             if (status != napi_ok) {
473                 HILOG_ERROR_I18N("JSUtils::GetPropertyFormObject: Get bool failed, property is %{public}s.",
474                     property.c_str());
475                 return false;
476             }
477             value = boolValue ? "true" : "false";
478             break;
479         }
480         case napi_valuetype::napi_string: {
481             int32_t code = 0;
482             value = JSUtils::GetString(env, propertyValue, code);
483             if (code != 0) {
484                 HILOG_ERROR_I18N("JSUtils::GetPropertyFormObject: Get string failed, property is %{public}s.",
485                     property.c_str());
486                 return false;
487             }
488             break;
489         }
490         case napi_valuetype::napi_number: {
491             int32_t intValue = 0;
492             status = napi_get_value_int32(env, propertyValue, &intValue);
493             if (status != napi_ok) {
494                 HILOG_ERROR_I18N("JSUtils::GetPropertyFormObject: Get int failed, property is %{public}s.",
495                     property.c_str());
496                 return false;
497             }
498             value = std::to_string(intValue);
499             break;
500         }
501         default:
502             return false;
503     }
504     return true;
505 }
506 
GetNapiPropertyFormObject(napi_env env,napi_value object,const std::string & property,int32_t & errCode)507 napi_value JSUtils::GetNapiPropertyFormObject(napi_env env, napi_value object, const std::string& property,
508     int32_t& errCode)
509 {
510     errCode = -1;
511     bool hasProperty = false;
512     napi_status status = napi_has_named_property(env, object, property.c_str(), &hasProperty);
513     if (status != napi_ok) {
514         HILOG_ERROR_I18N("JSUtils::GetNapiPropertyFormObject: Call napi_has_named_property failed, "
515             "property is %{public}s.", property.c_str());
516         return nullptr;
517     }
518     if (!hasProperty) {
519         return nullptr;
520     }
521     napi_value propertyValue = nullptr;
522     status = napi_get_named_property(env, object, property.c_str(), &propertyValue);
523     if (status != napi_ok || propertyValue == nullptr) {
524         HILOG_ERROR_I18N("JSUtils::GetPropertyFormObject: Call napi_get_named_property failed, "
525             "property is %{public}s.", property.c_str());
526         return nullptr;
527     }
528     errCode = 0;
529     return propertyValue;
530 }
531 
GetNumberValue(napi_env env,napi_value param,int32_t & errorCode)532 double JSUtils::GetNumberValue(napi_env env, napi_value param, int32_t &errorCode)
533 {
534     napi_value global = nullptr;
535     napi_status status = napi_get_global(env, &global);
536     if (status != napi_ok || global == nullptr) {
537         HILOG_ERROR_I18N("JSUtils::GetNumberValue: get global object failed");
538         errorCode = -1;
539         return 0;
540     }
541     napi_value numberConstructor;
542     status = napi_get_named_property(env, global, "Number", &numberConstructor);
543     if (status != napi_ok || numberConstructor == nullptr) {
544         HILOG_ERROR_I18N("JSUtils::GetNumberValue: Number constructor failed");
545         errorCode = -1;
546         return 0;
547     }
548     size_t argc = 1;
549     napi_value instance = nullptr;
550     napi_value argv[1] = { param };
551     status = napi_new_instance(env, numberConstructor, argc, argv, &instance);
552     if (status != napi_ok || instance == nullptr) {
553         HILOG_ERROR_I18N("JSUtils::GetNumberValue: Create Number object failed");
554         errorCode = -1;
555         return 0;
556     }
557     napi_value valueOfFunc = nullptr;
558     status = napi_get_named_property(env, instance, "valueOf", &valueOfFunc);
559     if (status != napi_ok || valueOfFunc == nullptr) {
560         HILOG_ERROR_I18N("JSUtils::GetNumberValue: Get valueOf function failed");
561         errorCode = -1;
562         return 0;
563     }
564     napi_value retValue = nullptr;
565     status = napi_call_function(env, instance, valueOfFunc, 0, nullptr, &retValue);
566     if (status != napi_ok || retValue == nullptr) {
567         HILOG_ERROR_I18N("JSUtils::GetNumberValue: Call valueOf() function failed");
568         errorCode = -1;
569         return 0;
570     }
571     double number = 0;
572     status = napi_get_value_double(env, retValue, &number);
573     if (status != napi_ok) {
574         HILOG_ERROR_I18N("JSUtils::GetNumberValue: Get valueOf() double value failed");
575         errorCode = -1;
576         return 0;
577     }
578     return number;
579 }
580 
GetDoubleFromNapiValue(napi_env env,napi_value param,napi_valuetype & valueType,int32_t & errorCode)581 double JSUtils::GetDoubleFromNapiValue(napi_env env, napi_value param, napi_valuetype &valueType,
582     int32_t &errorCode)
583 {
584     if (valueType != napi_valuetype::napi_number) {
585         return GetNumberValue(env, param, errorCode);
586     }
587     double number;
588     napi_status status = napi_get_value_double(env, param, &number);
589     if (status != napi_ok) {
590         HILOG_ERROR_I18N("JSUtils::GetDoubleFromNapiValue: Get double value failed");
591         errorCode = -1;
592         return 0;
593     }
594     return number;
595 }
596 } // namespace I18n
597 } // namespace Global
598 } // namespace OHOS