• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "error_util.h"
17 #include "hilog/log.h"
18 #include "variable_convertor.h"
19 
20 namespace OHOS {
21 namespace Global {
22 namespace I18n {
23 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "VariableConvertor" };
24 using namespace OHOS::HiviewDFX;
25 
CheckNapiValueType(napi_env env,napi_value value)26 bool VariableConvertor::CheckNapiValueType(napi_env env, napi_value value)
27 {
28     if (value != nullptr) {
29         napi_valuetype valueType = napi_valuetype::napi_undefined;
30         napi_typeof(env, value, &valueType);
31         if (valueType != napi_valuetype::napi_undefined && valueType != napi_valuetype::napi_null) {
32             return true;
33         }
34     }
35     return false;
36 }
37 
GetOptionValue(napi_env env,napi_value options,const std::string & optionName,std::string & value)38 void VariableConvertor::GetOptionValue(napi_env env, napi_value options, const std::string &optionName,
39     std::string &value)
40 {
41     napi_value optionValue = nullptr;
42     napi_valuetype type = napi_undefined;
43     napi_status status = napi_typeof(env, options, &type);
44     if (status != napi_ok && type != napi_object) {
45         HiLog::Error(LABEL, "Get option failed, option is not an object");
46         return;
47     }
48     bool hasProperty = false;
49     napi_status propStatus = napi_has_named_property(env, options, optionName.c_str(), &hasProperty);
50     if (propStatus == napi_ok && hasProperty) {
51         status = napi_get_named_property(env, options, optionName.c_str(), &optionValue);
52         if (status == napi_ok) {
53             size_t len;
54             napi_get_value_string_utf8(env, optionValue, nullptr, 0, &len);
55             std::vector<char> optionBuf(len + 1);
56             status = napi_get_value_string_utf8(env, optionValue, optionBuf.data(), len + 1, &len);
57             if (status != napi_ok) {
58                 HiLog::Error(LABEL, "Failed to get string item");
59                 return;
60             }
61             value = optionBuf.data();
62         }
63     }
64 }
65 
GetBoolOptionValue(napi_env env,napi_value & options,const std::string & optionName,bool & boolVal)66 bool VariableConvertor::GetBoolOptionValue(napi_env env, napi_value &options, const std::string &optionName,
67     bool &boolVal)
68 {
69     napi_valuetype type = napi_undefined;
70     napi_status status = napi_typeof(env, options, &type);
71     if (status != napi_ok && type != napi_object) {
72         HiLog::Error(LABEL, "option is not an object");
73         ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
74         return false;
75     }
76     bool hasProperty = false;
77     status = napi_has_named_property(env, options, optionName.c_str(), &hasProperty);
78     if (status != napi_ok || !hasProperty) {
79         HiLog::Info(LABEL, "option don't have property %{public}s", optionName.c_str());
80         return false;
81     }
82     napi_value optionValue = nullptr;
83     status = napi_get_named_property(env, options, optionName.c_str(), &optionValue);
84     if (status != napi_ok) {
85         HiLog::Info(LABEL, "get option %{public}s failed", optionName.c_str());
86         return false;
87     }
88     napi_get_value_bool(env, optionValue, &boolVal);
89     return true;
90 }
91 
GetString(napi_env & env,napi_value & value,int32_t & code)92 std::string VariableConvertor::GetString(napi_env &env, napi_value &value, int32_t &code)
93 {
94     size_t len = 0;
95     napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &len);
96     if (status != napi_ok) {
97         HiLog::Error(LABEL, "Get string failed");
98         code = 1;
99         return "";
100     }
101     std::vector<char> buf(len + 1);
102     status = napi_get_value_string_utf8(env, value, buf.data(), len + 1, &len);
103     if (status != napi_ok) {
104         HiLog::Error(LABEL, "Create string failed");
105         code = 1;
106         return "";
107     }
108     return buf.data();
109 }
110 
GetStringArrayFromJsParam(napi_env env,napi_value & jsArray,std::vector<std::string> & strArray)111 bool VariableConvertor::GetStringArrayFromJsParam(napi_env env, napi_value &jsArray, std::vector<std::string> &strArray)
112 {
113     if (jsArray == nullptr) {
114         HiLog::Error(LABEL, "js string array param not found.");
115         ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
116         return false;
117     }
118     bool isArray = false;
119     napi_status status = napi_is_array(env, jsArray, &isArray);
120     if (status != napi_ok || !isArray) {
121         HiLog::Error(LABEL, "js string array is not an Array.");
122         ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
123         return false;
124     }
125     uint32_t arrayLength = 0;
126     napi_get_array_length(env, jsArray, &arrayLength);
127     napi_value element = nullptr;
128     int32_t code = 0;
129     for (uint32_t i = 0; i < arrayLength; ++i) {
130         napi_get_element(env, jsArray, i, &element);
131         std::string str = GetString(env, element, code);
132         if (code != 0) {
133             HiLog::Error(LABEL, "can't get string from js array param.");
134             ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
135             return false;
136         }
137         strArray.push_back(str);
138     }
139     return true;
140 }
141 
CreateString(napi_env env,const std::string & str)142 napi_value VariableConvertor::CreateString(napi_env env, const std::string &str)
143 {
144     napi_value result;
145     napi_status status = napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &result);
146     if (status != napi_ok) {
147         HiLog::Error(LABEL, "create string js variable failed.");
148         return nullptr;
149     }
150     return result;
151 }
152 
CreateNumber(napi_env env,const int32_t & num)153 napi_value VariableConvertor::CreateNumber(napi_env env, const int32_t &num)
154 {
155     napi_value result;
156     napi_status status = napi_create_int32(env, num, &result);
157     if (status != napi_ok) {
158         HiLog::Error(LABEL, "create number js variable failed.");
159         return nullptr;
160     }
161     return result;
162 }
163 } // namespace I18n
164 } // namespace Global
165 } // namespace OHOS