Skip to content

Commit

Permalink
add Napi::Error::Fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Jun 28, 2021
1 parent a5ca78f commit cd8277a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
33 changes: 32 additions & 1 deletion include/napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,11 @@ inline Error Error::New(napi_env env, const std::string& message) {
return Error::New<Error>(env, message.c_str(), message.size(), napi_create_error);
}

inline NAPI_NO_RETURN void Error::Fatal(const char* location, const char* message) {
// napi_fatal_error(location, NAPI_AUTO_LENGTH, message, NAPI_AUTO_LENGTH);
abort();
}

inline Error::Error() : ObjectReference() {
}

Expand Down Expand Up @@ -2231,12 +2236,38 @@ inline const std::string& Error::Message() const NAPI_NOEXCEPT {
inline void Error::ThrowAsJavaScriptException() const {
HandleScope scope(_env);
if (!IsEmpty()) {

#ifdef NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS
bool pendingException = false;

// check if there is already a pending exception. If so don't try to throw a
// new one as that is not allowed/possible
napi_status status = napi_is_exception_pending(_env, &pendingException);

if ((status != napi_ok) ||
((status == napi_ok) && (pendingException == false))) {
// We intentionally don't use `NAPI_THROW_*` macros here to ensure
// that there is no possible recursion as `ThrowAsJavaScriptException`
// is part of `NAPI_THROW_*` macro definition for noexcept.

status = napi_throw(_env, Value());

if (status == napi_pending_exception) {
// The environment must be terminating as we checked earlier and there
// was no pending exception. In this case continuing will result
// in a fatal error and there is nothing the author has done incorrectly
// in their code that is worth flagging through a fatal error
return;
}
} else {
status = napi_pending_exception;
}
#else
// We intentionally don't use `NAPI_THROW_*` macros here to ensure
// that there is no possible recursion as `ThrowAsJavaScriptException`
// is part of `NAPI_THROW_*` macro definition for noexcept.

napi_status status = napi_throw(_env, Value());
#endif

#ifdef NAPI_CPP_EXCEPTIONS
if (status != napi_ok) {
Expand Down
14 changes: 10 additions & 4 deletions include/napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
NAPI_DISALLOW_ASSIGN(CLASS) \
NAPI_DISALLOW_COPY(CLASS)

#define NAPI_FATAL_IF_FAILED(status, location, message)
#define NAPI_FATAL_IF_FAILED(status, location, message) \
do { \
if ((status) != napi_ok) { \
Napi::Error::Fatal((location), (message)); \
} \
} while (0)

////////////////////////////////////////////////////////////////////////////////
/// Node-API C++ Wrapper Classes
Expand Down Expand Up @@ -1372,6 +1377,8 @@ namespace Napi {
static Error New(napi_env env, const char* message);
static Error New(napi_env env, const std::string& message);

static NAPI_NO_RETURN void Fatal(const char* location, const char* message);

Error();
Error(napi_env env, napi_value value);

Expand Down Expand Up @@ -2303,9 +2310,8 @@ namespace Napi {
Finalizer finalizeCallback,
FinalizerDataType* data = nullptr);

TypedThreadSafeFunction<ContextType, DataType, CallJs>();
TypedThreadSafeFunction<ContextType, DataType, CallJs>(
napi_threadsafe_function tsFunctionValue);
TypedThreadSafeFunction();
TypedThreadSafeFunction(napi_threadsafe_function tsFunctionValue);

operator napi_threadsafe_function() const;

Expand Down
1 change: 1 addition & 0 deletions include/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "js_native_api.h"

#define NAPI_MODULE_EXPORT __attribute__((used))
#define NAPI_NO_RETURN __attribute__((__noreturn__))

typedef napi_value (*napi_addon_register_func)(napi_env env,
napi_value exports);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tybys/emnapi",
"version": "0.3.0",
"version": "0.3.1",
"description": "Node-API implementation for Emscripten",
"main": "index.js",
"typings": "index.d.ts",
Expand Down

0 comments on commit cd8277a

Please sign in to comment.