Skip to content

Commit 295e560

Browse files
legendecasmhdawson
authored andcommitted
test: improve guards for experimental features
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. PR-URL: #545 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
1 parent 2e71842 commit 295e560

File tree

9 files changed

+79
-66
lines changed

9 files changed

+79
-66
lines changed

.travis.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,7 @@ install:
5656
script:
5757
# Travis CI sets NVM_NODEJS_ORG_MIRROR, but it makes node-gyp fail to download headers for nightly builds.
5858
- unset NVM_NODEJS_ORG_MIRROR
59-
- NODEJS_MAJOR_VERSION=$(node -p "process.versions.node.match(/\d+/)[0]")
6059

61-
- |
62-
if [ ${NODEJS_MAJOR_VERSION} -gt 11 ]; then
63-
npm test
64-
else
65-
npm test $NPMOPT --NAPI_VERSION=$(node -p "process.versions.napi")
66-
fi
60+
- npm test
6761
after_success:
6862
- cpp-coveralls --gcov-options '\-lp' --build-root test/build --exclude test

napi-inl.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,10 @@ inline bool Value::IsNumber() const {
383383
return Type() == napi_number;
384384
}
385385

386-
// currently experimental guard with version of NAPI_VERSION that it is
387-
// released in once it is no longer experimental
388-
#if (NAPI_VERSION > 2147483646)
386+
// Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
387+
// Once it is no longer experimental guard with the NAPI_VERSION in which it is
388+
// released instead.
389+
#ifdef NAPI_EXPERIMENTAL
389390
inline bool Value::IsBigInt() const {
390391
return Type() == napi_bigint;
391392
}
@@ -620,9 +621,10 @@ inline double Number::DoubleValue() const {
620621
return result;
621622
}
622623

623-
// currently experimental guard with version of NAPI_VERSION that it is
624-
// released in once it is no longer experimental
625-
#if (NAPI_VERSION > 2147483646)
624+
// Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
625+
// Once it is no longer experimental guard with the NAPI_VERSION in which it is
626+
// released instead.
627+
#ifdef NAPI_EXPERIMENTAL
626628
////////////////////////////////////////////////////////////////////////////////
627629
// BigInt Class
628630
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ namespace Napi {
112112
class Value;
113113
class Boolean;
114114
class Number;
115-
// currently experimental guard with version of NAPI_VERSION that it is
116-
// released in once it is no longer experimental
117-
#if (NAPI_VERSION > 2147483646)
115+
// Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
116+
// Once it is no longer experimental guard with the NAPI_VERSION in which it is
117+
// released instead.
118+
#ifdef NAPI_EXPERIMENTAL
118119
class BigInt;
119120
#endif // NAPI_EXPERIMENTAL
120121
#if (NAPI_VERSION > 4)
@@ -140,9 +141,10 @@ namespace Napi {
140141
typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
141142
typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
142143
typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
143-
// currently experimental guard with version of NAPI_VERSION that it is
144-
// released in once it is no longer experimental
145-
#if (NAPI_VERSION > 2147483646)
144+
// Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
145+
// Once it is no longer experimental guard with the NAPI_VERSION in which it is
146+
// released instead.
147+
#ifdef NAPI_EXPERIMENTAL
146148
typedef TypedArrayOf<int64_t> BigInt64Array; ///< Typed array of signed 64-bit integers
147149
typedef TypedArrayOf<uint64_t> BigUint64Array; ///< Typed array of unsigned 64-bit integers
148150
#endif // NAPI_EXPERIMENTAL
@@ -245,9 +247,10 @@ namespace Napi {
245247
bool IsNull() const; ///< Tests if a value is a null JavaScript value.
246248
bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
247249
bool IsNumber() const; ///< Tests if a value is a JavaScript number.
248-
// currently experimental guard with version of NAPI_VERSION that it is
249-
// released in once it is no longer experimental
250-
#if (NAPI_VERSION > 2147483646)
250+
// Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
251+
// Once it is no longer experimental guard with the NAPI_VERSION in which it is
252+
// released instead.
253+
#ifdef NAPI_EXPERIMENTAL
251254
bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
252255
#endif // NAPI_EXPERIMENTAL
253256
#if (NAPI_VERSION > 4)
@@ -322,9 +325,10 @@ namespace Napi {
322325
double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
323326
};
324327

325-
// currently experimental guard with version of NAPI_VERSION that it is
326-
// released in once it is no longer experimental
327-
#if (NAPI_VERSION > 2147483646)
328+
// Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
329+
// Once it is no longer experimental guard with the NAPI_VERSION in which it is
330+
// released instead.
331+
#ifdef NAPI_EXPERIMENTAL
328332
/// A JavaScript bigint value.
329333
class BigInt : public Value {
330334
public:
@@ -852,9 +856,10 @@ namespace Napi {
852856
: std::is_same<T, uint32_t>::value ? napi_uint32_array
853857
: std::is_same<T, float>::value ? napi_float32_array
854858
: std::is_same<T, double>::value ? napi_float64_array
855-
// currently experimental guard with version of NAPI_VERSION that it is
856-
// released in once it is no longer experimental
857-
#if (NAPI_VERSION > 2147483646)
859+
// Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
860+
// Once it is no longer experimental guard with the NAPI_VERSION in which it is
861+
// released instead.
862+
#ifdef NAPI_EXPERIMENTAL
858863
: std::is_same<T, int64_t>::value ? napi_bigint64_array
859864
: std::is_same<T, uint64_t>::value ? napi_biguint64_array
860865
#endif // NAPI_EXPERIMENTAL

test/bigint.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
2+
// released. Once it is no longer experimental guard with the NAPI_VERSION
3+
// in which it is released instead.
4+
#if (NODE_MAJOR_VERSION >= 10)
5+
16
#define NAPI_EXPERIMENTAL
27
#include "napi.h"
38

49
using namespace Napi;
510

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

1113
Value IsLossless(const CallbackInfo& info) {

test/binding.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define NAPI_EXPERIMENTAL
21
#include "napi.h"
32

43
using namespace Napi;
@@ -14,9 +13,10 @@ Object InitBasicTypesArray(Env env);
1413
Object InitBasicTypesBoolean(Env env);
1514
Object InitBasicTypesNumber(Env env);
1615
Object InitBasicTypesValue(Env env);
17-
// currently experimental guard with version of NAPI_VERSION that it is
18-
// released in once it is no longer experimental
19-
#if (NAPI_VERSION > 2147483646)
16+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
17+
// released. Once it is no longer experimental guard with the NAPI_VERSION
18+
// in which it is released instead.
19+
#if (NODE_MAJOR_VERSION >= 10)
2020
Object InitBigInt(Env env);
2121
#endif
2222
Object InitBuffer(Env env);
@@ -63,9 +63,10 @@ Object Init(Env env, Object exports) {
6363
exports.Set("basic_types_boolean", InitBasicTypesBoolean(env));
6464
exports.Set("basic_types_number", InitBasicTypesNumber(env));
6565
exports.Set("basic_types_value", InitBasicTypesValue(env));
66-
// currently experimental guard with version of NAPI_VERSION that it is
67-
// released in once it is no longer experimental
68-
#if (NAPI_VERSION > 2147483646)
66+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
67+
// released. Once it is no longer experimental guard with the NAPI_VERSION
68+
// in which it is released instead.
69+
#if (NODE_MAJOR_VERSION >= 10)
6970
exports.Set("bigint", InitBigInt(env));
7071
#endif
7172
#if (NAPI_VERSION > 4)

test/binding.gyp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
'variables': {
3-
'NAPI_VERSION%': "",
3+
'NAPI_VERSION%': "<!(node -p \"process.versions.napi\")",
4+
'NODE_MAJOR_VERSION%': "<!(node -p \"process.versions.node.match(/\\d+/)[0]\")",
45
'disable_deprecated': "<!(node -p \"process.env['npm_config_disable_deprecated']\")"
56
},
67
'target_defaults': {
@@ -59,6 +60,7 @@
5960
}
6061
}]
6162
],
63+
'defines': ['NODE_MAJOR_VERSION=<@(NODE_MAJOR_VERSION)'],
6264
'include_dirs': ["<!@(node -p \"require('../').include\")"],
6365
'dependencies': ["<!(node -p \"require('../').gyp\")"],
6466
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],

test/date.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define NAPI_EXPERIMENTAL
21
#include "napi.h"
32

43
using namespace Napi;

test/index.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,38 @@ let testModules = [
5050
'version_management'
5151
];
5252

53-
if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
54-
(process.env.npm_config_NAPI_VERSION < 50000)) {
55-
// currently experimental only test if NAPI_VERSION
56-
// is set to experimental. We can't use C max int
57-
// as that is not supported as a number on earlier
58-
// Node.js versions. Once bigint is in a release
59-
// this should be guarded on the napi version
60-
// in which bigint was added.
53+
const napiVersion = Number(process.versions.napi)
54+
const nodeMajorVersion = Number(process.versions.node.match(/\d+/)[0])
55+
56+
if (nodeMajorVersion < 10) {
57+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
58+
// released. Once it is no longer experimental guard with the NAPI_VERSION
59+
// in which it is released instead.
6160
testModules.splice(testModules.indexOf('bigint'), 1);
6261
testModules.splice(testModules.indexOf('typedarray-bigint'), 1);
6362
}
6463

65-
if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
66-
(process.env.npm_config_NAPI_VERSION < 3)) {
64+
if (napiVersion < 3) {
6765
testModules.splice(testModules.indexOf('callbackscope'), 1);
6866
testModules.splice(testModules.indexOf('version_management'), 1);
6967
}
7068

71-
if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
72-
(process.env.npm_config_NAPI_VERSION < 4)) {
69+
if (napiVersion < 4) {
7370
testModules.splice(testModules.indexOf('asyncprogressworker'), 1);
7471
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_ptr'), 1);
7572
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_sum'), 1);
7673
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_unref'), 1);
7774
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function'), 1);
7875
}
7976

80-
if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
81-
(process.env.npm_config_NAPI_VERSION < 5)) {
77+
if (napiVersion < 5) {
8278
testModules.splice(testModules.indexOf('date'), 1);
8379
}
8480

8581
if (typeof global.gc === 'function') {
82+
console.log(`Testing with N-API Version '${napiVersion}'.`);
83+
console.log(`Testing with Node.js Major Version '${nodeMajorVersion}'.\n`);
84+
8685
console.log('Starting test suite\n');
8786

8887
// Requiring each module runs tests in the module.

test/typedarray.cc

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
2+
// released. Once it is no longer experimental guard with the NAPI_VERSION
3+
// in which it is released instead.
4+
#if (NODE_MAJOR_VERSION >= 10)
15
#define NAPI_EXPERIMENTAL
6+
#endif
27
#include "napi.h"
38

49
using namespace Napi;
@@ -65,9 +70,10 @@ Value CreateTypedArray(const CallbackInfo& info) {
6570
NAPI_TYPEDARRAY_NEW(Float64Array, info.Env(), length, napi_float64_array) :
6671
NAPI_TYPEDARRAY_NEW_BUFFER(Float64Array, info.Env(), length, buffer, bufferOffset,
6772
napi_float64_array);
68-
// currently experimental guard with version of NAPI_VERSION that it is
69-
// released in once it is no longer experimental
70-
#if (NAPI_VERSION > 2147483646)
73+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
74+
// released. Once it is no longer experimental guard with the NAPI_VERSION
75+
// in which it is released instead.
76+
#if (NODE_MAJOR_VERSION >= 10)
7177
} else if (arrayType == "bigint64") {
7278
return buffer.IsUndefined() ?
7379
NAPI_TYPEDARRAY_NEW(BigInt64Array, info.Env(), length, napi_bigint64_array) :
@@ -101,9 +107,10 @@ Value GetTypedArrayType(const CallbackInfo& info) {
101107
case napi_uint32_array: return String::New(info.Env(), "uint32");
102108
case napi_float32_array: return String::New(info.Env(), "float32");
103109
case napi_float64_array: return String::New(info.Env(), "float64");
104-
// currently experimental guard with version of NAPI_VERSION that it is
105-
// released in once it is no longer experimental
106-
#if (NAPI_VERSION > 2147483646)
110+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
111+
// released. Once it is no longer experimental guard with the NAPI_VERSION
112+
// in which it is released instead.
113+
#if (NODE_MAJOR_VERSION >= 10)
107114
case napi_bigint64_array: return String::New(info.Env(), "bigint64");
108115
case napi_biguint64_array: return String::New(info.Env(), "biguint64");
109116
#endif
@@ -143,9 +150,10 @@ Value GetTypedArrayElement(const CallbackInfo& info) {
143150
return Number::New(info.Env(), array.As<Float32Array>()[index]);
144151
case napi_float64_array:
145152
return Number::New(info.Env(), array.As<Float64Array>()[index]);
146-
// currently experimental guard with version of NAPI_VERSION that it is
147-
// released in once it is no longer experimental
148-
#if (NAPI_VERSION > 2147483646)
153+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
154+
// released. Once it is no longer experimental guard with the NAPI_VERSION
155+
// in which it is released instead.
156+
#if (NODE_MAJOR_VERSION >= 10)
149157
case napi_bigint64_array:
150158
return BigInt::New(info.Env(), array.As<BigInt64Array>()[index]);
151159
case napi_biguint64_array:
@@ -189,9 +197,10 @@ void SetTypedArrayElement(const CallbackInfo& info) {
189197
case napi_float64_array:
190198
array.As<Float64Array>()[index] = value.DoubleValue();
191199
break;
192-
// currently experimental guard with version of NAPI_VERSION that it is
193-
// released in once it is no longer experimental
194-
#if (NAPI_VERSION > 2147483646)
200+
// Currently experimental guard with NODE_MAJOR_VERISION in which it was
201+
// released. Once it is no longer experimental guard with the NAPI_VERSION
202+
// in which it is released instead.
203+
#if (NODE_MAJOR_VERSION >= 10)
195204
case napi_bigint64_array: {
196205
bool lossless;
197206
array.As<BigInt64Array>()[index] = value.As<BigInt>().Int64Value(&lossless);

0 commit comments

Comments
 (0)