Skip to content

Commit 8c06368

Browse files
committed
Try func converter
1 parent 61fbaf1 commit 8c06368

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

package/cpp/jsi/JSIConverter.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <memory>
1010
#include <type_traits>
1111
#include <unordered_map>
12+
#include <array>
1213

1314
namespace margelo {
1415

@@ -102,24 +103,28 @@ template <typename TInner> struct JSIConverter<std::optional<TInner>> {
102103
template <typename ReturnType, typename... Args>
103104
struct JSIConverter<std::function<ReturnType(Args...)>> {
104105
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...));
106+
jsi::Function function = arg.asObject(runtime).asFunction(runtime);
107+
return [&runtime, function = std::move(function)] (Args... args) -> ReturnType {
108+
jsi::Value result = function.call(runtime, JSIConverter<Args>::toJSI(runtime, args)...);
109+
if constexpr (std::is_same_v<ReturnType, void>) {
110+
return;
111+
} else {
112+
return JSIConverter<ReturnType>::fromJSI(runtime, std::move(result));
113+
}
108114
};
109115
}
110116

111117
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...>) {
118+
static jsi::Value callHybridFunction(const std::function<ReturnType(Args...)>& function, jsi::Runtime& runtime, const jsi::Value* args, std::index_sequence<Is...>) {
113119
ReturnType result = function(JSIConverter<Args>::fromJSI(runtime, args[Is])...);
114120
return JSIConverter<ReturnType>::toJSI(runtime, result);
115121
}
116-
117122
static jsi::Value toJSI(jsi::Runtime& runtime, std::function<ReturnType(Args...)> function) {
118123
jsi::HostFunctionType jsFunction = [function = std::move(function)] (jsi::Runtime& runtime,
119124
const jsi::Value& thisValue,
120-
jsi::Value* args,
125+
const jsi::Value* args,
121126
size_t count) -> jsi::Value {
122-
callFunction(function, args, std::index_sequence_for<Args...>{});
127+
callHybridFunction(function, runtime, args, std::index_sequence_for<Args...>{});
123128
};
124129
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "hostFunction"), sizeof...(Args), jsFunction);
125130
}

0 commit comments

Comments
 (0)