Skip to content

Commit b8bad78

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Support bridging for Class methods return types
Summary: Changelog: [General][Added] - Added support for bridging Class methods return types Previously, this wouldn't work, unless you define your C++ implementation of the TM to have primitive return type that can be converted to JavaScript's type. Differential Revision: D74478572
1 parent 775c16d commit b8bad78

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

packages/react-native/ReactCommon/react/bridging/Class.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ T callFromJs(
4141
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...);
4242
return jsi::Value();
4343

44-
} else if constexpr (is_jsi_v<T>) {
44+
} else if constexpr (is_jsi_v<T> || supportsToJs<R, T>) {
4545
static_assert(supportsToJs<R, T>, "Incompatible return type");
4646

4747
return toJs(
4848
rt,
4949
(instance->*method)(
5050
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...),
5151
jsInvoker);
52-
5352
} else if constexpr (is_optional_jsi_v<T>) {
5453
static_assert(
5554
is_optional_v<R>

packages/react-native/ReactCommon/react/bridging/tests/ClassTest.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,62 @@ TEST_F(BridgingTest, callFromJsTest) {
9292
EXPECT_TRUE(called);
9393
}
9494

95+
struct MethodReturnTypeCastingTestObject {
96+
public:
97+
explicit MethodReturnTypeCastingTestObject(int value) : value_(value) {}
98+
99+
int toInteger() const {
100+
return value_;
101+
}
102+
103+
private:
104+
int value_;
105+
};
106+
107+
template <>
108+
struct Bridging<MethodReturnTypeCastingTestObject> {
109+
static MethodReturnTypeCastingTestObject fromJs(
110+
jsi::Runtime& /*rt*/,
111+
const jsi::Value& value) {
112+
return MethodReturnTypeCastingTestObject(
113+
static_cast<int>(value.asNumber()));
114+
}
115+
116+
static int toJs(
117+
jsi::Runtime& /*rt*/,
118+
const MethodReturnTypeCastingTestObject& value) {
119+
return value.toInteger();
120+
}
121+
};
122+
123+
struct MethodReturnTypeCastingTestClass {
124+
explicit MethodReturnTypeCastingTestClass(
125+
std::shared_ptr<CallInvoker> invoker)
126+
: invoker_(std::move(invoker)) {}
127+
128+
// This is the key, return type is not a primitive, but an object with defined
129+
// bridging template.
130+
MethodReturnTypeCastingTestObject
131+
add(jsi::Runtime& /*unused*/, int a, int b) {
132+
return MethodReturnTypeCastingTestObject(a + b);
133+
}
134+
135+
private:
136+
std::shared_ptr<CallInvoker> invoker_;
137+
};
138+
139+
TEST_F(BridgingTest, methodReturnTypeCastingTest) {
140+
auto instance = MethodReturnTypeCastingTestClass(invoker);
141+
142+
EXPECT_EQ(
143+
2,
144+
bridging::callFromJs<int>(
145+
rt,
146+
&MethodReturnTypeCastingTestClass::add,
147+
invoker,
148+
&instance,
149+
1,
150+
1));
151+
}
152+
95153
} // namespace facebook::react

0 commit comments

Comments
 (0)