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
18 #include "string_ex.h"
19
20 namespace OHOS {
21 namespace MiscServices {
22 constexpr int64_t JS_NUMBER_MAX_VALUE = (1LL << 53) - 1;
GetType(napi_env env,napi_value in)23 napi_valuetype JsUtil::GetType(napi_env env, napi_value in)
24 {
25 napi_valuetype valueType = napi_undefined;
26 napi_typeof(env, in, &valueType);
27 return valueType;
28 }
HasProperty(napi_env env,napi_value object,const std::string & property)29 bool JsUtil::HasProperty(napi_env env, napi_value object, const std::string &property)
30 {
31 bool hasProperty = false;
32 napi_status status = napi_has_named_property(env, object, property.c_str(), &hasProperty);
33 if (status == napi_ok && hasProperty) {
34 return true;
35 }
36 return false;
37 }
GetValue(napi_env env,napi_value in,std::string & out)38 bool JsUtil::GetValue(napi_env env, napi_value in, std::string &out)
39 {
40 size_t size = 0;
41 auto status = napi_get_value_string_utf8(env, in, nullptr, 0, &size);
42 if (status != napi_ok) {
43 return false;
44 }
45 out.resize(size + 1, 0);
46 status = napi_get_value_string_utf8(env, in, const_cast<char *>(out.data()), size + 1, &size);
47 out.resize(size);
48 return status == napi_ok;
49 }
50
GetValue(napi_env env,napi_value in,std::u16string & out)51 bool JsUtil::GetValue(napi_env env, napi_value in, std::u16string &out)
52 {
53 std::string tempOut;
54 bool ret = GetValue(env, in, tempOut);
55 if (ret) {
56 out = Str8ToStr16(tempOut);
57 }
58 return ret;
59 }
GetValue(napi_env env,napi_value in,int32_t & out)60 bool JsUtil::GetValue(napi_env env, napi_value in, int32_t &out)
61 {
62 return napi_get_value_int32(env, in, &out) == napi_ok;
63 }
GetValue(napi_env env,napi_value in,uint32_t & out)64 bool JsUtil::GetValue(napi_env env, napi_value in, uint32_t &out)
65 {
66 return napi_get_value_uint32(env, in, &out) == napi_ok;
67 }
GetValue(napi_env env,napi_value in,int64_t & out)68 bool JsUtil::GetValue(napi_env env, napi_value in, int64_t &out)
69 {
70 return napi_get_value_int64(env, in, &out) == napi_ok;
71 }
GetValue(napi_env env,napi_value in,bool & out)72 bool JsUtil::GetValue(napi_env env, napi_value in, bool &out)
73 {
74 return napi_get_value_bool(env, in, &out) == napi_ok;
75 }
GetValue(napi_env env,napi_value in,double & out)76 bool JsUtil::GetValue(napi_env env, napi_value in, double &out)
77 {
78 return napi_get_value_double(env, in, &out) == napi_ok;
79 }
GetValue(napi_env env,napi_value in)80 napi_value JsUtil::GetValue(napi_env env, napi_value in)
81 {
82 return in;
83 }
GetValue(napi_env env,const std::string & in)84 napi_value JsUtil::GetValue(napi_env env, const std::string &in)
85 {
86 napi_value out = nullptr;
87 napi_create_string_utf8(env, in.c_str(), in.length(), &out);
88 return out;
89 }
GetValue(napi_env env,int32_t in)90 napi_value JsUtil::GetValue(napi_env env, int32_t in)
91 {
92 napi_value out = nullptr;
93 napi_create_int32(env, in, &out);
94 return out;
95 }
GetValue(napi_env env,uint32_t in)96 napi_value JsUtil::GetValue(napi_env env, uint32_t in)
97 {
98 napi_value out = nullptr;
99 napi_create_uint32(env, in, &out);
100 return out;
101 }
GetValue(napi_env env,int64_t in)102 napi_value JsUtil::GetValue(napi_env env, int64_t in)
103 {
104 if (in > JS_NUMBER_MAX_VALUE) {
105 // cannot exceed the range of js
106 return nullptr;
107 }
108 napi_value out = nullptr;
109 napi_create_int64(env, in, &out);
110 return out;
111 }
GetValue(napi_env env,bool in)112 napi_value JsUtil::GetValue(napi_env env, bool in)
113 {
114 napi_value out = nullptr;
115 napi_get_boolean(env, in, &out);
116 return out;
117 }
118
GetValueU16String(napi_env env,const std::u16string & in)119 napi_value JsUtil::GetValueU16String(napi_env env, const std::u16string &in)
120 {
121 napi_value out = nullptr;
122 napi_create_string_utf16(env, in.c_str(), in.length(), &out);
123 return out;
124 }
125
GetValueU16String(napi_env env,napi_value in,std::u16string & out)126 bool JsUtil::GetValueU16String(napi_env env, napi_value in, std::u16string &out)
127 {
128 size_t size = 0;
129 auto status = napi_get_value_string_utf16(env, in, nullptr, 0, &size);
130 if (status != napi_ok) {
131 return false;
132 }
133 out.resize(size + 1, 0);
134 status = napi_get_value_string_utf16(env, in, out.data(), size + 1, &size);
135 out.resize(size);
136 return status == napi_ok;
137 }
138 } // namespace MiscServices
139 } // namespace OHOS