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