1/* 2 * Copyright (c) 2021 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#include "js_napi_common.h" 16 17namespace ACE { 18namespace NAPI { 19namespace SYSTEM_TEST_NAPI { 20 21bool IsTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType) 22{ 23 HILOG_INFO("%{public}s,called", __func__); 24 napi_valuetype valueType = napi_undefined; 25 26 if (napi_typeof(env, param, &valueType) != napi_ok) { 27 return false; 28 } 29 return valueType == expectType; 30} 31 32bool UnwrapIntValue(napi_env env, napi_value jsValue, int& result) 33{ 34 HILOG_INFO("%{public}s,called", __func__); 35 napi_valuetype jsValueType = napi_undefined; 36 NAPI_CALL_BASE(env, napi_typeof(env, jsValue, &jsValueType), false); 37 if (jsValueType != napi_number) { 38 return false; 39 } 40 int32_t natValue32 = 0; 41 NAPI_CALL_BASE(env, napi_get_value_int32(env, jsValue, &natValue32), false); 42 result = static_cast<int>(natValue32); 43 return true; 44} 45 46napi_value UnwrapStringParam(std::string& str, napi_env env, napi_value argv) 47{ 48 HILOG_INFO("%{public}s,called", __func__); 49 napi_valuetype valueType = napi_valuetype::napi_undefined; 50 51 napi_status rev = napi_typeof(env, argv, &valueType); 52 if (rev != napi_ok) { 53 return nullptr; 54 } 55 56 if (valueType != napi_valuetype::napi_string) { 57 HILOG_INFO("%{public}s called, Parameter type does not match", __func__); 58 return nullptr; 59 } 60 61 size_t len; 62 napi_status status = napi_get_value_string_utf8(env, argv, nullptr, 0, &len); 63 if (status != napi_ok) { 64 HILOG_INFO("%{public}s called, Get locale tag length failed", __func__); 65 return nullptr; 66 } 67 std::vector<char> buf(len + 1); 68 status = napi_get_value_string_utf8(env, argv, buf.data(), len + 1, &len); 69 if (status != napi_ok) { 70 HILOG_INFO("%{public}s called, Get locale tag failed", __func__); 71 return nullptr; 72 } 73 str = std::string(buf.data()); 74 75 napi_value result = nullptr; 76 NAPI_CALL(env, napi_create_int32(env, 1, &result)); 77 return result; 78} 79} // namespace SYSTEM_TEST_NAPI 80} // namespace NAPI 81} // namespace ACE