Skip to content

Commit

Permalink
Fix throwing exceptions from asHostObject
Browse files Browse the repository at this point in the history
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
  • Loading branch information
neildhar authored and facebook-github-bot committed Jun 3, 2022
1 parent a041951 commit 0035cc9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
10 changes: 8 additions & 2 deletions ReactCommon/jsi/jsi/jsi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ inline PropNameID&& toPropNameID(Runtime&, PropNameID&& name) {
return std::move(name);
}

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

} // namespace detail

Expand Down Expand Up @@ -184,7 +189,8 @@ inline std::shared_ptr<T> Object::getHostObject(Runtime& runtime) const {
template <typename T>
inline std::shared_ptr<T> Object::asHostObject(Runtime& runtime) const {
if (!isHostObject<T>(runtime)) {
detail::throwJSError(runtime, "Object is not a HostObject of desired type");
detail::throwOrDie<JSError>(
runtime, "Object is not a HostObject of desired type");
}
return std::static_pointer_cast<T>(runtime.getHostObject(*this));
}
Expand Down
8 changes: 0 additions & 8 deletions ReactCommon/jsi/jsi/jsi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ Value callGlobalFunction(Runtime& runtime, const char* name, const Value& arg) {

} // namespace

namespace detail {

void throwJSError(Runtime& rt, const char* msg) {
throw JSError(rt, msg);
}

} // namespace detail

Buffer::~Buffer() = default;

PreparedJavaScript::~PreparedJavaScript() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ TEST_F(RuntimeSchedulerTest, handlingError) {
bool didRunTask = false;
auto firstCallback = createHostFunctionFromLambda([this, &didRunTask](bool) {
didRunTask = true;
jsi::detail::throwJSError(*runtime_, "Test error");
throw jsi::JSError(*runtime_, "Test error");
return jsi::Value::undefined();
});

Expand Down

0 comments on commit 0035cc9

Please sign in to comment.