Skip to content

Commit

Permalink
napi: add napi_env to napi_get_last_error_info()
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 nodejs#198
  • Loading branch information
Gabriel Schulhof authored and jasongin committed Mar 29, 2017
1 parent c9f1143 commit 1b56244
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,8 @@ void napi_clear_last_error() {
static_last_error.engine_reserved = nullptr;
}

const napi_extended_error_info* napi_get_last_error_info() {
const napi_extended_error_info* napi_get_last_error_info(napi_env env) {
(void)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 Down
3 changes: 2 additions & 1 deletion src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ 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 const napi_extended_error_info* napi_get_last_error_info(
napi_env env);

// Getters for defined singletons
NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result);
Expand Down
24 changes: 12 additions & 12 deletions test/addons-napi/3_callbacks/binding.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
#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 char* errorMessage = napi_get_last_error_info((env))->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
12 changes: 6 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,12 @@
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 char* errorMessage = napi_get_last_error_info((env))->error_message; \
errorMessage = errorMessage ? errorMessage : "empty error message"; \
napi_throw_error((env), errorMessage); \
return; \
}

static const char theText[] =
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_conversions/test_conversions.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#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);
if (error_info->error_code != napi_ok) {
napi_throw_error(env, error_info->error_message);
}
Expand Down

0 comments on commit 1b56244

Please sign in to comment.