Skip to content

Commit

Permalink
napi: add napi_env to napi_get_last_error_info() (#211)
Browse files Browse the repository at this point in the history
This makes it necessary to pass napi_env to napi_get_last_error_info()
thereby ensuring that errors can be tied to the napi_env in the future.

Re #198
  • Loading branch information
Gabriel "_|Nix|_" Schulhof authored and jasongin committed Mar 29, 2017
1 parent c41c754 commit e5461a5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
28 changes: 16 additions & 12 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,20 @@ void napi_clear_last_error() {
static_last_error.engine_reserved = nullptr;
}

const napi_extended_error_info* napi_get_last_error_info() {
napi_status napi_set_last_error(napi_status error_code,
uint32_t engine_error_code = 0,
void* engine_reserved = nullptr) {
static_last_error.error_code = error_code;
static_last_error.engine_error_code = engine_error_code;
static_last_error.engine_reserved = engine_reserved;

return error_code;
}

napi_status napi_get_last_error_info(napi_env env,
const napi_extended_error_info** result) {
CHECK_ARG(env);

static_assert(node::arraysize(error_messages) == napi_status_last,
"Count of error messages must match count of error values");
assert(static_last_error.error_code < napi_status_last);
Expand All @@ -647,17 +660,8 @@ const napi_extended_error_info* napi_get_last_error_info() {
static_last_error.error_message =
error_messages[static_last_error.error_code];

return &static_last_error;
}

napi_status napi_set_last_error(napi_status error_code,
uint32_t engine_error_code = 0,
void* engine_reserved = nullptr) {
static_last_error.error_code = error_code;
static_last_error.engine_error_code = engine_error_code;
static_last_error.engine_reserved = engine_reserved;

return error_code;
*result = &static_last_error;
return napi_ok;
}

napi_status napi_create_function(napi_env env,
Expand Down
4 changes: 3 additions & 1 deletion src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ EXTERN_C_START

NAPI_EXTERN void napi_module_register(napi_module* mod);

NAPI_EXTERN const napi_extended_error_info* napi_get_last_error_info();
NAPI_EXTERN napi_status
napi_get_last_error_info(napi_env env,
const napi_extended_error_info** result);

// Getters for defined singletons
NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result);
Expand Down
26 changes: 14 additions & 12 deletions test/addons-napi/3_callbacks/binding.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
#include <node_api.h>

#define NAPI_CALL(theCall) \
if ((theCall) != napi_ok) { \
const char* errorMessage = napi_get_last_error_info()->error_message; \
errorMessage = errorMessage ? errorMessage : "empty error message"; \
napi_throw_error((env), errorMessage); \
return; \
#define NAPI_CALL(env, theCall) \
if ((theCall) != napi_ok) { \
const napi_extended_error_info* error; \
napi_get_last_error_info((env), &error); \
const char* errorMessage = error->error_message; \
errorMessage = errorMessage ? errorMessage : "empty error message"; \
napi_throw_error((env), errorMessage); \
return; \
}

void RunCallback(napi_env env, napi_callback_info info) {
napi_value args[1];
NAPI_CALL(napi_get_cb_args(env, info, args, 1));
NAPI_CALL(env, napi_get_cb_args(env, info, args, 1));

napi_value cb = args[0];

napi_value argv[1];
NAPI_CALL(napi_create_string_utf8(env, "hello world", -1, argv));
NAPI_CALL(env, napi_create_string_utf8(env, "hello world", -1, argv));

napi_value global;
NAPI_CALL(napi_get_global(env, &global));
NAPI_CALL(env, napi_get_global(env, &global));

NAPI_CALL(napi_call_function(env, global, cb, 1, argv, NULL));
NAPI_CALL(env, napi_call_function(env, global, cb, 1, argv, NULL));
}

void RunCallbackWithRecv(napi_env env, napi_callback_info info) {
napi_value args[2];
NAPI_CALL(napi_get_cb_args(env, info, args, 2));
NAPI_CALL(env, napi_get_cb_args(env, info, args, 2));

napi_value cb = args[0];
napi_value recv = args[1];

NAPI_CALL(napi_call_function(env, recv, cb, 0, NULL, NULL));
NAPI_CALL(env, napi_call_function(env, recv, cb, 0, NULL, NULL));
}

#define DECLARE_NAPI_METHOD(name, func) \
Expand Down
14 changes: 8 additions & 6 deletions test/addons-napi/test_buffer/test_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
return; \
}

#define NAPI_CALL(env, theCall) \
if ((theCall) != napi_ok) { \
const char* errorMessage = napi_get_last_error_info()->error_message; \
errorMessage = errorMessage ? errorMessage : "empty error message"; \
napi_throw_error((env), errorMessage); \
return; \
#define NAPI_CALL(env, theCall) \
if ((theCall) != napi_ok) { \
const napi_extended_error_info* error; \
napi_get_last_error_info((env), &error); \
const char* errorMessage = error->error_message; \
errorMessage = errorMessage ? errorMessage : "empty error message"; \
napi_throw_error((env), errorMessage); \
return; \
}

static const char theText[] =
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_conversions/test_conversions.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <node_api.h>

void ThrowLastError(napi_env env) {
const napi_extended_error_info* error_info = napi_get_last_error_info();
const napi_extended_error_info* error_info;
napi_get_last_error_info(env, &error_info);
if (error_info->error_code != napi_ok) {
napi_throw_error(env, error_info->error_message);
}
Expand Down

0 comments on commit e5461a5

Please sign in to comment.