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 }
HasProperty(napi_env env,napi_value object,const std::string & property)28 bool JsUtil::HasProperty(napi_env env, napi_value object, const std::string &property)
29 {
30 bool hasProperty = false;
31 napi_status status = napi_has_named_property(env, object, property.c_str(), &hasProperty);
32 if (status == napi_ok && hasProperty) {
33 return true;
34 }
35 return false;
36 }
GetValue(napi_env env,napi_value in,std::string & out)37 bool JsUtil::GetValue(napi_env env, napi_value in, std::string &out)
38 {
39 size_t size = 0;
40 auto status = napi_get_value_string_utf8(env, in, nullptr, 0, &size);
41 if (status != napi_ok) {
42 return false;
43 }
44 out.resize(size + 1, 0);
45 status = napi_get_value_string_utf8(env, in, const_cast<char *>(out.data()), size + 1, &size);
46 out.resize(size);
47 return status == napi_ok;
48 }
49
GetValue(napi_env env,napi_value in,std::u16string & out)50 bool JsUtil::GetValue(napi_env env, napi_value in, std::u16string &out)
51 {
52 std::string tempOut;
53 bool ret = GetValue(env, in, tempOut);
54 if (ret) {
55 out = Str8ToStr16(tempOut);
56 }
57 return ret;
58 }
GetValue(napi_env env,napi_value in,int32_t & out)59 bool JsUtil::GetValue(napi_env env, napi_value in, int32_t &out)
60 {
61 return napi_get_value_int32(env, in, &out) == napi_ok;
62 }
GetValue(napi_env env,napi_value in,uint32_t & out)63 bool JsUtil::GetValue(napi_env env, napi_value in, uint32_t &out)
64 {
65 return napi_get_value_uint32(env, in, &out) == napi_ok;
66 }
GetValue(napi_env env,napi_value in,int64_t & out)67 bool JsUtil::GetValue(napi_env env, napi_value in, int64_t &out)
68 {
69 return napi_get_value_int64(env, in, &out) == napi_ok;
70 }
GetValue(napi_env env,napi_value in,bool & out)71 bool JsUtil::GetValue(napi_env env, napi_value in, bool &out)
72 {
73 return napi_get_value_bool(env, in, &out) == napi_ok;
74 }
GetValue(napi_env env,napi_value in,double & out)75 bool JsUtil::GetValue(napi_env env, napi_value in, double &out)
76 {
77 return napi_get_value_double(env, in, &out) == napi_ok;
78 }
GetValue(napi_env env,napi_value in)79 napi_value JsUtil::GetValue(napi_env env, napi_value in)
80 {
81 return in;
82 }
GetValue(napi_env env,const std::string & in)83 napi_value JsUtil::GetValue(napi_env env, const std::string &in)
84 {
85 napi_value out = nullptr;
86 napi_create_string_utf8(env, in.c_str(), in.length(), &out);
87 return out;
88 }
GetValue(napi_env env,int32_t in)89 napi_value JsUtil::GetValue(napi_env env, int32_t in)
90 {
91 napi_value out = nullptr;
92 napi_create_int32(env, in, &out);
93 return out;
94 }
GetValue(napi_env env,uint32_t in)95 napi_value JsUtil::GetValue(napi_env env, uint32_t in)
96 {
97 napi_value out = nullptr;
98 napi_create_uint32(env, in, &out);
99 return out;
100 }
GetValue(napi_env env,int64_t in)101 napi_value JsUtil::GetValue(napi_env env, int64_t in)
102 {
103 if (in > JS_NUMBER_MAX_VALUE) {
104 // cannot exceed the range of js
105 return nullptr;
106 }
107 napi_value out = nullptr;
108 napi_create_int64(env, in, &out);
109 return out;
110 }
GetValue(napi_env env,bool in)111 napi_value JsUtil::GetValue(napi_env env, bool in)
112 {
113 napi_value out = nullptr;
114 napi_get_boolean(env, in, &out);
115 return out;
116 }
117 } // namespace MiscServices
118 } // namespace OHOS