• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef DISTRIBUTEDDATAMGR_APPDATAMGR_JSUTILS_H
17 #define DISTRIBUTEDDATAMGR_APPDATAMGR_JSUTILS_H
18 
19 #include <iostream>
20 #include <map>
21 #include <string>
22 #include <variant>
23 #include <vector>
24 #include <type_traits>
25 
26 #include "napi/native_api.h"
27 #include "napi/native_common.h"
28 #include "napi/native_node_api.h"
29 
30 namespace OHOS {
31 namespace PreferencesJsKit {
32 namespace JSUtils {
33 constexpr int OK = 0;
34 constexpr int ERR = -1;
35 constexpr int EXCEED_MAX_LENGTH = -2;
36 constexpr int32_t DEFAULT_BUF_SIZE = 1024;
37 constexpr int32_t BUF_CACHE_MARGIN = 4;
38 constexpr int32_t ASYNC_RST_SIZE = 2;
39 constexpr int32_t MAX_VALUE_LENGTH = 8 * 1024;
40 constexpr int32_t SYNC_RESULT_ELEMNT_NUM = 2;
41 
42 bool Equals(napi_env env, napi_value value, napi_ref copy);
43 
44 int32_t Convert2NativeValue(napi_env env, napi_value jsValue, bool &output);
45 int32_t Convert2NativeValue(napi_env env, napi_value jsValue, float &output);
46 int32_t Convert2NativeValue(napi_env env, napi_value jsValue, double &output);
47 int32_t Convert2NativeValue(napi_env env, napi_value jsValue, int32_t &output);
48 int32_t Convert2NativeValue(napi_env env, napi_value jsValue, int64_t &output);
49 int32_t Convert2NativeValue(napi_env env, napi_value jsValue, std::string &output);
50 int32_t Convert2NativeValue(napi_env env, napi_value jsValue, std::monostate &value);
51 
52 template<typename T> int32_t Convert2NativeValue(napi_env env, napi_value jsValue, std::vector<T> &value);
53 
54 template<typename... Types>
55 int32_t Convert2NativeValue(napi_env env, napi_value jsValue, std::variant<Types...> &value);
56 
57 napi_value Convert2JSValue(napi_env env, int32_t value);
58 napi_value Convert2JSValue(napi_env env, int64_t value);
59 napi_value Convert2JSValue(napi_env env, uint32_t value);
60 napi_value Convert2JSValue(napi_env env, bool value);
61 napi_value Convert2JSValue(napi_env env, float value);
62 napi_value Convert2JSValue(napi_env env, double value);
63 napi_value Convert2JSValue(napi_env env, const std::string &value);
64 napi_value Convert2JSValue(napi_env env, const std::monostate &value);
65 
66 template<typename T>
67 std::enable_if_t<std::is_class_v<T>, napi_value> Convert2JSValue(napi_env env, const T &value);
68 template<typename T>
69 std::enable_if_t<!std::is_class_v<T>, napi_value> Convert2JSValue(napi_env env, T value);
70 
71 template<typename T> napi_value Convert2JSValue(napi_env env, const std::vector<T> &value);
72 
73 template<typename... Types> napi_value Convert2JSValue(napi_env env, const std::variant<Types...> &value);
74 
GetCPPValue(napi_env env,napi_value jsValue,T & value)75 template<typename T> int32_t GetCPPValue(napi_env env, napi_value jsValue, T &value)
76 {
77     return napi_invalid_arg;
78 }
79 
GetCPPValue(napi_env env,napi_value jsValue,T & value)80 template<typename T, typename First, typename... Types> int32_t GetCPPValue(napi_env env, napi_value jsValue, T &value)
81 {
82     First cValue;
83     auto ret = Convert2NativeValue(env, jsValue, cValue);
84     if (ret == napi_ok) {
85         value = cValue;
86         return ret;
87     }
88     return GetCPPValue<T, Types...>(env, jsValue, value);
89 }
90 
GetJSValue(napi_env env,const T & value)91 template<typename T> napi_value GetJSValue(napi_env env, const T &value)
92 {
93     return nullptr;
94 }
95 
GetJSValue(napi_env env,const T & value)96 template<typename T, typename First, typename... Types> napi_value GetJSValue(napi_env env, const T &value)
97 {
98     auto *val = std::get_if<First>(&value);
99     if (val != nullptr) {
100         return Convert2JSValue(env, *val);
101     }
102     return GetJSValue<T, Types...>(env, value);
103 }
104 } // namespace JSUtils
105 
Convert2NativeValue(napi_env env,napi_value jsValue,std::vector<T> & value)106 template<typename T> int32_t JSUtils::Convert2NativeValue(napi_env env, napi_value jsValue, std::vector<T> &value)
107 {
108     bool isArray = false;
109     napi_is_array(env, jsValue, &isArray);
110     if (!isArray) {
111         return napi_invalid_arg;
112     }
113 
114     uint32_t arrLen = 0;
115     napi_get_array_length(env, jsValue, &arrLen);
116     if (arrLen == 0) {
117         return napi_ok;
118     }
119 
120     for (size_t i = 0; i < arrLen; ++i) {
121         napi_value element;
122         napi_get_element(env, jsValue, i, &element);
123         T item;
124         auto status = Convert2NativeValue(env, element, item);
125         if (status != napi_ok) {
126             return napi_invalid_arg;
127         }
128         value.push_back(std::move(item));
129     }
130     return napi_ok;
131 }
132 
133 template<typename... Types>
Convert2NativeValue(napi_env env,napi_value jsValue,std::variant<Types...> & value)134 int32_t JSUtils::Convert2NativeValue(napi_env env, napi_value jsValue, std::variant<Types...> &value)
135 {
136     return GetCPPValue<decltype(value), Types...>(env, jsValue, value);
137 }
138 
Convert2JSValue(napi_env env,const std::vector<T> & value)139 template<typename T> napi_value JSUtils::Convert2JSValue(napi_env env, const std::vector<T> &value)
140 {
141     napi_value jsValue;
142     napi_status status = napi_create_array_with_length(env, value.size(), &jsValue);
143     if (status != napi_ok) {
144         return nullptr;
145     }
146 
147     for (size_t i = 0; i < value.size(); ++i) {
148         napi_set_element(env, jsValue, i, Convert2JSValue(env, static_cast<const T &>(value[i])));
149     }
150     return jsValue;
151 }
152 
Convert2JSValue(napi_env env,const std::variant<Types...> & value)153 template<typename... Types> napi_value JSUtils::Convert2JSValue(napi_env env, const std::variant<Types...> &value)
154 {
155     return GetJSValue<decltype(value), Types...>(env, value);
156 }
157 } // namespace PreferencesJsKit
158 } // namespace OHOS
159 
160 #endif // DISTRIBUTEDDATAMGR_APPDATAMGR_JSUTILS_H
161