-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathModelHostObject.h
More file actions
97 lines (85 loc) · 3.62 KB
/
ModelHostObject.h
File metadata and controls
97 lines (85 loc) · 3.62 KB
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
#pragma once
#include <cstdio>
#include <string>
#include <tuple>
#include <vector>
#include <ReactCommon/CallInvoker.h>
#include <rnexecutorch/Log.h>
#include <rnexecutorch/host_objects/JsiConversions.h>
#include <rnexecutorch/jsi/JsiHostObject.h>
#include <rnexecutorch/jsi/Promise.h>
namespace rnexecutorch {
template <typename Model> class ModelHostObject : public JsiHostObject {
public:
explicit ModelHostObject(const std::shared_ptr<Model> &model,
std::shared_ptr<react::CallInvoker> callInvoker)
: model(model), callInvoker(callInvoker) {
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>, forward));
}
JSI_HOST_FUNCTION(forward) {
auto promise = Promise::createPromise(
runtime, callInvoker,
[this, count, args, &runtime](std::shared_ptr<Promise> promise) {
constexpr std::size_t forwardArgCount =
jsiconversion::getArgumentCount(&Model::forward);
if (forwardArgCount != count) {
char errorMessage[100];
std::snprintf(
errorMessage, sizeof(errorMessage),
"Argument count mismatch, was expecting: %zu but got: %zu",
forwardArgCount, count);
promise->reject(errorMessage);
return;
}
try {
auto argsConverted = jsiconversion::createArgsTupleFromJsi(
&Model::forward, args, runtime);
// We need to dispatch a thread if we want the forward to be
// asynchronous. In this thread all accesses to jsi::Runtime need to
// be done via the callInvoker.
std::thread([this, promise,
argsConverted = std::move(argsConverted)]() {
try {
auto result = std::apply(
std::bind_front(&Model::forward, model), argsConverted);
callInvoker->invokeAsync([promise, result = std::move(result)](
jsi::Runtime &runtime) {
promise->resolve(
jsiconversion::getJsiValue(std::move(result), runtime));
});
} catch (const std::runtime_error &e) {
// This catch should be merged with the next two
// (std::runtime_error and jsi::JSError inherits from
// std::exception) HOWEVER react native has broken RTTI which
// breaks proper exception type checking. Remove when the
// following change is present in our version:
// https://github.com/facebook/react-native/commit/3132cc88dd46f95898a756456bebeeb6c248f20e
callInvoker->invokeAsync(
[&e, promise]() { promise->reject(e.what()); });
return;
} catch (const jsi::JSError &e) {
callInvoker->invokeAsync(
[&e, promise]() { promise->reject(e.what()); });
return;
} catch (const std::exception &e) {
callInvoker->invokeAsync(
[&e, promise]() { promise->reject(e.what()); });
return;
} catch (...) {
callInvoker->invokeAsync(
[promise]() { promise->reject("Unknown error"); });
return;
}
}).detach();
} catch (...) {
promise->reject(
"Couldn't parse JS arguments in native forward function");
}
});
return promise;
}
private:
std::shared_ptr<Model> model;
std::shared_ptr<react::CallInvoker> callInvoker;
};
} // namespace rnexecutorch