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#ifndef OHOS_JS_UTIL_H
17#define OHOS_JS_UTIL_H
18#include <string>
19#include <vector>
20
21#include "napi/native_api.h"
22#include "napi/native_node_api.h"
23namespace OHOS {
24namespace MiscServices {
25class JsUtil {
26public:
27    static napi_valuetype GetType(napi_env env, napi_value in);
28    // js to native
29    static bool GetValue(napi_env env, napi_value in, std::string &out);
30    static bool GetValue(napi_env env, napi_value in, std::u16string &out);
31    static bool GetValue(napi_env env, napi_value in, int32_t &out);
32    static bool GetValue(napi_env env, napi_value in, uint32_t &out);
33    static bool GetValue(napi_env env, napi_value in, int64_t &out);
34    static bool GetValue(napi_env env, napi_value in, bool &out);
35    static bool GetValue(napi_env env, napi_value in, double &out);
36    template<typename T>
37    static bool GetValue(napi_env env, napi_value in, std::vector<T> &items)
38    {
39        uint32_t len = 0;
40        napi_get_array_length(env, in, &len);
41        items.resize(len);
42        for (uint32_t i = 0; i < len; i++) {
43            napi_value item = nullptr;
44            auto status = napi_get_element(env, in, i, &item);
45            T buff{};
46            if (status != napi_ok || !GetValue(env, item, buff)) {
47                return false;
48            }
49            items[i] = std::move(buff);
50        }
51        return true;
52    }
53
54    // native to js
55    static napi_value GetValue(napi_env env, napi_value in);
56    static napi_value GetValue(napi_env env, const std::string &in);
57    static napi_value GetValue(napi_env env, int32_t in);
58    static napi_value GetValue(napi_env env, uint32_t in);
59    static napi_value GetValue(napi_env env, int64_t in);
60    static napi_value GetValue(napi_env env, bool in);
61    template<typename T>
62    static napi_value GetValue(napi_env env, const std::vector<T> &items)
63    {
64        napi_value array = nullptr;
65        auto status = napi_create_array(env, &array);
66        if (status != napi_ok) {
67            return nullptr;
68        }
69        uint32_t index = 0;
70        for (const T &item : items) {
71            auto itemValue = GetValue(env, item);
72            if (itemValue == nullptr) {
73                return nullptr;
74            }
75            status = napi_set_element(env, array, index++, itemValue);
76            if (status != napi_ok) {
77                return nullptr;
78            }
79        }
80        return array;
81    }
82    class Object {
83    public:
84        template<typename T>
85        static bool WriteProperty(napi_env env, napi_value object, const std::string &property, const T &value)
86        {
87            return napi_set_named_property(env, object, property.c_str(), GetValue(env, value)) == napi_ok;
88        }
89        template<typename T>
90        static bool ReadProperty(napi_env env, napi_value object, const std::string &property, T &value)
91        {
92            napi_value propValue = nullptr;
93            napi_get_named_property(env, object, property.c_str(), &propValue);
94            return GetValue(env, propValue, value);
95        }
96    };
97    class Const {
98    public:
99        static napi_value Null(napi_env env)
100        {
101            napi_value value = nullptr;
102            napi_get_null(env, &value);
103            return value;
104        }
105        static napi_value Undefined(napi_env env)
106        {
107            napi_value value = nullptr;
108            napi_get_undefined(env, &value);
109            return value;
110        }
111    };
112    class ScopeGuard {
113    public:
114        explicit ScopeGuard(napi_env env) : env_(env), scope_(nullptr)
115        {
116            napi_open_handle_scope(env_, &scope_);
117        }
118        ~ScopeGuard()
119        {
120            napi_close_handle_scope(env_, scope_);
121        }
122
123    private:
124        napi_env env_;
125        napi_handle_scope scope_;
126    };
127};
128} // namespace MiscServices
129} // namespace OHOS
130#endif // OHOS_JS_UTIL_H
131