@@ -73,6 +73,15 @@ template <> struct JSIConverter<bool> {
73
73
}
74
74
};
75
75
76
+ template <> struct JSIConverter <std::string> {
77
+ static std::string fromJSI (jsi::Runtime& runtime, const jsi::Value& arg) {
78
+ return arg.asString (runtime).utf8 (runtime);
79
+ }
80
+ static jsi::Value toJSI (jsi::Runtime& runtime, const std::string& arg) {
81
+ return jsi::String::createFromUtf8 (runtime, arg);
82
+ }
83
+ };
84
+
76
85
template <typename TInner> struct JSIConverter <std::optional<TInner>> {
77
86
static std::optional<TInner> fromJSI (jsi::Runtime& runtime, const jsi::Value& arg) {
78
87
if (arg.isUndefined () || arg.isNull ()) {
@@ -90,13 +99,29 @@ template <typename TInner> struct JSIConverter<std::optional<TInner>> {
90
99
}
91
100
};
92
101
93
- template <> struct JSIConverter <std::string> {
94
- static std::string fromJSI (jsi::Runtime& runtime, const jsi::Value& arg) {
95
- return arg.asString (runtime).utf8 (runtime);
96
- }
97
- static jsi::Value toJSI (jsi::Runtime& runtime, const std::string& arg) {
98
- return jsi::String::createFromUtf8 (runtime, arg);
99
- }
102
+ template <typename ReturnType, typename ... Args>
103
+ struct JSIConverter <std::function<ReturnType(Args...)>> {
104
+ static std::function<ReturnType(Args...)> fromJSI (jsi::Runtime& runtime, const jsi::Value& arg) {
105
+ jsi::Function func = arg.asObject (runtime).asFunction (runtime);
106
+ return [&runtime, func = std::move (func)] (Args... args) -> ReturnType {
107
+ func.call (runtime, JSIConverter::toJSI (args...));
108
+ };
109
+ }
110
+
111
+ template <size_t ... Is>
112
+ static jsi::Value callFunction (std::function<ReturnType(Args...)> function, jsi::Runtime& runtime, const jsi::Value* args, std::index_sequence<Is...>) {
113
+ ReturnType result = function (JSIConverter<Args>::fromJSI (runtime, args[Is])...);
114
+ return JSIConverter<ReturnType>::toJSI (runtime, result);
115
+ }
116
+
117
+ static jsi::Value toJSI (jsi::Runtime& runtime, std::function<ReturnType(Args...)> function) {
118
+ jsi::HostFunctionType jsFunction = [function = std::move (function)] (jsi::Runtime& runtime,
119
+ const jsi::Value& thisValue,
120
+ jsi::Value* args,
121
+ size_t count) -> jsi::Value {
122
+ callFunction (function, args, std::index_sequence_for<Args...>{});
123
+ };
124
+ }
100
125
};
101
126
102
127
template <typename ElementType> struct JSIConverter <std::vector<ElementType>> {
0 commit comments