From 1b562443b079ca9ae25ea88d52f73a73ca2b1471 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Wed, 29 Mar 2017 23:25:37 +0300 Subject: [PATCH] napi: add napi_env to napi_get_last_error_info() 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 https://github.com/nodejs/abi-stable-node/issues/198 --- src/node_api.cc | 3 ++- src/node_api.h | 3 ++- test/addons-napi/3_callbacks/binding.c | 24 +++++++++---------- test/addons-napi/test_buffer/test_buffer.c | 12 +++++----- .../test_conversions/test_conversions.c | 2 +- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 13352c8800..5d8a672e4e 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -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); diff --git a/src/node_api.h b/src/node_api.h index 61372322a9..1e9b7f21f6 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -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); diff --git a/test/addons-napi/3_callbacks/binding.c b/test/addons-napi/3_callbacks/binding.c index 8e96043d5d..8af8da4acc 100644 --- a/test/addons-napi/3_callbacks/binding.c +++ b/test/addons-napi/3_callbacks/binding.c @@ -1,36 +1,36 @@ #include -#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) \ diff --git a/test/addons-napi/test_buffer/test_buffer.c b/test/addons-napi/test_buffer/test_buffer.c index c0c5209abf..6af6ae837a 100644 --- a/test/addons-napi/test_buffer/test_buffer.c +++ b/test/addons-napi/test_buffer/test_buffer.c @@ -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[] = diff --git a/test/addons-napi/test_conversions/test_conversions.c b/test/addons-napi/test_conversions/test_conversions.c index 4902752761..d39ded0531 100644 --- a/test/addons-napi/test_conversions/test_conversions.c +++ b/test/addons-napi/test_conversions/test_conversions.c @@ -1,7 +1,7 @@ #include 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); }