Skip to content

Commit

Permalink
src: wrap finalizer callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Jul 30, 2020
1 parent b3cfa55 commit db0af19
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ inline napi_value WrapCallback(Callable callback) {
#endif // NAPI_CPP_EXCEPTIONS
}

// For use in JS to C++ void callback wrappers to catch any Napi::Error
// exceptions and rethrow them as JavaScript exceptions before returning from the
// callback.
template <typename Callable>
inline void WrapVoidCallback(Callable callback) {
#ifdef NAPI_CPP_EXCEPTIONS
try {
callback();
} catch (const Error& e) {
e.ThrowAsJavaScriptException();
}
#else // NAPI_CPP_EXCEPTIONS
// When C++ exceptions are disabled, errors are immediately thrown as JS
// exceptions, so there is no need to catch and rethrow them here.
callback();
#endif // NAPI_CPP_EXCEPTIONS
}

template <typename Callable, typename Return>
struct CallbackData {
static inline
Expand Down Expand Up @@ -120,17 +138,21 @@ struct CallbackData<Callable, void> {
template <typename T, typename Finalizer, typename Hint = void>
struct FinalizeData {
static inline
void Wrapper(napi_env env, void* data, void* finalizeHint) {
FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
finalizeData->callback(Env(env), static_cast<T*>(data));
delete finalizeData;
void Wrapper(napi_env env, void* data, void* finalizeHint) noexcept {
WrapVoidCallback([&] {
FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
finalizeData->callback(Env(env), static_cast<T*>(data));
delete finalizeData;
});
}

static inline
void WrapperWithHint(napi_env env, void* data, void* finalizeHint) {
FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
finalizeData->callback(Env(env), static_cast<T*>(data), finalizeData->hint);
delete finalizeData;
void WrapperWithHint(napi_env env, void* data, void* finalizeHint) noexcept {
WrapVoidCallback([&] {
FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
finalizeData->callback(Env(env), static_cast<T*>(data), finalizeData->hint);
delete finalizeData;
});
}

Finalizer callback;
Expand Down

0 comments on commit db0af19

Please sign in to comment.