|
9 | 9 | #include <memory>
|
10 | 10 | #include <type_traits>
|
11 | 11 | #include <unordered_map>
|
| 12 | +#include <array> |
12 | 13 |
|
13 | 14 | namespace margelo {
|
14 | 15 |
|
@@ -102,24 +103,28 @@ template <typename TInner> struct JSIConverter<std::optional<TInner>> {
|
102 | 103 | template <typename ReturnType, typename... Args>
|
103 | 104 | struct JSIConverter<std::function<ReturnType(Args...)>> {
|
104 | 105 | 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 | + } |
108 | 114 | };
|
109 | 115 | }
|
110 | 116 |
|
111 | 117 | 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...>) { |
113 | 119 | ReturnType result = function(JSIConverter<Args>::fromJSI(runtime, args[Is])...);
|
114 | 120 | return JSIConverter<ReturnType>::toJSI(runtime, result);
|
115 | 121 | }
|
116 |
| - |
117 | 122 | static jsi::Value toJSI(jsi::Runtime& runtime, std::function<ReturnType(Args...)> function) {
|
118 | 123 | jsi::HostFunctionType jsFunction = [function = std::move(function)] (jsi::Runtime& runtime,
|
119 | 124 | const jsi::Value& thisValue,
|
120 |
| - jsi::Value* args, |
| 125 | + const jsi::Value* args, |
121 | 126 | size_t count) -> jsi::Value {
|
122 |
| - callFunction(function, args, std::index_sequence_for<Args...>{}); |
| 127 | + callHybridFunction(function, runtime, args, std::index_sequence_for<Args...>{}); |
123 | 128 | };
|
124 | 129 | return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "hostFunction"), sizeof...(Args), jsFunction);
|
125 | 130 | }
|
|
0 commit comments