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 "napi_utils.h"
17 #include "stats_log.h"
18 
19 namespace OHOS {
20 namespace PowerMgr {
GetCallbackInfo(napi_env & env,napi_callback_info & info,size_t & argc,napi_value argv[])21 void NapiUtils::GetCallbackInfo(napi_env& env, napi_callback_info& info, size_t& argc, napi_value argv[])
22 {
23     napi_value thisVar = nullptr;
24     void *data = nullptr;
25     if (napi_ok != napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)) {
26         STATS_HILOGW(COMP_FWK, "Failed to get the input parameter");
27     }
28 }
29 
CreateReference(napi_env & env,napi_value & value)30 napi_ref NapiUtils::CreateReference(napi_env& env, napi_value& value)
31 {
32     napi_ref refVal = nullptr;
33     if (napi_ok != napi_create_reference(env, value, 1, &refVal)) {
34         STATS_HILOGW(COMP_FWK, "Failed to create a value reference");
35         return refVal;
36     }
37     return refVal;
38 }
39 
ReleaseReference(napi_env & env,napi_ref & ref)40 void NapiUtils::ReleaseReference(napi_env& env, napi_ref& ref)
41 {
42     if (ref != nullptr) {
43         napi_delete_reference(env, ref);
44         ref = nullptr;
45     }
46 }
47 
CheckValueType(napi_env & env,napi_value & value,napi_valuetype checkType)48 bool NapiUtils::CheckValueType(napi_env& env, napi_value& value, napi_valuetype checkType)
49 {
50     napi_valuetype valueType = napi_undefined;
51     napi_typeof(env, value, &valueType);
52     if (valueType != checkType) {
53         STATS_HILOGW(COMP_FWK, "Parameter type error");
54         return false;
55     }
56     return true;
57 }
58 
SetIntValue(napi_env & env,const std::string fieldStr,const int32_t value,napi_value & result)59 void NapiUtils::SetIntValue(napi_env& env, const std::string fieldStr, const int32_t value, napi_value& result)
60 {
61     napi_value napiValue = nullptr;
62     napi_create_int32(env, value, &napiValue);
63     napi_set_named_property(env, result, fieldStr.c_str(), napiValue);
64 }
65 
SetDoubleValue(napi_env & env,const std::string fieldStr,const double value,napi_value & result)66 void NapiUtils::SetDoubleValue(napi_env& env, const std::string fieldStr, const double value, napi_value& result)
67 {
68     napi_value napiValue = nullptr;
69     napi_create_double(env, value, &napiValue);
70     napi_set_named_property(env, result, fieldStr.c_str(), napiValue);
71 }
72 }
73 }
74