From 88e8b7cf83353e575a1e1eb63f81a320002605a4 Mon Sep 17 00:00:00 2001 From: Octavian Soldea Date: Mon, 18 Nov 2019 08:23:59 -0800 Subject: [PATCH] n-api: correct bug in napi_get_last_error napi_get_last_error returns incorrect napi_status. Backport-PR-URL: https://github.com/nodejs/node/pull/30532 PR-URL: https://github.com/nodejs/node/pull/28702 Reviewed-By: James M Snell Reviewed-By: Gabriel Schulhof Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson --- src/node_api.cc | 10 ++++++---- src/node_api_types.h | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index f04a5bbfb930a9..39b4fe8bca595f 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1407,14 +1407,16 @@ napi_status napi_get_last_error_info(napi_env env, CHECK_ENV(env); CHECK_ARG(env, result); - // you must update this assert to reference the last message - // in the napi_status enum each time a new error message is added. + // The value of the constant below must be updated to reference the last + // message in the `napi_status` enum each time a new error message is added. // We don't have a napi_status_last as this would result in an ABI // change each time a message was added. + const int last_status = napi_date_expected; + static_assert( - node::arraysize(error_messages) == napi_date_expected + 1, + node::arraysize(error_messages) == last_status + 1, "Count of error messages must match count of error values"); - CHECK_LE(env->last_error.error_code, napi_callback_scope_mismatch); + CHECK_LE(env->last_error.error_code, last_status); // Wait until someone requests the last error information to fetch the error // message string diff --git a/src/node_api_types.h b/src/node_api_types.h index 0aece04aeef85b..211e4611c2d70b 100644 --- a/src/node_api_types.h +++ b/src/node_api_types.h @@ -84,6 +84,10 @@ typedef enum { napi_bigint_expected, napi_date_expected, } napi_status; +// Note: when adding a new enum value to `napi_status`, please also update +// `const int last_status` in `napi_get_last_error_info()' definition, +// in file js_native_api_v8.cc. Please also update the definition of +// `napi_status` in doc/api/n-api.md to reflect the newly added value(s). #if NAPI_VERSION >= 4 typedef enum {