-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathJSIConverter.hpp
196 lines (178 loc) · 5.71 KB
/
JSIConverter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Created by Marc Rousavy on 21.02.24.
//
#pragma once
// Forward declare a few of the common types that might have cyclic includes.
namespace margelo::nitro {
template <typename T, typename Enable>
struct JSIConverter;
} // namespace margelo::nitro
#include <jsi/jsi.h>
#include <type_traits>
#include <variant>
namespace margelo::nitro {
using namespace facebook;
/**
* The JSIConverter<T> class can convert any type from and to a jsi::Value.
* It uses templates to statically create fromJSI/toJSI methods, and will throw compile-time errors
* if a given type is not convertable.
* Value types, custom types (HybridObject), and even functions with any number of arguments/types are supported.
* This type can be extended by just creating a new template for JSIConverter in a header.
*/
template <typename T, typename Enable = void>
struct JSIConverter final {
JSIConverter() = delete;
/**
* Converts the given `jsi::Value` to type `T`.
* By default, this static-asserts.
*/
static inline T fromJSI(jsi::Runtime&, const jsi::Value&) {
static_assert(always_false<T>::value, "This type is not supported by the JSIConverter!");
return T();
}
/**
* Converts `T` to a `jsi::Value`.
* By default, this static-asserts.
*/
static inline jsi::Value toJSI(jsi::Runtime&, T) {
static_assert(always_false<T>::value, "This type is not supported by the JSIConverter!");
return jsi::Value::undefined();
}
/**
* Returns whether the given `jsi::Value` can be converted to `T`.
* This involves runtime type-checks.
* By default, this returns `false`.
*/
static inline bool canConvert(jsi::Runtime&, const jsi::Value&) {
return false;
}
private:
template <typename>
struct always_false : std::false_type {};
};
// int <> number
template <>
struct JSIConverter<int> final {
static inline int fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return static_cast<int>(arg.asNumber());
}
static inline jsi::Value toJSI(jsi::Runtime&, int arg) {
return jsi::Value(arg);
}
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
return value.isNumber();
}
};
// std::monostate <> null
template <>
struct JSIConverter<std::monostate> final {
static inline std::monostate fromJSI(jsi::Runtime&, const jsi::Value&) {
return std::monostate();
}
static inline jsi::Value toJSI(jsi::Runtime&, std::monostate) {
return jsi::Value::null();
}
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
return value.isNull() || value.isUndefined();
}
};
// double <> number
template <>
struct JSIConverter<double> final {
static inline double fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return arg.asNumber();
}
static inline jsi::Value toJSI(jsi::Runtime&, double arg) {
return jsi::Value(arg);
}
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
return value.isNumber();
}
};
// float <> number
template <>
struct JSIConverter<float> final {
static inline float fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return static_cast<float>(arg.asNumber());
}
static inline jsi::Value toJSI(jsi::Runtime&, float arg) {
return jsi::Value(static_cast<double>(arg));
}
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
return value.isNumber();
}
};
// int64_t <> BigInt
template <>
struct JSIConverter<int64_t> final {
static inline double fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
return arg.asBigInt(runtime).asInt64(runtime);
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, int64_t arg) {
return jsi::BigInt::fromInt64(runtime, arg);
}
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
if (value.isBigInt()) {
jsi::BigInt bigint = value.getBigInt(runtime);
return bigint.isInt64(runtime);
}
return false;
}
};
// uint64_t <> BigInt
template <>
struct JSIConverter<uint64_t> final {
static inline double fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
return arg.asBigInt(runtime).asUint64(runtime);
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, uint64_t arg) {
return jsi::BigInt::fromUint64(runtime, arg);
}
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
if (value.isBigInt()) {
jsi::BigInt bigint = value.getBigInt(runtime);
return bigint.isUint64(runtime);
}
return false;
}
};
// bool <> boolean
template <>
struct JSIConverter<bool> final {
static inline bool fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return arg.asBool();
}
static inline jsi::Value toJSI(jsi::Runtime&, bool arg) {
return jsi::Value(arg);
}
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
return value.isBool();
}
};
// std::string <> string
template <>
struct JSIConverter<std::string> final {
static inline std::string fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
return arg.asString(runtime).utf8(runtime);
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, const std::string& arg) {
return jsi::String::createFromUtf8(runtime, arg);
}
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
return value.isString();
}
};
} // namespace margelo::nitro
#include "JSIConverter+AnyMap.hpp"
#include "JSIConverter+ArrayBuffer.hpp"
#include "JSIConverter+Exception.hpp"
#include "JSIConverter+Function.hpp"
#include "JSIConverter+Future.hpp"
#include "JSIConverter+HostObject.hpp"
#include "JSIConverter+HybridObject.hpp"
#include "JSIConverter+Optional.hpp"
#include "JSIConverter+Promise.hpp"
#include "JSIConverter+Tuple.hpp"
#include "JSIConverter+UnorderedMap.hpp"
#include "JSIConverter+Variant.hpp"
#include "JSIConverter+Vector.hpp"