From 38e01b7e3bbef31f0f7c8e4256e5d8b7641b6b6c Mon Sep 17 00:00:00 2001 From: NickNaso Date: Fri, 31 Aug 2018 18:36:02 +0200 Subject: [PATCH] src: first pass on adding version management apis PR-URL: https://github.com/nodejs/node-addon-api/pull/325 Reviewed-By: Michael Dawson --- README.md | 1 + doc/version_management.md | 43 ++++++++++++++++++++++++++++++++++++++ napi-inl.h | 18 ++++++++++++++++ napi.h | 7 +++++++ test/binding.cc | 2 ++ test/binding.gyp | 1 + test/index.js | 1 + test/version_management.cc | 27 ++++++++++++++++++++++++ test/version_management.js | 32 ++++++++++++++++++++++++++++ 9 files changed, 132 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 9d4011575..bdb8b9389 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,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..e41663661 --- /dev/null +++ b/doc/version_management.md @@ -0,0 +1,43 @@ +# VersionManagement + +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 + +### 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 +information is stored in the `napi_node_version` structrue that is defined as +shown 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/napi-inl.h b/napi-inl.h index 77f403e0c..813a27adc 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3319,6 +3319,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 3ad095485..e142ee79b 100644 --- a/napi.h +++ b/napi.h @@ -1613,6 +1613,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. diff --git a/test/binding.cc b/test/binding.cc index aa807e7f5..071b75172 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -22,6 +22,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)); @@ -45,6 +46,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 cc75c09f0..3945a8085 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -27,6 +27,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..f52db2f73 --- /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); + +}