Skip to content

Commit 6f94aae

Browse files
committed
fix: make the host function installation generic (#327)
## Description Make the model host function installation generic, so that you can easily introduce additional methods that resolve with a promise. ### Tested on - [x] iOS - [x] Android ### Related issues #255 ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [x] My changes generate no new warnings
1 parent 64eb709 commit 6f94aae

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

common/rnexecutorch/host_objects/ModelHostObject.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,42 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
1919
explicit ModelHostObject(const std::shared_ptr<Model> &model,
2020
std::shared_ptr<react::CallInvoker> callInvoker)
2121
: model(model), callInvoker(callInvoker) {
22-
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>, forward));
22+
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
23+
promiseHostFunction<&Model::forward>,
24+
"forward"));
2325
}
2426

25-
JSI_HOST_FUNCTION(forward) {
27+
// A generic host function that resolves a promise with a result of a
28+
// function. JSI arguments are converted to the types provided in the function
29+
// signature, and the return value is converted back to JSI before resolving.
30+
template <auto FnPtr> JSI_HOST_FUNCTION(promiseHostFunction) {
2631
auto promise = Promise::createPromise(
2732
runtime, callInvoker,
2833
[this, count, args, &runtime](std::shared_ptr<Promise> promise) {
29-
constexpr std::size_t forwardArgCount =
30-
jsiconversion::getArgumentCount(&Model::forward);
31-
if (forwardArgCount != count) {
34+
constexpr std::size_t functionArgCount =
35+
jsiconversion::getArgumentCount(FnPtr);
36+
if (functionArgCount != count) {
3237
char errorMessage[100];
3338
std::snprintf(
3439
errorMessage, sizeof(errorMessage),
3540
"Argument count mismatch, was expecting: %zu but got: %zu",
36-
forwardArgCount, count);
41+
functionArgCount, count);
3742
promise->reject(errorMessage);
3843
return;
3944
}
4045

4146
try {
42-
auto argsConverted = jsiconversion::createArgsTupleFromJsi(
43-
&Model::forward, args, runtime);
47+
auto argsConverted =
48+
jsiconversion::createArgsTupleFromJsi(FnPtr, args, runtime);
4449

45-
// We need to dispatch a thread if we want the forward to be
50+
// We need to dispatch a thread if we want the function to be
4651
// asynchronous. In this thread all accesses to jsi::Runtime need to
4752
// be done via the callInvoker.
4853
std::thread([this, promise,
4954
argsConverted = std::move(argsConverted)]() {
5055
try {
51-
auto result = std::apply(
52-
std::bind_front(&Model::forward, model), argsConverted);
56+
auto result =
57+
std::apply(std::bind_front(FnPtr, model), argsConverted);
5358

5459
callInvoker->invokeAsync([promise, result = std::move(result)](
5560
jsi::Runtime &runtime) {
@@ -81,8 +86,7 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
8186
}
8287
}).detach();
8388
} catch (...) {
84-
promise->reject(
85-
"Couldn't parse JS arguments in native forward function");
89+
promise->reject("Couldn't parse JS arguments in a native function");
8690
}
8791
});
8892

common/rnexecutorch/jsi/JsiHostObject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
jsi::Value NAME(jsi::Runtime &runtime, const jsi::Value &thisValue, \
1616
const jsi::Value *args, size_t count)
1717

18-
#define JSI_EXPORT_FUNCTION(CLASS, FUNCTION) \
18+
#define JSI_EXPORT_FUNCTION(CLASS, FUNCTION, NAME) \
1919
std::make_pair( \
20-
std::string(#FUNCTION), \
20+
NAME, \
2121
static_cast<jsi::Value (JsiHostObject::*)( \
2222
jsi::Runtime &, const jsi::Value &, const jsi::Value *, size_t)>( \
2323
&CLASS::FUNCTION))

0 commit comments

Comments
 (0)