From 7e4bf097df5021cf10f5d6672ef96f467c4ee7c1 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Fri, 31 Aug 2018 18:36:02 +0200 Subject: [PATCH 1/6] First pass on adding version management utility --- napi-inl.h | 18 ++++++++++++++++++ napi.h | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/napi-inl.h b/napi-inl.h index 46ba2c0b9..bad4a94f9 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3235,6 +3235,24 @@ inline int64_t MemoryManagement::AdjustExternalMemory(Env env, int64_t change_in return result; } +//////////////////////////////////////////////////////////////////////////////// +// Version Management class +//////////////////////////////////////////////////////////////////////////////// + +inline uint32_t VersionManagement::GetNapiVersion(Env env) { + uint32_t result; + napi_status status = napi_get_version(env, &result); + NAPI_THROW_IF_FAILED(env, status, 0); + return result; +} + +inline const napi_node_version* VersionManagement::GetNodeVersion(Env env) { + const napi_node_version* result; + napi_status status = napi_get_node_version(env, &result); + NAPI_THROW_IF_FAILED(env, status, 0); + return result; +} + // These macros shouldn't be useful in user code. #undef NAPI_THROW #undef NAPI_THROW_IF_FAILED diff --git a/napi.h b/napi.h index f9852968b..4d0d524e9 100644 --- a/napi.h +++ b/napi.h @@ -1557,6 +1557,13 @@ namespace Napi { static int64_t AdjustExternalMemory(Env env, int64_t change_in_bytes); }; + // Version management + class VersionManagement { + public: + static uint32_t GetNapiVersion(Env env); + static const napi_node_version* GetNodeVersion(Env env); + }; + } // namespace Napi // Inline implementations of all the above class methods are included here. From dcb167aa475377a339026537ec7d7818bbc9c75c Mon Sep 17 00:00:00 2001 From: NickNaso Date: Mon, 3 Sep 2018 16:45:30 +0200 Subject: [PATCH 2/6] Added test and documentation for version management. --- README.md | 1 + doc/version_management.md | 44 ++++++++++++++++++++++++++++++++++++++ test/binding.cc | 2 ++ test/binding.gyp | 1 + test/index.js | 1 + test/version_management.cc | 27 +++++++++++++++++++++++ test/version_management.js | 32 +++++++++++++++++++++++++++ 7 files changed, 108 insertions(+) create mode 100644 doc/version_management.md create mode 100644 test/version_management.cc create mode 100644 test/version_management.js diff --git a/README.md b/README.md index 700b4db72..e2c0d0ba8 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ still a work in progress as its not yet complete). - [Async Operations](doc/async_operations.md) - [AsyncWorker](doc/async_worker.md) - [Promises](doc/promises.md) + - [Version management](doc/version_management.md) diff --git a/doc/version_management.md b/doc/version_management.md new file mode 100644 index 000000000..acc248cca --- /dev/null +++ b/doc/version_management.md @@ -0,0 +1,44 @@ +# VersionManagement + +The `Napi::VersionManagement` class contains methods that allow to retrieve +informations about the version of N-API and Node.js. It is important take +decisions based on different versions of the system. + +## Methods + +### GetNapiVersion + +Retrieves the highest N-API version supported by Node.js runtime. + +```cpp +static uint32_t GetNapiVersion(Env env); +``` + +- `[in] env`: The environment in which the API is invoked under. + +Returns the highest N-API version supported by Node.js runtime. + +### GetNodeVersion + +Retrives information about Node.js version present on the system. All the +informations are store in the `napi_node_version` structrue that is defined as +repoted below: + +```cpp +typedef struct { + uint32_t major; + uint32_t minor; + uint32_t patch; + const char* release; +} napi_node_version; +```` + +```cpp +static const napi_node_version* GetNodeVersion(Env env); +``` + +- `[in] env`: The environment in which the API is invoked under. + +Returns the structure a pointer to the structure `napi_node_version` populated by +the version information of Node.js runtime. + diff --git a/test/binding.cc b/test/binding.cc index 3b4bb7b0a..70006e90f 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -20,6 +20,7 @@ Object InitPromise(Env env); Object InitTypedArray(Env env); Object InitObjectWrap(Env env); Object InitObjectReference(Env env); +Object InitVersionManagement(Env env); Object Init(Env env, Object exports) { exports.Set("arraybuffer", InitArrayBuffer(env)); @@ -41,6 +42,7 @@ Object Init(Env env, Object exports) { exports.Set("typedarray", InitTypedArray(env)); exports.Set("objectwrap", InitObjectWrap(env)); exports.Set("objectreference", InitObjectReference(env)); + exports.Set("version_management", InitVersionManagement(env)); return exports; } diff --git a/test/binding.gyp b/test/binding.gyp index 698eb45b7..5666fae1c 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -25,6 +25,7 @@ 'typedarray.cc', 'objectwrap.cc', 'objectreference.cc', + 'version_management.cc' ], 'include_dirs': ["major)); + version.Set("minor", Number::New(env, node_version->minor)); + version.Set("patch", Number::New(env, node_version->patch)); + version.Set("release", String::New(env, node_version->release)); + return version; +} + +Object InitVersionManagement(Env env) { + Object exports = Object::New(env); + exports["getNapiVersion"] = Function::New(env, getNapiVersion); + exports["getNodeVersion"] = Function::New(env, getNodeVersion); + return exports; +} diff --git a/test/version_management.js b/test/version_management.js new file mode 100644 index 000000000..b1600c7b4 --- /dev/null +++ b/test/version_management.js @@ -0,0 +1,32 @@ +'use strict'; +const buildType = process.config.target_defaults.default_configuration; +const assert = require('assert'); + +test(require(`./build/${buildType}/binding.node`)); +test(require(`./build/${buildType}/binding_noexcept.node`)); + +function parseVersion() { + const expected = {}; + expected.napi = parseInt(process.versions.napi); + expected.release = process.release.name; + const nodeVersion = process.versions.node.split('.'); + expected.major = parseInt(nodeVersion[0]); + expected.minor = parseInt(nodeVersion[1]); + expected.patch = parseInt(nodeVersion[2]); + return expected; +} + +function test(binding) { + + const expected = parseVersion(); + + const napiVersion = binding.version_management.getNapiVersion(); + assert.strictEqual(napiVersion, expected.napi); + + const nodeVersion = binding.version_management.getNodeVersion(); + assert.strictEqual(nodeVersion.major, expected.major); + assert.strictEqual(nodeVersion.minor, expected.minor); + assert.strictEqual(nodeVersion.patch, expected.patch); + assert.strictEqual(nodeVersion.release, expected.release); + +} From 77310a675cf92646c34d04740703d2211f7197cb Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Thu, 6 Sep 2018 16:46:58 -0400 Subject: [PATCH 3/6] squash: fix some nits --- doc/version_management.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/version_management.md b/doc/version_management.md index acc248cca..f81517a00 100644 --- a/doc/version_management.md +++ b/doc/version_management.md @@ -1,8 +1,8 @@ # VersionManagement -The `Napi::VersionManagement` class contains methods that allow to retrieve -informations about the version of N-API and Node.js. It is important take -decisions based on different versions of the system. +The `Napi::VersionManagement` class contains methods that allow information +to be retrieved about the version of N-API and Node.js. In some cases it is +important to make decisions based on different versions of the system. ## Methods @@ -21,8 +21,8 @@ Returns the highest N-API version supported by Node.js runtime. ### GetNodeVersion Retrives information about Node.js version present on the system. All the -informations are store in the `napi_node_version` structrue that is defined as -repoted below: +information is stored in the `napi_node_version` structrue that is defined as +shown below: ```cpp typedef struct { From 6f6a98e0b0baba7638a7f930eb751c71ce6ef2cd Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 18 Sep 2018 15:36:29 -0400 Subject: [PATCH 4/6] squash: update for latest tightening of warnings --- napi-inl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index bad4a94f9..b1b721332 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3242,14 +3242,14 @@ inline int64_t MemoryManagement::AdjustExternalMemory(Env env, int64_t change_in inline uint32_t VersionManagement::GetNapiVersion(Env env) { uint32_t result; napi_status status = napi_get_version(env, &result); - NAPI_THROW_IF_FAILED(env, status, 0); + NAPI_THROW_IF_FAILED_VOID(env, status); return result; } inline const napi_node_version* VersionManagement::GetNodeVersion(Env env) { const napi_node_version* result; napi_status status = napi_get_node_version(env, &result); - NAPI_THROW_IF_FAILED(env, status, 0); + NAPI_THROW_IF_FAILED_VOID(env, status); return result; } From c3c22e1a0284f958dc29b91568d28302a9cff618 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 18 Sep 2018 15:42:01 -0400 Subject: [PATCH 5/6] squash: revert bad change. --- napi-inl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index b1b721332..a31c416a7 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3242,14 +3242,14 @@ inline int64_t MemoryManagement::AdjustExternalMemory(Env env, int64_t change_in inline uint32_t VersionManagement::GetNapiVersion(Env env) { uint32_t result; napi_status status = napi_get_version(env, &result); - NAPI_THROW_IF_FAILED_VOID(env, status); + NAPI_THROW_IF_FAILED(env, status, 0); return result; } inline const napi_node_version* VersionManagement::GetNodeVersion(Env env) { const napi_node_version* result; napi_status status = napi_get_node_version(env, &result); - NAPI_THROW_IF_FAILED_VOID(env, status); + NAPI_THROW_IF_FAILED_VOID(env, status, 0); return result; } From 423226b486e2b5a1d7ccf7d114b48d83f91e6622 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 18 Sep 2018 15:42:54 -0400 Subject: [PATCH 6/6] squash: revert bad change --- napi-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napi-inl.h b/napi-inl.h index a31c416a7..bad4a94f9 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3249,7 +3249,7 @@ inline uint32_t VersionManagement::GetNapiVersion(Env env) { inline const napi_node_version* VersionManagement::GetNodeVersion(Env env) { const napi_node_version* result; napi_status status = napi_get_node_version(env, &result); - NAPI_THROW_IF_FAILED_VOID(env, status, 0); + NAPI_THROW_IF_FAILED(env, status, 0); return result; }