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 "js_util.h"
17 #include "string_ex.h"
18
19 namespace OHOS {
20 namespace MiscServices {
21 constexpr int64_t JS_NUMBER_MAX_VALUE = (1LL << 53) - 1;
GetType(napi_env env,napi_value in)22 napi_valuetype JsUtil::GetType(napi_env env, napi_value in)
23 {
24 napi_valuetype valueType = napi_undefined;
25 napi_typeof(env, in, &valueType);
26 return valueType;
27 }
GetValue(napi_env env,napi_value in,std::string & out)28 bool JsUtil::GetValue(napi_env env, napi_value in, std::string &out)
29 {
30 size_t size = 0;
31 auto status = napi_get_value_string_utf8(env, in, nullptr, 0, &size);
32 if (status != napi_ok) {
33 return false;
34 }
35 out.resize(size + 1, 0);
36 status = napi_get_value_string_utf8(env, in, const_cast<char *>(out.data()), size + 1, &size);
37 out.resize(size);
38 return status == napi_ok;
39 }
40
GetValue(napi_env env,napi_value in,std::u16string & out)41 bool JsUtil::GetValue(napi_env env, napi_value in, std::u16string &out)
42 {
43 std::string tempOut;
44 bool ret = GetValue(env, in, tempOut);
45 if (ret) {
46 out = Str8ToStr16(tempOut);
47 }
48 return ret;
49 }
GetValue(napi_env env,napi_value in,int32_t & out)50 bool JsUtil::GetValue(napi_env env, napi_value in, int32_t &out)
51 {
52 return napi_get_value_int32(env, in, &out) == napi_ok;
53 }
GetValue(napi_env env,napi_value in,uint32_t & out)54 bool JsUtil::GetValue(napi_env env, napi_value in, uint32_t &out)
55 {
56 return napi_get_value_uint32(env, in, &out) == napi_ok;
57 }
GetValue(napi_env env,napi_value in,int64_t & out)58 bool JsUtil::GetValue(napi_env env, napi_value in, int64_t &out)
59 {
60 return napi_get_value_int64(env, in, &out) == napi_ok;
61 }
GetValue(napi_env env,napi_value in,bool & out)62 bool JsUtil::GetValue(napi_env env, napi_value in, bool &out)
63 {
64 return napi_get_value_bool(env, in, &out) == napi_ok;
65 }
GetValue(napi_env env,napi_value in,double & out)66 bool JsUtil::GetValue(napi_env env, napi_value in, double &out)
67 {
68 return napi_get_value_double(env, in, &out) == napi_ok;
69 }
GetValue(napi_env env,const std::string & in)70 napi_value JsUtil::GetValue(napi_env env, const std::string &in)
71 {
72 napi_value out = nullptr;
73 napi_create_string_utf8(env, in.c_str(), in.length(), &out);
74 return out;
75 }
GetValue(napi_env env,int32_t in)76 napi_value JsUtil::GetValue(napi_env env, int32_t in)
77 {
78 napi_value out = nullptr;
79 napi_create_int32(env, in, &out);
80 return out;
81 }
GetValue(napi_env env,uint32_t in)82 napi_value JsUtil::GetValue(napi_env env, uint32_t in)
83 {
84 napi_value out = nullptr;
85 napi_create_uint32(env, in, &out);
86 return out;
87 }
GetValue(napi_env env,int64_t in)88 napi_value JsUtil::GetValue(napi_env env, int64_t in)
89 {
90 if (in > JS_NUMBER_MAX_VALUE) {
91 // cannot exceed the range of js
92 return nullptr;
93 }
94 napi_value out = nullptr;
95 napi_create_int64(env, in, &out);
96 return out;
97 }
GetValue(napi_env env,bool in)98 napi_value JsUtil::GetValue(napi_env env, bool in)
99 {
100 napi_value out = nullptr;
101 napi_get_boolean(env, in, &out);
102 return out;
103 }
104 } // namespace MiscServices
105 } // namespace OHOS