From 73fed84ceb51dea3993179885009e86c9630eb12 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Wed, 19 Sep 2018 18:54:28 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node-addon-api/pull/350 Fixes: https://github.com/nodejs/node-addon-api/issues/349 Reviewed-By: Gus Caplan Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Jinho Bang --- napi-inl.h | 8 ++++-- napi.h | 20 +++++++++---- test/bigint.cc | 5 ++++ test/binding.cc | 9 ++++++ test/binding.gyp | 6 ++++ test/index.js | 13 +++++++++ test/typedarray-bigint.js | 59 +++++++++++++++++++++++++++++++++++++++ test/typedarray.cc | 16 +++++++++++ test/typedarray.js | 47 ------------------------------- 9 files changed, 129 insertions(+), 54 deletions(-) create mode 100644 test/typedarray-bigint.js diff --git a/napi-inl.h b/napi-inl.h index c96319843..5846cfb7b 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -298,7 +298,9 @@ inline bool Value::IsNumber() const { return Type() == napi_number; } -#ifdef NAPI_EXPERIMENTAL +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) inline bool Value::IsBigInt() const { return Type() == napi_bigint; } @@ -520,7 +522,9 @@ inline double Number::DoubleValue() const { return result; } -#ifdef NAPI_EXPERIMENTAL +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) //////////////////////////////////////////////////////////////////////////////// // BigInt Class //////////////////////////////////////////////////////////////////////////////// diff --git a/napi.h b/napi.h index 7b0f17f11..8085617f2 100644 --- a/napi.h +++ b/napi.h @@ -52,7 +52,9 @@ namespace Napi { class Value; class Boolean; class Number; -#ifdef NAPI_EXPERIMENTAL +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) class BigInt; #endif // NAPI_EXPERIMENTAL class String; @@ -75,7 +77,9 @@ namespace Napi { typedef TypedArrayOf Uint32Array; ///< Typed-array of unsigned 32-bit integers typedef TypedArrayOf Float32Array; ///< Typed-array of 32-bit floating-point values typedef TypedArrayOf Float64Array; ///< Typed-array of 64-bit floating-point values -#ifdef NAPI_EXPERIMENTAL +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) typedef TypedArrayOf BigInt64Array; ///< Typed array of signed 64-bit integers typedef TypedArrayOf BigUint64Array; ///< Typed array of unsigned 64-bit integers #endif // NAPI_EXPERIMENTAL @@ -178,7 +182,9 @@ namespace Napi { bool IsNull() const; ///< Tests if a value is a null JavaScript value. bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean. bool IsNumber() const; ///< Tests if a value is a JavaScript number. -#ifdef NAPI_EXPERIMENTAL +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint. #endif // NAPI_EXPERIMENTAL bool IsString() const; ///< Tests if a value is a JavaScript string. @@ -250,7 +256,9 @@ namespace Napi { double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value. }; -#ifdef NAPI_EXPERIMENTAL +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) /// A JavaScript bigint value. class BigInt : public Value { public: @@ -754,7 +762,9 @@ namespace Napi { : std::is_same::value ? napi_uint32_array : std::is_same::value ? napi_float32_array : std::is_same::value ? napi_float64_array -#ifdef NAPI_EXPERIMENTAL +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) : std::is_same::value ? napi_bigint64_array : std::is_same::value ? napi_biguint64_array #endif // NAPI_EXPERIMENTAL diff --git a/test/bigint.cc b/test/bigint.cc index 48e44ac18..5d1e36367 100644 --- a/test/bigint.cc +++ b/test/bigint.cc @@ -3,6 +3,9 @@ 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) { @@ -74,3 +77,5 @@ Object InitBigInt(Env env) { return exports; } + +#endif diff --git a/test/binding.cc b/test/binding.cc index 071b75172..c2bd101f3 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -1,3 +1,4 @@ +#define NAPI_EXPERIMENTAL #include "napi.h" using namespace Napi; @@ -7,7 +8,11 @@ Object InitAsyncWorker(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 +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) Object InitBigInt(Env env); +#endif Object InitBuffer(Env env); Object InitDataView(Env env); Object InitDataViewReadWrite(Env env); @@ -30,7 +35,11 @@ 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 +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) exports.Set("bigint", InitBigInt(env)); +#endif exports.Set("buffer", InitBuffer(env)); exports.Set("dataview", InitDataView(env)); exports.Set("dataview_read_write", InitDataView(env)); diff --git a/test/binding.gyp b/test/binding.gyp index ed9c6899b..7bff35e8a 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -1,4 +1,7 @@ { + 'variables': { + 'NAPI_VERSION%': "" + }, 'target_defaults': { 'sources': [ 'arraybuffer.cc', @@ -29,6 +32,9 @@ 'objectreference.cc', 'version_management.cc' ], + 'conditions': [ + ['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ] + ], 'include_dirs': [" { + try { + const length = 4; + const t = binding.typedarray.createTypedArray(type, length); + assert.ok(t instanceof Constructor); + assert.strictEqual(binding.typedarray.getTypedArrayType(t), type); + assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length); + + t[3] = 11n; + assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n); + binding.typedarray.setTypedArrayElement(t, 3, 22n); + assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n); + assert.strictEqual(t[3], 22n); + + const b = binding.typedarray.getTypedArrayBuffer(t); + assert.ok(b instanceof ArrayBuffer); + } catch (e) { + console.log(type, Constructor); + throw e; + } + + try { + const length = 4; + const offset = 8; + const b = new ArrayBuffer(offset + 64 * 4); + + const t = binding.typedarray.createTypedArray(type, length, b, offset); + assert.ok(t instanceof Constructor); + assert.strictEqual(binding.typedarray.getTypedArrayType(t), type); + assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length); + + t[3] = 11n; + assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n); + binding.typedarray.setTypedArrayElement(t, 3, 22n); + assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n); + assert.strictEqual(t[3], 22n); + + assert.strictEqual(binding.typedarray.getTypedArrayBuffer(t), b); + } catch (e) { + console.log(type, Constructor); + throw e; + } + }); + + assert.throws(() => { + binding.typedarray.createInvalidTypedArray(); + }, /Invalid (pointer passed as )?argument/); +} diff --git a/test/typedarray.cc b/test/typedarray.cc index d231f69f6..9b3a1996f 100644 --- a/test/typedarray.cc +++ b/test/typedarray.cc @@ -65,6 +65,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 +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) } else if (arrayType == "bigint64") { return buffer.IsUndefined() ? NAPI_TYPEDARRAY_NEW(BigInt64Array, info.Env(), length, napi_bigint64_array) : @@ -75,6 +78,7 @@ Value CreateTypedArray(const CallbackInfo& info) { NAPI_TYPEDARRAY_NEW(BigUint64Array, info.Env(), length, napi_biguint64_array) : NAPI_TYPEDARRAY_NEW_BUFFER(BigUint64Array, info.Env(), length, buffer, bufferOffset, napi_biguint64_array); +#endif } else { Error::New(info.Env(), "Invalid typed-array type.").ThrowAsJavaScriptException(); return Value(); @@ -97,8 +101,12 @@ 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 +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) case napi_bigint64_array: return String::New(info.Env(), "bigint64"); case napi_biguint64_array: return String::New(info.Env(), "biguint64"); +#endif default: return String::New(info.Env(), "invalid"); } } @@ -135,10 +143,14 @@ Value GetTypedArrayElement(const CallbackInfo& info) { return Number::New(info.Env(), array.As()[index]); case napi_float64_array: return Number::New(info.Env(), array.As()[index]); +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) case napi_bigint64_array: return BigInt::New(info.Env(), array.As()[index]); case napi_biguint64_array: return BigInt::New(info.Env(), array.As()[index]); +#endif default: Error::New(info.Env(), "Invalid typed-array type.").ThrowAsJavaScriptException(); return Value(); @@ -177,6 +189,9 @@ void SetTypedArrayElement(const CallbackInfo& info) { case napi_float64_array: array.As()[index] = value.DoubleValue(); break; +// currently experimental guard with version of NAPI_VERSION that it is +// released in once it is no longer experimental +#if (NAPI_VERSION > 2147483646) case napi_bigint64_array: { bool lossless; array.As()[index] = value.As().Int64Value(&lossless); @@ -187,6 +202,7 @@ void SetTypedArrayElement(const CallbackInfo& info) { array.As()[index] = value.As().Uint64Value(&lossless); break; } +#endif default: Error::New(info.Env(), "Invalid typed-array type.").ThrowAsJavaScriptException(); } diff --git a/test/typedarray.js b/test/typedarray.js index 680cf856e..9aa880c16 100644 --- a/test/typedarray.js +++ b/test/typedarray.js @@ -64,53 +64,6 @@ function test(binding) { } }); - [ - ['bigint64', BigInt64Array], - ['biguint64', BigUint64Array], - ].forEach(([type, Constructor]) => { - try { - const length = 4; - const t = binding.typedarray.createTypedArray(type, length); - assert.ok(t instanceof Constructor); - assert.strictEqual(binding.typedarray.getTypedArrayType(t), type); - assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length); - - t[3] = 11n; - assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n); - binding.typedarray.setTypedArrayElement(t, 3, 22n); - assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n); - assert.strictEqual(t[3], 22n); - - const b = binding.typedarray.getTypedArrayBuffer(t); - assert.ok(b instanceof ArrayBuffer); - } catch (e) { - console.log(type, Constructor); - throw e; - } - - try { - const length = 4; - const offset = 8; - const b = new ArrayBuffer(offset + 64 * 4); - - const t = binding.typedarray.createTypedArray(type, length, b, offset); - assert.ok(t instanceof Constructor); - assert.strictEqual(binding.typedarray.getTypedArrayType(t), type); - assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length); - - t[3] = 11n; - assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n); - binding.typedarray.setTypedArrayElement(t, 3, 22n); - assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n); - assert.strictEqual(t[3], 22n); - - assert.strictEqual(binding.typedarray.getTypedArrayBuffer(t), b); - } catch (e) { - console.log(type, Constructor); - throw e; - } - }); - assert.throws(() => { binding.typedarray.createInvalidTypedArray(); }, /Invalid (pointer passed as )?argument/);