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
This commit is contained in:
Neil Dhar 2022-06-03 15:04:46 -07:00 коммит произвёл Facebook GitHub Bot
Родитель a04195167b
Коммит 0035cc9292
3 изменённых файлов: 9 добавлений и 11 удалений

Просмотреть файл

@ -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
@ -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));
}

Просмотреть файл

@ -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;

Просмотреть файл

@ -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();
});