Skip to content

Commit 2b64c74

Browse files
committed
fix iOS jsi::Runtime errors
1 parent 2d038ae commit 2b64c74

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

common/rnexecutorch/RnExecutorchInstaller.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <rnexecutorch/host_objects/JsiConversions.h>
44
#include <rnexecutorch/host_objects/ModelHostObject.h>
5-
#include <rnexecutorch/jsi/JsiPromise.h>
65
#include <rnexecutorch/models/StyleTransfer.h>
76

87
namespace rnexecutorch {

common/rnexecutorch/host_objects/ModelHostObject.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,45 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
3434
errorMessage, sizeof(errorMessage),
3535
"Argument count mismatch, was expecting: %zu but got: %zu",
3636
forwardArgCount, count);
37-
38-
promise->reject(errorMessage);
37+
callInvoker->invokeAsync(
38+
[errorMessage, &promise]() { promise->reject(errorMessage); });
3939
return;
4040
}
4141

4242
// We need to dispatch a thread if we want the forward to be
4343
// asynchronous
44-
std::thread([args, &runtime, this, promise = std::move(promise)]() {
44+
std::thread([args, this, promise, &runtime]() {
4545
try {
4646
auto argsConverted = jsiconversion::createArgsTupleFromJsi(
4747
&Model::forward, args, runtime);
4848
auto result = std::apply(std::bind_front(&Model::forward, model),
4949
argsConverted);
50-
51-
promise->resolve(
52-
jsiconversion::getJsiValue(std::move(result), runtime));
50+
callInvoker->invokeAsync(
51+
[promise, result = std::move(result)](jsi::Runtime &runtime) {
52+
promise->resolve(
53+
jsiconversion::getJsiValue(std::move(result), runtime));
54+
});
5355
} catch (const std::runtime_error &e) {
5456
// This catch should be merged with the next two
5557
// (std::runtime_error and jsi::JSError inherits from
5658
// std::exception) HOWEVER react native has broken RTTI which
5759
// breaks proper exception type checking. Remove when the
5860
// following change is present in our version:
5961
// https://github.com/facebook/react-native/commit/3132cc88dd46f95898a756456bebeeb6c248f20e
60-
promise->reject(e.what());
62+
callInvoker->invokeAsync(
63+
[&e, promise]() { promise->reject(e.what()); });
6164
return;
6265
} catch (const jsi::JSError &e) {
63-
promise->reject(e.what());
66+
callInvoker->invokeAsync(
67+
[&e, promise]() { promise->reject(e.what()); });
6468
return;
6569
} catch (const std::exception &e) {
66-
promise->reject(e.what());
70+
callInvoker->invokeAsync(
71+
[&e, promise]() { promise->reject(e.what()); });
6772
return;
6873
} catch (...) {
69-
promise->reject("Unknown error");
74+
callInvoker->invokeAsync(
75+
[promise]() { promise->reject("Unknown error"); });
7076
return;
7177
}
7278
}).detach();

common/rnexecutorch/jsi/Promise.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,12 @@ Promise::Promise(jsi::Runtime &runtime,
99
_resolver(std::move(resolver)), _rejecter(std::move(rejecter)) {}
1010

1111
void Promise::resolve(jsi::Value &&result) {
12-
// invokeAsync takes a std::function which is copyable so we need to wrap
13-
// the jsi::Value which is not.
14-
auto resultPtr = std::make_shared<jsi::Value>(std::move(result));
15-
callInvoker->invokeAsync([resultPtr, this]() -> void {
16-
_resolver.asObject(runtime).asFunction(runtime).call(runtime,
17-
std::move(*resultPtr));
18-
});
12+
_resolver.asObject(runtime).asFunction(runtime).call(runtime, result);
1913
}
2014

2115
void Promise::reject(std::string message) {
22-
callInvoker->invokeAsync([message = std::move(message), this]() -> void {
23-
jsi::JSError error(runtime, message);
24-
_rejecter.asObject(runtime).asFunction(runtime).call(runtime,
25-
error.value());
26-
});
16+
jsi::JSError error(runtime, message);
17+
_rejecter.asObject(runtime).asFunction(runtime).call(runtime, error.value());
2718
}
2819

2920
} // namespace rnexecutorch

0 commit comments

Comments
 (0)