From 1fb5c4859a7c775db6929bc3660255929508fb50 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Tue, 20 Jun 2023 10:29:29 +0800 Subject: [PATCH] node-api: rename internal NAPI_VERSION definition --- src/js_native_api_v8.cc | 2 +- src/node_api.cc | 13 +++++++------ src/node_metadata.cc | 2 +- src/node_version.h | 7 ++++--- tools/getnapibuildversion.py | 2 +- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 92f765eb0a99f9..823799a901ad83 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -3167,7 +3167,7 @@ napi_status NAPI_CDECL napi_get_dataview_info(napi_env env, napi_status NAPI_CDECL napi_get_version(napi_env env, uint32_t* result) { CHECK_ENV(env); CHECK_ARG(env, result); - *result = NAPI_VERSION; + *result = NODE_API_SUPPORTED_VERSION_MAX; return napi_clear_last_error(env); } diff --git a/src/node_api.cc b/src/node_api.cc index 7537dc20b2bd82..1f1cfb916a7f1e 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -160,7 +160,7 @@ void ThrowNodeApiVersionError(node::Environment* node_env, error_message += " requires Node-API version "; error_message += std::to_string(module_api_version); error_message += ", but this version of Node.js only supports version "; - error_message += NODE_STRINGIFY(NAPI_VERSION) " add-ons."; + error_message += NODE_STRINGIFY(NODE_API_SUPPORTED_VERSION_MAX) " add-ons."; node_env->ThrowError(error_message.c_str()); } @@ -172,7 +172,7 @@ inline napi_env NewEnv(v8::Local context, // Validate module_api_version. if (module_api_version < NODE_API_DEFAULT_MODULE_API_VERSION) { module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION; - } else if (module_api_version > NAPI_VERSION && + } else if (module_api_version > NODE_API_SUPPORTED_VERSION_MAX && module_api_version != NAPI_VERSION_EXPERIMENTAL) { node::Environment* node_env = node::Environment::GetCurrent(context); CHECK_NOT_NULL(node_env); @@ -673,15 +673,16 @@ node::addon_context_register_func get_node_api_context_register_func( const char* module_name, int32_t module_api_version) { static_assert( - NAPI_VERSION == 9, + NODE_API_SUPPORTED_VERSION_MAX == 9, "New version of Node-API requires adding another else-if statement below " "for the new version and updating this assert condition."); - if (module_api_version <= NODE_API_DEFAULT_MODULE_API_VERSION) { - return node_api_context_register_func; - } else if (module_api_version == 9) { + if (module_api_version == 9) { return node_api_context_register_func<9>; } else if (module_api_version == NAPI_VERSION_EXPERIMENTAL) { return node_api_context_register_func; + } else if (module_api_version >= NODE_API_SUPPORTED_VERSION_MIN && + module_api_version <= NODE_API_DEFAULT_MODULE_API_VERSION) { + return node_api_context_register_func; } else { v8impl::ThrowNodeApiVersionError(node_env, module_name, module_api_version); return nullptr; diff --git a/src/node_metadata.cc b/src/node_metadata.cc index 22546e9de25bdf..112d6a9b7fb56f 100644 --- a/src/node_metadata.cc +++ b/src/node_metadata.cc @@ -82,7 +82,7 @@ Metadata::Versions::Versions() { ares = ARES_VERSION_STR; modules = NODE_STRINGIFY(NODE_MODULE_VERSION); nghttp2 = NGHTTP2_VERSION; - napi = NODE_STRINGIFY(NAPI_VERSION); + napi = NODE_STRINGIFY(NODE_API_SUPPORTED_VERSION_MAX); llhttp = NODE_STRINGIFY(LLHTTP_VERSION_MAJOR) "." diff --git a/src/node_version.h b/src/node_version.h index d477f632fd6cf9..6768e5b1306fde 100644 --- a/src/node_version.h +++ b/src/node_version.h @@ -91,9 +91,10 @@ */ #define NODE_MODULE_VERSION 115 -// The NAPI_VERSION provided by this version of the runtime. This is the version -// which the Node binary being built supports. -#define NAPI_VERSION 9 +// The NAPI_VERSION supported by the runtime. This is the inclusive range of +// versions which the Node.js binary being built supports. +#define NODE_API_SUPPORTED_VERSION_MAX 9 +#define NODE_API_SUPPORTED_VERSION_MIN 1 // Node API modules use NAPI_VERSION 8 by default if it is not explicitly // specified. It must be always 8. diff --git a/tools/getnapibuildversion.py b/tools/getnapibuildversion.py index de1de676d3763e..e110031e8a104d 100644 --- a/tools/getnapibuildversion.py +++ b/tools/getnapibuildversion.py @@ -12,7 +12,7 @@ def get_napi_version(): f = open(napi_version_h) - regex = '^#define NAPI_VERSION' + regex = '^#define NODE_API_SUPPORTED_VERSION_MAX' for line in f: if re.match(regex, line):