Skip to content

Commit

Permalink
Add callFromJs bridging API
Browse files Browse the repository at this point in the history
Summary:
This adds `bridging::callFromJs` that can call class instance methods with JSI arguments and will automagically convert types to the types expected by the method, or otherwise will fail to compile. The same type conversion back to JSI applies as well for the return value, if there is one.

This will allow C++ TurboModules to more easily define their interface in terms of C++ types instead of having to interact with JSI directly for everything, though it remains possibles for JSI values to pass through if that's what a given method wants.

Changelog:
Internal

Reviewed By: christophpurrer

Differential Revision: D34780511

fbshipit-source-id: 1f9caadeefa6d4023f679e95f3decc64d156b3f0
  • Loading branch information
appden authored and facebook-github-bot committed Mar 11, 2022
1 parent 30cb78e commit 6697b7b
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions ReactCommon/react/bridging/Bridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <react/bridging/Array.h>
#include <react/bridging/Bool.h>
#include <react/bridging/Class.h>
#include <react/bridging/Error.h>
#include <react/bridging/Function.h>
#include <react/bridging/Number.h>
Expand Down
55 changes: 55 additions & 0 deletions ReactCommon/react/bridging/Class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <react/bridging/Base.h>

namespace facebook::react::bridging {

template <
typename T,
typename C,
typename R,
typename... Args,
typename... JSArgs>
T callFromJs(
jsi::Runtime &rt,
R (C::*method)(jsi::Runtime &, Args...),
const std::shared_ptr<CallInvoker> &jsInvoker,
C *instance,
JSArgs &&...args) {
static_assert(
sizeof...(Args) == sizeof...(JSArgs), "Incorrect arguments length");

if constexpr (std::is_void_v<T>) {
(instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...);

} else if constexpr (std::is_void_v<R>) {
static_assert(
std::is_same_v<T, jsi::Value>,
"Void functions may only return undefined");

(instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...);
return jsi::Value();

} else if constexpr (is_jsi_v<T>) {
return toJs(
rt,
(instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...),
jsInvoker);

} else {
return (instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...);
}
}

} // namespace facebook::react::bridging
97 changes: 97 additions & 0 deletions ReactCommon/react/bridging/tests/ClassTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "BridgingTest.h"

namespace facebook::react {

using namespace std::literals;

struct TestClass {
TestClass(std::shared_ptr<CallInvoker> invoker) : invoker_(invoker) {}

double add(jsi::Runtime &, int a, float b) {
return a + b;
}

jsi::Object getObject(jsi::Runtime &, jsi::Object obj) {
return obj;
}

AsyncPromise<std::string> getPromise(jsi::Runtime &rt, std::string result) {
auto promise = AsyncPromise<std::string>(rt, invoker_);
promise.resolve(result);
return promise;
}

std::string
callFunc(jsi::Runtime &, SyncCallback<std::string(int)> func, int num) {
return func(num);
}

void callAsync(jsi::Runtime &, AsyncCallback<> callback) {
callback();
}

private:
std::shared_ptr<CallInvoker> invoker_;
};

TEST_F(BridgingTest, callFromJsTest) {
auto instance = TestClass(invoker);

EXPECT_EQ(
3.0,
bridging::callFromJs<double>(
rt, &TestClass::add, invoker, &instance, 1, 2.0));

auto object = jsi::Object(rt);

EXPECT_TRUE(jsi::Object::strictEquals(
rt,
object,
bridging::callFromJs<jsi::Object>(
rt, &TestClass::getObject, invoker, &instance, object)));

auto promise = bridging::callFromJs<jsi::Object>(
rt,
&TestClass::getPromise,
invoker,
&instance,
jsi::String::createFromAscii(rt, "hi"));
auto then = promise.getPropertyAsFunction(rt, "then");

std::string result;
then.callWithThis(
rt,
promise,
bridging::toJs(
rt, [&](std::string res) { result = res; }, invoker));

flushQueue();
EXPECT_EQ("hi"s, result);

auto func = function("(num) => String(num)");

EXPECT_EQ(
"1"s,
bridging::callFromJs<jsi::String>(
rt, &TestClass::callFunc, invoker, &instance, func, 1)
.utf8(rt));

bool called = false;
func = bridging::toJs(
rt, [&] { called = true; }, invoker);

bridging::callFromJs<void>(
rt, &TestClass::callAsync, invoker, &instance, func);

flushQueue();
EXPECT_TRUE(called);
}

} // namespace facebook::react

0 comments on commit 6697b7b

Please sign in to comment.