• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_VALUE_CONVERSIONS_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_VALUE_CONVERSIONS_H
18 
19 #include <string>
20 
21 #include "frameworks/bridge/js_frontend/engine/quickjs/qjs_utils.h"
22 
23 namespace __detail__ {
24 
25 template<typename T>
26 struct _is_signed_int_ {
27     static constexpr bool value =
28         std::is_integral<T>::value && std::is_signed<T>::value && !std::is_same<T, bool>::value;
29 };
30 
31 template<typename T>
32 static constexpr bool is_signed_int_v = _is_signed_int_<T>::value;
33 
34 template<typename T>
fromJSValue(JSValueConst val)35 T fromJSValue(JSValueConst val)
36 {
37     static_assert(!std::is_const_v<T> && !std::is_reference_v<T>, //
38         "Cannot convert values to reference or cv-qualified types!");
39 
40     JSContext* ctx = OHOS::Ace::Framework::QJSContext::Current();
41     if constexpr (is_signed_int_v<T>) {
42         int64_t res;
43         JS_ToInt64(ctx, &res, val);
44         return res;
45     } else if constexpr (std::is_unsigned_v<T>) {
46         uint32_t res;
47         JS_ToUint32(ctx, &res, val);
48         return res;
49     } else if constexpr (std::is_floating_point_v<T>) {
50         double res;
51         JS_ToFloat64(ctx, &res, val);
52         return res;
53     } else if constexpr (std::is_same_v<T, std::string>) {
54         OHOS::Ace::Framework::ScopedString str(val);
55         return str.str();
56     }
57 
58     return T();
59 }
60 
61 template<typename T>
toJSValue(T val)62 JSValue toJSValue(T val)
63 {
64     JSContext* ctx = OHOS::Ace::Framework::QJSContext::Current();
65     if constexpr (is_signed_int_v<T>) {
66         return JS_NewInt64(ctx, val);
67     } else if constexpr (std::is_unsigned_v<T>) {
68         return JS_NewInt64(ctx, val);
69     } else if constexpr (std::is_floating_point_v<T>) {
70         return JS_NewFloat64(ctx, val);
71     } else if constexpr (std::is_same_v<T, std::string>) {
72         return JS_NewStringLen(ctx, val.c_str(), val.size());
73     } else if constexpr (std::is_same_v<T, const char*>) {
74         return JS_NewStringLen(ctx, val, strlen(val));
75     }
76 
77     return JS_ThrowInternalError(ctx, "Conversion failure...");
78 }
79 
80 template<typename... Types>
81 struct TupleConverter {
operatorTupleConverter82     std::tuple<Types...> operator()(JSValueConst* argv)
83     {
84         int index = 0;
85         return {
86             __detail__::fromJSValue<Types>(argv[index++])...,
87         };
88     }
89 };
90 
91 }; // namespace __detail__
92 
93 namespace OHOS::Ace::Framework::QJSValueConvertor {
94 
95 template<typename T>
toQJSValue(T && val)96 inline JSValue toQJSValue(T&& val)
97 {
98     return __detail__::toJSValue(std::forward<T>(val));
99 }
100 
101 } // namespace OHOS::Ace::Framework::QJSValueConvertor
102 
103 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_VALUE_CONVERSIONS_H
104