Skip to content

Commit 0035cc9

Browse files
neildharfacebook-github-bot
authored andcommitted
Fix throwing exceptions from asHostObject
Summary: The current implementation of `throwJSError` places it in jsi.cpp, but does not actually export it. This means that when JSI is being provided by a dynamic library, `detail::throwJSError` will not be available. To fix this, move the definition of `throwJSError` into jsi-inl.h, similar to all of the other functions in the `detail` namespace. This uses a roundabout implementation of `throwJSError` in order to avoid directly using `throw`, which would fail to compile when exceptions are turned off. Changelog: [Internal] Reviewed By: jpporto Differential Revision: D36873154 fbshipit-source-id: bbea48e0d4d5fd65d67a029ba12e183128b96322
1 parent a041951 commit 0035cc9

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

ReactCommon/jsi/jsi/jsi-inl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ inline PropNameID&& toPropNameID(Runtime&, PropNameID&& name) {
5656
return std::move(name);
5757
}
5858

59-
void throwJSError(Runtime&, const char* msg);
59+
/// Helper to throw while still compiling with exceptions turned off.
60+
template <typename E, typename... Args>
61+
[[noreturn]] inline void throwOrDie(Args&&... args) {
62+
std::rethrow_exception(
63+
std::make_exception_ptr(E{std::forward<Args>(args)...}));
64+
}
6065

6166
} // namespace detail
6267

@@ -184,7 +189,8 @@ inline std::shared_ptr<T> Object::getHostObject(Runtime& runtime) const {
184189
template <typename T>
185190
inline std::shared_ptr<T> Object::asHostObject(Runtime& runtime) const {
186191
if (!isHostObject<T>(runtime)) {
187-
detail::throwJSError(runtime, "Object is not a HostObject of desired type");
192+
detail::throwOrDie<JSError>(
193+
runtime, "Object is not a HostObject of desired type");
188194
}
189195
return std::static_pointer_cast<T>(runtime.getHostObject(*this));
190196
}

ReactCommon/jsi/jsi/jsi.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ Value callGlobalFunction(Runtime& runtime, const char* name, const Value& arg) {
6262

6363
} // namespace
6464

65-
namespace detail {
66-
67-
void throwJSError(Runtime& rt, const char* msg) {
68-
throw JSError(rt, msg);
69-
}
70-
71-
} // namespace detail
72-
7365
Buffer::~Buffer() = default;
7466

7567
PreparedJavaScript::~PreparedJavaScript() = default;

ReactCommon/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ TEST_F(RuntimeSchedulerTest, handlingError) {
453453
bool didRunTask = false;
454454
auto firstCallback = createHostFunctionFromLambda([this, &didRunTask](bool) {
455455
didRunTask = true;
456-
jsi::detail::throwJSError(*runtime_, "Test error");
456+
throw jsi::JSError(*runtime_, "Test error");
457457
return jsi::Value::undefined();
458458
});
459459

0 commit comments

Comments
 (0)