1 /*
2  * Copyright (c) 2021-2022 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_register_util.h"
17 
18 #include <cinttypes>
19 
20 #include "js_register_module.h"
21 #include "napi_constants.h"
22 #include "util_napi.h"
23 #include "util_napi_error.h"
24 
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "JSRegisterUtil"
27 
28 namespace OHOS {
29 namespace MMI {
GetNamedPropertyBool(const napi_env & env,const napi_value & object,const std::string & name,bool & ret)30 int32_t GetNamedPropertyBool(const napi_env& env, const napi_value& object, const std::string& name, bool& ret)
31 {
32     napi_value napiValue = {};
33     CHKRF(napi_get_named_property(env, object, name.c_str(), &napiValue), GET_NAMED_PROPERTY);
34     if (napiValue == nullptr) {
35         MMI_HILOGE("The value is null");
36         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid KeyEvent");
37         return RET_ERR;
38     }
39     napi_valuetype tmpType = napi_undefined;
40     CHKRF(napi_typeof(env, napiValue, &tmpType), TYPEOF);
41     if (tmpType != napi_boolean) {
42         MMI_HILOGE("The name is not bool");
43         THROWERR_API9(env, COMMON_PARAMETER_ERROR, name.c_str(), "bool");
44         return RET_ERR;
45     }
46     CHKRF(napi_get_value_bool(env, napiValue, &ret), GET_VALUE_BOOL);
47     return RET_OK;
48 }
49 
GetNamedPropertyInt32(const napi_env & env,const napi_value & object,const std::string & name,int32_t & ret,bool required)50 int32_t GetNamedPropertyInt32(const napi_env& env, const napi_value& object,
51     const std::string& name, int32_t& ret, bool required)
52 {
53     napi_value napiValue = {};
54     if (napi_get_named_property(env, object, name.c_str(), &napiValue) != napi_ok) {
55         MMI_HILOGE("Call napi_get_named_property failed");
56         return RET_ERR;
57     }
58     if (napiValue == nullptr) {
59         MMI_HILOGE("The value is null");
60         if (required) {
61             THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid KeyEvent");
62         }
63         return RET_ERR;
64     }
65     napi_valuetype tmpType = napi_undefined;
66     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
67         MMI_HILOGE("Call napi_typeof failed");
68         return RET_ERR;
69     }
70     if (tmpType != napi_number) {
71         MMI_HILOGE("The value is not int32_t");
72         if (required) {
73             THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid KeyEvent");
74         }
75         return RET_ERR;
76     }
77     if (napi_get_value_int32(env, napiValue, &ret) != napi_ok) {
78         MMI_HILOGE("NapiElement get int32 value failed");
79         return RET_ERR;
80     }
81     return RET_OK;
82 }
83 
GetNamedPropertyInt64(const napi_env & env,const napi_value & object,const std::string & name,int64_t & ret)84 int32_t GetNamedPropertyInt64(const napi_env& env, const napi_value& object, const std::string& name, int64_t& ret)
85 {
86     napi_value napiValue = {};
87     if (napi_get_named_property(env, object, name.c_str(), &napiValue) != napi_ok) {
88         MMI_HILOGE("Call napi_get_named_property failed");
89         return RET_ERR;
90     }
91     if (napiValue == nullptr) {
92         MMI_HILOGE("The value is null");
93         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid KeyEvent");
94         return RET_ERR;
95     }
96     napi_valuetype tmpType = napi_undefined;
97     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
98         MMI_HILOGE("Call napi_typeof failed");
99         return RET_ERR;
100     }
101     if (tmpType != napi_number) {
102         MMI_HILOGE("The value is not int64_t");
103         THROWERR_API9(env, COMMON_PARAMETER_ERROR, name.c_str(), "int");
104         return RET_ERR;
105     }
106     if (napi_get_value_int64(env, napiValue, &ret) != napi_ok) {
107         MMI_HILOGE("NapiElement get int64 value failed");
108         return RET_ERR;
109     }
110     return RET_OK;
111 }
112 
GetNamedPropertyDouble(const napi_env & env,const napi_value & object,const std::string & name,double & ret)113 int32_t GetNamedPropertyDouble(const napi_env& env, const napi_value& object, const std::string& name, double& ret)
114 {
115     napi_value napiValue = {};
116     if (napi_get_named_property(env, object, name.c_str(), &napiValue) != napi_ok) {
117         MMI_HILOGE("Call napi_get_named_property failed");
118         return RET_ERR;
119     }
120     if (napiValue == nullptr) {
121         MMI_HILOGE("The value is null");
122         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid value");
123         return RET_ERR;
124     }
125     napi_valuetype tmpType = napi_undefined;
126     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
127         MMI_HILOGE("Call napi_typeof failed");
128         return RET_ERR;
129     }
130     if (tmpType != napi_number) {
131         MMI_HILOGE("The value is not double");
132         THROWERR_API9(env, COMMON_PARAMETER_ERROR, name.c_str(), "double");
133         return RET_ERR;
134     }
135     if (napi_get_value_double(env, napiValue, &ret) != napi_ok) {
136         MMI_HILOGE("NapiElement get double value failed");
137         return RET_ERR;
138     }
139     return RET_OK;
140 }
141 } // namespace MMI
142 } // namespace OHOS
143