Skip to content

Commit 73fed84

Browse files
committed
test: add ability to control experimental tests
Add the ability to specify a NAPI_VERSION which limits the tests executed to those supported by that version. As an example tests can be built/run as: npm test --NAPI_VERSION=3 PR-URL: #350 Fixes: #349 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Jinho Bang <zino@chromium.org>
1 parent b6e2d92 commit 73fed84

File tree

9 files changed

+129
-54
lines changed

9 files changed

+129
-54
lines changed

napi-inl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ inline bool Value::IsNumber() const {
298298
return Type() == napi_number;
299299
}
300300

301-
#ifdef NAPI_EXPERIMENTAL
301+
// currently experimental guard with version of NAPI_VERSION that it is
302+
// released in once it is no longer experimental
303+
#if (NAPI_VERSION > 2147483646)
302304
inline bool Value::IsBigInt() const {
303305
return Type() == napi_bigint;
304306
}
@@ -520,7 +522,9 @@ inline double Number::DoubleValue() const {
520522
return result;
521523
}
522524

523-
#ifdef NAPI_EXPERIMENTAL
525+
// currently experimental guard with version of NAPI_VERSION that it is
526+
// released in once it is no longer experimental
527+
#if (NAPI_VERSION > 2147483646)
524528
////////////////////////////////////////////////////////////////////////////////
525529
// BigInt Class
526530
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ namespace Napi {
5252
class Value;
5353
class Boolean;
5454
class Number;
55-
#ifdef NAPI_EXPERIMENTAL
55+
// currently experimental guard with version of NAPI_VERSION that it is
56+
// released in once it is no longer experimental
57+
#if (NAPI_VERSION > 2147483646)
5658
class BigInt;
5759
#endif // NAPI_EXPERIMENTAL
5860
class String;
@@ -75,7 +77,9 @@ namespace Napi {
7577
typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
7678
typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
7779
typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
78-
#ifdef NAPI_EXPERIMENTAL
80+
// currently experimental guard with version of NAPI_VERSION that it is
81+
// released in once it is no longer experimental
82+
#if (NAPI_VERSION > 2147483646)
7983
typedef TypedArrayOf<int64_t> BigInt64Array; ///< Typed array of signed 64-bit integers
8084
typedef TypedArrayOf<uint64_t> BigUint64Array; ///< Typed array of unsigned 64-bit integers
8185
#endif // NAPI_EXPERIMENTAL
@@ -178,7 +182,9 @@ namespace Napi {
178182
bool IsNull() const; ///< Tests if a value is a null JavaScript value.
179183
bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
180184
bool IsNumber() const; ///< Tests if a value is a JavaScript number.
181-
#ifdef NAPI_EXPERIMENTAL
185+
// currently experimental guard with version of NAPI_VERSION that it is
186+
// released in once it is no longer experimental
187+
#if (NAPI_VERSION > 2147483646)
182188
bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
183189
#endif // NAPI_EXPERIMENTAL
184190
bool IsString() const; ///< Tests if a value is a JavaScript string.
@@ -250,7 +256,9 @@ namespace Napi {
250256
double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
251257
};
252258

253-
#ifdef NAPI_EXPERIMENTAL
259+
// currently experimental guard with version of NAPI_VERSION that it is
260+
// released in once it is no longer experimental
261+
#if (NAPI_VERSION > 2147483646)
254262
/// A JavaScript bigint value.
255263
class BigInt : public Value {
256264
public:
@@ -754,7 +762,9 @@ namespace Napi {
754762
: std::is_same<T, uint32_t>::value ? napi_uint32_array
755763
: std::is_same<T, float>::value ? napi_float32_array
756764
: std::is_same<T, double>::value ? napi_float64_array
757-
#ifdef NAPI_EXPERIMENTAL
765+
// currently experimental guard with version of NAPI_VERSION that it is
766+
// released in once it is no longer experimental
767+
#if (NAPI_VERSION > 2147483646)
758768
: std::is_same<T, int64_t>::value ? napi_bigint64_array
759769
: std::is_same<T, uint64_t>::value ? napi_biguint64_array
760770
#endif // NAPI_EXPERIMENTAL

test/bigint.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
using namespace Napi;
55

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)
69
namespace {
710

811
Value IsLossless(const CallbackInfo& info) {
@@ -74,3 +77,5 @@ Object InitBigInt(Env env) {
7477

7578
return exports;
7679
}
80+
81+
#endif

test/binding.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define NAPI_EXPERIMENTAL
12
#include "napi.h"
23

34
using namespace Napi;
@@ -7,7 +8,11 @@ Object InitAsyncWorker(Env env);
78
Object InitBasicTypesBoolean(Env env);
89
Object InitBasicTypesNumber(Env env);
910
Object InitBasicTypesValue(Env env);
11+
// currently experimental guard with version of NAPI_VERSION that it is
12+
// released in once it is no longer experimental
13+
#if (NAPI_VERSION > 2147483646)
1014
Object InitBigInt(Env env);
15+
#endif
1116
Object InitBuffer(Env env);
1217
Object InitDataView(Env env);
1318
Object InitDataViewReadWrite(Env env);
@@ -30,7 +35,11 @@ Object Init(Env env, Object exports) {
3035
exports.Set("basic_types_boolean", InitBasicTypesBoolean(env));
3136
exports.Set("basic_types_number", InitBasicTypesNumber(env));
3237
exports.Set("basic_types_value", InitBasicTypesValue(env));
38+
// currently experimental guard with version of NAPI_VERSION that it is
39+
// released in once it is no longer experimental
40+
#if (NAPI_VERSION > 2147483646)
3341
exports.Set("bigint", InitBigInt(env));
42+
#endif
3443
exports.Set("buffer", InitBuffer(env));
3544
exports.Set("dataview", InitDataView(env));
3645
exports.Set("dataview_read_write", InitDataView(env));

test/binding.gyp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
'variables': {
3+
'NAPI_VERSION%': ""
4+
},
25
'target_defaults': {
36
'sources': [
47
'arraybuffer.cc',
@@ -29,6 +32,9 @@
2932
'objectreference.cc',
3033
'version_management.cc'
3134
],
35+
'conditions': [
36+
['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ]
37+
],
3238
'include_dirs': ["<!@(node -p \"require('../').include\")"],
3339
'dependencies': ["<!(node -p \"require('../').gyp\")"],
3440
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],

test/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,24 @@ let testModules = [
3131
'object/set_property',
3232
'promise',
3333
'typedarray',
34+
'typedarray-bigint',
3435
'objectwrap',
3536
'objectreference',
3637
'version_management'
3738
];
3839

40+
if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
41+
(process.env.npm_config_NAPI_VERSION < 50000)) {
42+
// currently experimental only test if NAPI_VERSION
43+
// is set to experimental. We can't use C max int
44+
// as that is not supported as a number on earlier
45+
// Node.js versions. Once bigint is in a release
46+
// this should be guarded on the napi version
47+
// in which bigint was added.
48+
testModules.splice(testModules.indexOf('bigint'), 1);
49+
testModules.splice(testModules.indexOf('typedarray-bigint'), 1);
50+
}
51+
3952
if (typeof global.gc === 'function') {
4053
console.log('Starting test suite\n');
4154

test/typedarray-bigint.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
const buildType = process.config.target_defaults.default_configuration;
3+
const assert = require('assert');
4+
5+
test(require(`./build/${buildType}/binding.node`));
6+
test(require(`./build/${buildType}/binding_noexcept.node`));
7+
8+
function test(binding) {
9+
[
10+
['bigint64', BigInt64Array],
11+
['biguint64', BigUint64Array],
12+
].forEach(([type, Constructor]) => {
13+
try {
14+
const length = 4;
15+
const t = binding.typedarray.createTypedArray(type, length);
16+
assert.ok(t instanceof Constructor);
17+
assert.strictEqual(binding.typedarray.getTypedArrayType(t), type);
18+
assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length);
19+
20+
t[3] = 11n;
21+
assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n);
22+
binding.typedarray.setTypedArrayElement(t, 3, 22n);
23+
assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n);
24+
assert.strictEqual(t[3], 22n);
25+
26+
const b = binding.typedarray.getTypedArrayBuffer(t);
27+
assert.ok(b instanceof ArrayBuffer);
28+
} catch (e) {
29+
console.log(type, Constructor);
30+
throw e;
31+
}
32+
33+
try {
34+
const length = 4;
35+
const offset = 8;
36+
const b = new ArrayBuffer(offset + 64 * 4);
37+
38+
const t = binding.typedarray.createTypedArray(type, length, b, offset);
39+
assert.ok(t instanceof Constructor);
40+
assert.strictEqual(binding.typedarray.getTypedArrayType(t), type);
41+
assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length);
42+
43+
t[3] = 11n;
44+
assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n);
45+
binding.typedarray.setTypedArrayElement(t, 3, 22n);
46+
assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n);
47+
assert.strictEqual(t[3], 22n);
48+
49+
assert.strictEqual(binding.typedarray.getTypedArrayBuffer(t), b);
50+
} catch (e) {
51+
console.log(type, Constructor);
52+
throw e;
53+
}
54+
});
55+
56+
assert.throws(() => {
57+
binding.typedarray.createInvalidTypedArray();
58+
}, /Invalid (pointer passed as )?argument/);
59+
}

test/typedarray.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ Value CreateTypedArray(const CallbackInfo& info) {
6565
NAPI_TYPEDARRAY_NEW(Float64Array, info.Env(), length, napi_float64_array) :
6666
NAPI_TYPEDARRAY_NEW_BUFFER(Float64Array, info.Env(), length, buffer, bufferOffset,
6767
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)
6871
} else if (arrayType == "bigint64") {
6972
return buffer.IsUndefined() ?
7073
NAPI_TYPEDARRAY_NEW(BigInt64Array, info.Env(), length, napi_bigint64_array) :
@@ -75,6 +78,7 @@ Value CreateTypedArray(const CallbackInfo& info) {
7578
NAPI_TYPEDARRAY_NEW(BigUint64Array, info.Env(), length, napi_biguint64_array) :
7679
NAPI_TYPEDARRAY_NEW_BUFFER(BigUint64Array, info.Env(), length, buffer, bufferOffset,
7780
napi_biguint64_array);
81+
#endif
7882
} else {
7983
Error::New(info.Env(), "Invalid typed-array type.").ThrowAsJavaScriptException();
8084
return Value();
@@ -97,8 +101,12 @@ Value GetTypedArrayType(const CallbackInfo& info) {
97101
case napi_uint32_array: return String::New(info.Env(), "uint32");
98102
case napi_float32_array: return String::New(info.Env(), "float32");
99103
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)
100107
case napi_bigint64_array: return String::New(info.Env(), "bigint64");
101108
case napi_biguint64_array: return String::New(info.Env(), "biguint64");
109+
#endif
102110
default: return String::New(info.Env(), "invalid");
103111
}
104112
}
@@ -135,10 +143,14 @@ Value GetTypedArrayElement(const CallbackInfo& info) {
135143
return Number::New(info.Env(), array.As<Float32Array>()[index]);
136144
case napi_float64_array:
137145
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)
138149
case napi_bigint64_array:
139150
return BigInt::New(info.Env(), array.As<BigInt64Array>()[index]);
140151
case napi_biguint64_array:
141152
return BigInt::New(info.Env(), array.As<BigUint64Array>()[index]);
153+
#endif
142154
default:
143155
Error::New(info.Env(), "Invalid typed-array type.").ThrowAsJavaScriptException();
144156
return Value();
@@ -177,6 +189,9 @@ void SetTypedArrayElement(const CallbackInfo& info) {
177189
case napi_float64_array:
178190
array.As<Float64Array>()[index] = value.DoubleValue();
179191
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)
180195
case napi_bigint64_array: {
181196
bool lossless;
182197
array.As<BigInt64Array>()[index] = value.As<BigInt>().Int64Value(&lossless);
@@ -187,6 +202,7 @@ void SetTypedArrayElement(const CallbackInfo& info) {
187202
array.As<BigUint64Array>()[index] = value.As<BigInt>().Uint64Value(&lossless);
188203
break;
189204
}
205+
#endif
190206
default:
191207
Error::New(info.Env(), "Invalid typed-array type.").ThrowAsJavaScriptException();
192208
}

test/typedarray.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -64,53 +64,6 @@ function test(binding) {
6464
}
6565
});
6666

67-
[
68-
['bigint64', BigInt64Array],
69-
['biguint64', BigUint64Array],
70-
].forEach(([type, Constructor]) => {
71-
try {
72-
const length = 4;
73-
const t = binding.typedarray.createTypedArray(type, length);
74-
assert.ok(t instanceof Constructor);
75-
assert.strictEqual(binding.typedarray.getTypedArrayType(t), type);
76-
assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length);
77-
78-
t[3] = 11n;
79-
assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n);
80-
binding.typedarray.setTypedArrayElement(t, 3, 22n);
81-
assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n);
82-
assert.strictEqual(t[3], 22n);
83-
84-
const b = binding.typedarray.getTypedArrayBuffer(t);
85-
assert.ok(b instanceof ArrayBuffer);
86-
} catch (e) {
87-
console.log(type, Constructor);
88-
throw e;
89-
}
90-
91-
try {
92-
const length = 4;
93-
const offset = 8;
94-
const b = new ArrayBuffer(offset + 64 * 4);
95-
96-
const t = binding.typedarray.createTypedArray(type, length, b, offset);
97-
assert.ok(t instanceof Constructor);
98-
assert.strictEqual(binding.typedarray.getTypedArrayType(t), type);
99-
assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length);
100-
101-
t[3] = 11n;
102-
assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n);
103-
binding.typedarray.setTypedArrayElement(t, 3, 22n);
104-
assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n);
105-
assert.strictEqual(t[3], 22n);
106-
107-
assert.strictEqual(binding.typedarray.getTypedArrayBuffer(t), b);
108-
} catch (e) {
109-
console.log(type, Constructor);
110-
throw e;
111-
}
112-
});
113-
11467
assert.throws(() => {
11568
binding.typedarray.createInvalidTypedArray();
11669
}, /Invalid (pointer passed as )?argument/);

0 commit comments

Comments
 (0)