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 "js_util.h"
17
18#include "string_ex.h"
19
20namespace OHOS {
21namespace MiscServices {
22constexpr int64_t JS_NUMBER_MAX_VALUE = (1LL << 53) - 1;
23napi_valuetype JsUtil::GetType(napi_env env, napi_value in)
24{
25    napi_valuetype valueType = napi_undefined;
26    napi_typeof(env, in, &valueType);
27    return valueType;
28}
29bool JsUtil::GetValue(napi_env env, napi_value in, std::string &out)
30{
31    size_t size = 0;
32    auto status = napi_get_value_string_utf8(env, in, nullptr, 0, &size);
33    if (status != napi_ok) {
34        return false;
35    }
36    out.resize(size + 1, 0);
37    status = napi_get_value_string_utf8(env, in, const_cast<char *>(out.data()), size + 1, &size);
38    out.resize(size);
39    return status == napi_ok;
40}
41
42bool JsUtil::GetValue(napi_env env, napi_value in, std::u16string &out)
43{
44    std::string tempOut;
45    bool ret = GetValue(env, in, tempOut);
46    if (ret) {
47        out = Str8ToStr16(tempOut);
48    }
49    return ret;
50}
51bool JsUtil::GetValue(napi_env env, napi_value in, int32_t &out)
52{
53    return napi_get_value_int32(env, in, &out) == napi_ok;
54}
55bool JsUtil::GetValue(napi_env env, napi_value in, uint32_t &out)
56{
57    return napi_get_value_uint32(env, in, &out) == napi_ok;
58}
59bool JsUtil::GetValue(napi_env env, napi_value in, int64_t &out)
60{
61    return napi_get_value_int64(env, in, &out) == napi_ok;
62}
63bool JsUtil::GetValue(napi_env env, napi_value in, bool &out)
64{
65    return napi_get_value_bool(env, in, &out) == napi_ok;
66}
67bool JsUtil::GetValue(napi_env env, napi_value in, double &out)
68{
69    return napi_get_value_double(env, in, &out) == napi_ok;
70}
71napi_value JsUtil::GetValue(napi_env env, napi_value in)
72{
73    return in;
74}
75napi_value JsUtil::GetValue(napi_env env, const std::string &in)
76{
77    napi_value out = nullptr;
78    napi_create_string_utf8(env, in.c_str(), in.length(), &out);
79    return out;
80}
81napi_value JsUtil::GetValue(napi_env env, int32_t in)
82{
83    napi_value out = nullptr;
84    napi_create_int32(env, in, &out);
85    return out;
86}
87napi_value JsUtil::GetValue(napi_env env, uint32_t in)
88{
89    napi_value out = nullptr;
90    napi_create_uint32(env, in, &out);
91    return out;
92}
93napi_value JsUtil::GetValue(napi_env env, int64_t in)
94{
95    if (in > JS_NUMBER_MAX_VALUE) {
96        // cannot exceed the range of js
97        return nullptr;
98    }
99    napi_value out = nullptr;
100    napi_create_int64(env, in, &out);
101    return out;
102}
103napi_value JsUtil::GetValue(napi_env env, bool in)
104{
105    napi_value out = nullptr;
106    napi_get_boolean(env, in, &out);
107    return out;
108}
109} // namespace MiscServices
110} // namespace OHOS