Skip to content

Commit

Permalink
NAPI_EXPERIMENTAL mixed with various Node.js Major Version
Browse files Browse the repository at this point in the history
NAPI_EXPERIMENTAL would be expand to NAPI_VERSION=2147483647 if
NAPI_VERSION is not defined. It would be not compatible with various
Node.js versions if we use NAPI_VERSION > 2147483646 mixed with
definition of NAPI_EXPERIMENTAL. This fix introduced NODE_MAJOR_VERSION
to allow more precise control over test sets that which set should be
enabled on a Node.js release.
  • Loading branch information
legendecas committed Sep 18, 2019
1 parent 6192e70 commit a9acd95
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 29 deletions.
1 change: 0 additions & 1 deletion test/basic_types/array.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define NAPI_EXPERIMENTAL
#include "napi.h"

using namespace Napi;
Expand Down
7 changes: 4 additions & 3 deletions test/bigint.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// currently experimental guard with version of NODE_MAJOR_VERSION that it is
// released in once it is no longer experimental
#if (NODE_MAJOR_VERSION >= 10)

#define NAPI_EXPERIMENTAL
#include "napi.h"

using namespace Napi;

// currently experimental guard with version of NAPI_VERSION that it is
// released in once it is no longer experimental
#if (NAPI_VERSION > 2147483646)
namespace {

Value IsLossless(const CallbackInfo& info) {
Expand Down
9 changes: 4 additions & 5 deletions test/binding.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define NAPI_EXPERIMENTAL
#include "napi.h"

using namespace Napi;
Expand All @@ -11,9 +10,9 @@ Object InitBasicTypesArray(Env env);
Object InitBasicTypesBoolean(Env env);
Object InitBasicTypesNumber(Env env);
Object InitBasicTypesValue(Env env);
// currently experimental guard with version of NAPI_VERSION that it is
// currently experimental guard with version of NODE_MAJOR_VERSION that it is
// released in once it is no longer experimental
#if (NAPI_VERSION > 2147483646)
#if (NODE_MAJOR_VERSION >= 10)
Object InitBigInt(Env env);
#endif
Object InitBuffer(Env env);
Expand Down Expand Up @@ -55,9 +54,9 @@ Object Init(Env env, Object exports) {
exports.Set("basic_types_boolean", InitBasicTypesBoolean(env));
exports.Set("basic_types_number", InitBasicTypesNumber(env));
exports.Set("basic_types_value", InitBasicTypesValue(env));
// currently experimental guard with version of NAPI_VERSION that it is
// currently experimental guard with version of NODE_MAJOR_VERSION that it is
// released in once it is no longer experimental
#if (NAPI_VERSION > 2147483646)
#if (NODE_MAJOR_VERSION >= 10)
exports.Set("bigint", InitBigInt(env));
#endif
#if (NAPI_VERSION > 4)
Expand Down
4 changes: 3 additions & 1 deletion test/binding.gyp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
'variables': {
'NAPI_VERSION%': "",
'NAPI_VERSION%': "<!(node -p \"process.versions.napi\")",
'NODE_MAJOR_VERSION%': "<!(node -p \"process.versions.node.match(/\\d+/)[0]\")",
'disable_deprecated': "<!(node -p \"process.env['npm_config_disable_deprecated']\")"
},
'target_defaults': {
Expand Down Expand Up @@ -55,6 +56,7 @@
}
}]
],
'defines': ['NODE_MAJOR_VERSION=<@(NODE_MAJOR_VERSION)'],
'include_dirs': ["<!@(node -p \"require('../').include\")"],
'dependencies': ["<!(node -p \"require('../').gyp\")"],
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
Expand Down
1 change: 0 additions & 1 deletion test/date.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define NAPI_EXPERIMENTAL
#include "napi.h"

using namespace Napi;
Expand Down
22 changes: 12 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ let testModules = [
'version_management'
];

if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
(process.env.npm_config_NAPI_VERSION < 50000)) {
// currently experimental only test if NAPI_VERSION
// is set to experimental. We can't use C max int
const napiVersion = Number(process.versions.napi)
const nodeMajorVersion = Number(process.versions.node.match(/\d+/)[0])

if (nodeMajorVersion < 10) {
// currently experimental only test if node major version
// is set to experimental. We can't use napi_experimental here
// as that is not supported as a number on earlier
// Node.js versions. Once bigint is in a release
// this should be guarded on the napi version
Expand All @@ -58,24 +60,24 @@ if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
testModules.splice(testModules.indexOf('typedarray-bigint'), 1);
}

if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
(process.env.npm_config_NAPI_VERSION < 3)) {
if (napiVersion < 3) {
testModules.splice(testModules.indexOf('callbackscope'), 1);
testModules.splice(testModules.indexOf('version_management'), 1);
}

if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
(process.env.npm_config_NAPI_VERSION < 4)) {
if (napiVersion < 4) {
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_ptr'), 1);
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function'), 1);
}

if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
(process.env.npm_config_NAPI_VERSION < 5)) {
if (napiVersion < 5) {
testModules.splice(testModules.indexOf('date'), 1);
}

if (typeof global.gc === 'function') {
console.log(`Testing with N-API Version '${napiVersion}'.`);
console.log(`Testing with Node.js Major Version '${nodeMajorVersion}'.\n`);

console.log('Starting test suite\n');

// Requiring each module runs tests in the module.
Expand Down
20 changes: 12 additions & 8 deletions test/typedarray.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// currently experimental guard with version of NODE_MAJOR_VERSION that it is
// released in once it is no longer experimental
#if (NODE_MAJOR_VERSION >= 10)
#define NAPI_EXPERIMENTAL
#endif
#include "napi.h"

using namespace Napi;
Expand Down Expand Up @@ -65,9 +69,9 @@ Value CreateTypedArray(const CallbackInfo& info) {
NAPI_TYPEDARRAY_NEW(Float64Array, info.Env(), length, napi_float64_array) :
NAPI_TYPEDARRAY_NEW_BUFFER(Float64Array, info.Env(), length, buffer, bufferOffset,
napi_float64_array);
// currently experimental guard with version of NAPI_VERSION that it is
// currently experimental guard with version of NODE_MAJOR_VERSION that it is
// released in once it is no longer experimental
#if (NAPI_VERSION > 2147483646)
#if (NODE_MAJOR_VERSION >= 10)
} else if (arrayType == "bigint64") {
return buffer.IsUndefined() ?
NAPI_TYPEDARRAY_NEW(BigInt64Array, info.Env(), length, napi_bigint64_array) :
Expand Down Expand Up @@ -101,9 +105,9 @@ Value GetTypedArrayType(const CallbackInfo& info) {
case napi_uint32_array: return String::New(info.Env(), "uint32");
case napi_float32_array: return String::New(info.Env(), "float32");
case napi_float64_array: return String::New(info.Env(), "float64");
// currently experimental guard with version of NAPI_VERSION that it is
// currently experimental guard with version of NODE_MAJOR_VERSION that it is
// released in once it is no longer experimental
#if (NAPI_VERSION > 2147483646)
#if (NODE_MAJOR_VERSION >= 10)
case napi_bigint64_array: return String::New(info.Env(), "bigint64");
case napi_biguint64_array: return String::New(info.Env(), "biguint64");
#endif
Expand Down Expand Up @@ -143,9 +147,9 @@ Value GetTypedArrayElement(const CallbackInfo& info) {
return Number::New(info.Env(), array.As<Float32Array>()[index]);
case napi_float64_array:
return Number::New(info.Env(), array.As<Float64Array>()[index]);
// currently experimental guard with version of NAPI_VERSION that it is
// currently experimental guard with version of NODE_MAJOR_VERSION that it is
// released in once it is no longer experimental
#if (NAPI_VERSION > 2147483646)
#if (NODE_MAJOR_VERSION >= 10)
case napi_bigint64_array:
return BigInt::New(info.Env(), array.As<BigInt64Array>()[index]);
case napi_biguint64_array:
Expand Down Expand Up @@ -189,9 +193,9 @@ void SetTypedArrayElement(const CallbackInfo& info) {
case napi_float64_array:
array.As<Float64Array>()[index] = value.DoubleValue();
break;
// currently experimental guard with version of NAPI_VERSION that it is
// currently experimental guard with version of NODE_MAJOR_VERSION that it is
// released in once it is no longer experimental
#if (NAPI_VERSION > 2147483646)
#if (NODE_MAJOR_VERSION >= 10)
case napi_bigint64_array: {
bool lossless;
array.As<BigInt64Array>()[index] = value.As<BigInt>().Int64Value(&lossless);
Expand Down

0 comments on commit a9acd95

Please sign in to comment.