133eb0b6dSopenharmony_ci/*
233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License.
533eb0b6dSopenharmony_ci * You may obtain a copy of the License at
633eb0b6dSopenharmony_ci *
733eb0b6dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
833eb0b6dSopenharmony_ci *
933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and
1333eb0b6dSopenharmony_ci * limitations under the License.
1433eb0b6dSopenharmony_ci */
1533eb0b6dSopenharmony_ci#include "js_napi_common.h"
1633eb0b6dSopenharmony_ci
1733eb0b6dSopenharmony_cinamespace ACE {
1833eb0b6dSopenharmony_cinamespace NAPI {
1933eb0b6dSopenharmony_cinamespace SYSTEM_TEST_NAPI {
2033eb0b6dSopenharmony_ci
2133eb0b6dSopenharmony_cibool IsTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType)
2233eb0b6dSopenharmony_ci{
2333eb0b6dSopenharmony_ci    HILOG_INFO("%{public}s,called", __func__);
2433eb0b6dSopenharmony_ci    napi_valuetype valueType = napi_undefined;
2533eb0b6dSopenharmony_ci
2633eb0b6dSopenharmony_ci    if (napi_typeof(env, param, &valueType) != napi_ok) {
2733eb0b6dSopenharmony_ci        return false;
2833eb0b6dSopenharmony_ci    }
2933eb0b6dSopenharmony_ci    return valueType == expectType;
3033eb0b6dSopenharmony_ci}
3133eb0b6dSopenharmony_ci
3233eb0b6dSopenharmony_cibool UnwrapIntValue(napi_env env, napi_value jsValue, int& result)
3333eb0b6dSopenharmony_ci{
3433eb0b6dSopenharmony_ci    HILOG_INFO("%{public}s,called", __func__);
3533eb0b6dSopenharmony_ci    napi_valuetype jsValueType = napi_undefined;
3633eb0b6dSopenharmony_ci    NAPI_CALL_BASE(env, napi_typeof(env, jsValue, &jsValueType), false);
3733eb0b6dSopenharmony_ci    if (jsValueType != napi_number) {
3833eb0b6dSopenharmony_ci        return false;
3933eb0b6dSopenharmony_ci    }
4033eb0b6dSopenharmony_ci    int32_t natValue32 = 0;
4133eb0b6dSopenharmony_ci    NAPI_CALL_BASE(env, napi_get_value_int32(env, jsValue, &natValue32), false);
4233eb0b6dSopenharmony_ci    result = static_cast<int>(natValue32);
4333eb0b6dSopenharmony_ci    return true;
4433eb0b6dSopenharmony_ci}
4533eb0b6dSopenharmony_ci
4633eb0b6dSopenharmony_cinapi_value UnwrapStringParam(std::string& str, napi_env env, napi_value argv)
4733eb0b6dSopenharmony_ci{
4833eb0b6dSopenharmony_ci    HILOG_INFO("%{public}s,called", __func__);
4933eb0b6dSopenharmony_ci    napi_valuetype valueType = napi_valuetype::napi_undefined;
5033eb0b6dSopenharmony_ci
5133eb0b6dSopenharmony_ci    napi_status rev = napi_typeof(env, argv, &valueType);
5233eb0b6dSopenharmony_ci    if (rev != napi_ok) {
5333eb0b6dSopenharmony_ci        return nullptr;
5433eb0b6dSopenharmony_ci    }
5533eb0b6dSopenharmony_ci
5633eb0b6dSopenharmony_ci    if (valueType != napi_valuetype::napi_string) {
5733eb0b6dSopenharmony_ci        HILOG_INFO("%{public}s called, Parameter type does not match", __func__);
5833eb0b6dSopenharmony_ci        return nullptr;
5933eb0b6dSopenharmony_ci    }
6033eb0b6dSopenharmony_ci
6133eb0b6dSopenharmony_ci    size_t len;
6233eb0b6dSopenharmony_ci    napi_status status = napi_get_value_string_utf8(env, argv, nullptr, 0, &len);
6333eb0b6dSopenharmony_ci    if (status != napi_ok) {
6433eb0b6dSopenharmony_ci        HILOG_INFO("%{public}s called, Get locale tag length failed", __func__);
6533eb0b6dSopenharmony_ci        return nullptr;
6633eb0b6dSopenharmony_ci    }
6733eb0b6dSopenharmony_ci    std::vector<char> buf(len + 1);
6833eb0b6dSopenharmony_ci    status = napi_get_value_string_utf8(env, argv, buf.data(), len + 1, &len);
6933eb0b6dSopenharmony_ci    if (status != napi_ok) {
7033eb0b6dSopenharmony_ci        HILOG_INFO("%{public}s called, Get locale tag failed", __func__);
7133eb0b6dSopenharmony_ci        return nullptr;
7233eb0b6dSopenharmony_ci    }
7333eb0b6dSopenharmony_ci    str = std::string(buf.data());
7433eb0b6dSopenharmony_ci
7533eb0b6dSopenharmony_ci    napi_value result = nullptr;
7633eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_create_int32(env, 1, &result));
7733eb0b6dSopenharmony_ci    return result;
7833eb0b6dSopenharmony_ci}
7933eb0b6dSopenharmony_ci} // namespace SYSTEM_TEST_NAPI
8033eb0b6dSopenharmony_ci} // namespace NAPI
8133eb0b6dSopenharmony_ci} // namespace ACE