From 295e560f5554c87d0a9dde6b73942ebd85fddb9d Mon Sep 17 00:00:00 2001 From: legendecas Date: Sat, 2 Nov 2019 04:56:53 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node-addon-api/pull/545/ Reviewed-By: Michael Dawson Reviewed-By: Gabriel Schulhof --- .travis.yml | 8 +------- napi-inl.h | 14 ++++++++------ napi.h | 35 ++++++++++++++++++++--------------- test/bigint.cc | 8 +++++--- test/binding.cc | 15 ++++++++------- test/binding.gyp | 4 +++- test/date.cc | 1 - test/index.js | 27 +++++++++++++-------------- test/typedarray.cc | 33 +++++++++++++++++++++------------ 9 files changed, 79 insertions(+), 66 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6a9075313..008e9edf9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -56,13 +56,7 @@ install: script: # Travis CI sets NVM_NODEJS_ORG_MIRROR, but it makes node-gyp fail to download headers for nightly builds. - unset NVM_NODEJS_ORG_MIRROR - - NODEJS_MAJOR_VERSION=$(node -p "process.versions.node.match(/\d+/)[0]") - - | - if [ ${NODEJS_MAJOR_VERSION} -gt 11 ]; then - npm test - else - npm test $NPMOPT --NAPI_VERSION=$(node -p "process.versions.napi") - fi + - npm test after_success: - cpp-coveralls --gcov-options '\-lp' --build-root test/build --exclude test diff --git a/napi-inl.h b/napi-inl.h index 05a7ef97c..16fe3b3f2 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -383,9 +383,10 @@ inline bool Value::IsNumber() const { return Type() == napi_number; } -// currently experimental guard with version of NAPI_VERSION that it is -// released in once it is no longer experimental -#if (NAPI_VERSION > 2147483646) +// Currently experimental guard with the definition of NAPI_EXPERIMENTAL. +// Once it is no longer experimental guard with the NAPI_VERSION in which it is +// released instead. +#ifdef NAPI_EXPERIMENTAL inline bool Value::IsBigInt() const { return Type() == napi_bigint; } @@ -620,9 +621,10 @@ inline double Number::DoubleValue() const { return result; } -// currently experimental guard with version of NAPI_VERSION that it is -// released in once it is no longer experimental -#if (NAPI_VERSION > 2147483646) +// Currently experimental guard with the definition of NAPI_EXPERIMENTAL. +// Once it is no longer experimental guard with the NAPI_VERSION in which it is +// released instead. +#ifdef NAPI_EXPERIMENTAL //////////////////////////////////////////////////////////////////////////////// // BigInt Class //////////////////////////////////////////////////////////////////////////////// diff --git a/napi.h b/napi.h index d3a79a1c0..2fc9b10f3 100644 --- a/napi.h +++ b/napi.h @@ -112,9 +112,10 @@ namespace Napi { class Value; class Boolean; class Number; -// currently experimental guard with version of NAPI_VERSION that it is -// released in once it is no longer experimental -#if (NAPI_VERSION > 2147483646) +// Currently experimental guard with the definition of NAPI_EXPERIMENTAL. +// Once it is no longer experimental guard with the NAPI_VERSION in which it is +// released instead. +#ifdef NAPI_EXPERIMENTAL class BigInt; #endif // NAPI_EXPERIMENTAL #if (NAPI_VERSION > 4) @@ -140,9 +141,10 @@ 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 -// currently experimental guard with version of NAPI_VERSION that it is -// released in once it is no longer experimental -#if (NAPI_VERSION > 2147483646) +// Currently experimental guard with the definition of NAPI_EXPERIMENTAL. +// Once it is no longer experimental guard with the NAPI_VERSION in which it is +// released instead. +#ifdef NAPI_EXPERIMENTAL typedef TypedArrayOf BigInt64Array; ///< Typed array of signed 64-bit integers typedef TypedArrayOf BigUint64Array; ///< Typed array of unsigned 64-bit integers #endif // NAPI_EXPERIMENTAL @@ -245,9 +247,10 @@ 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. -// currently experimental guard with version of NAPI_VERSION that it is -// released in once it is no longer experimental -#if (NAPI_VERSION > 2147483646) +// Currently experimental guard with the definition of NAPI_EXPERIMENTAL. +// Once it is no longer experimental guard with the NAPI_VERSION in which it is +// released instead. +#ifdef NAPI_EXPERIMENTAL bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint. #endif // NAPI_EXPERIMENTAL #if (NAPI_VERSION > 4) @@ -322,9 +325,10 @@ namespace Napi { double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value. }; -// currently experimental guard with version of NAPI_VERSION that it is -// released in once it is no longer experimental -#if (NAPI_VERSION > 2147483646) +// Currently experimental guard with the definition of NAPI_EXPERIMENTAL. +// Once it is no longer experimental guard with the NAPI_VERSION in which it is +// released instead. +#ifdef NAPI_EXPERIMENTAL /// A JavaScript bigint value. class BigInt : public Value { public: @@ -852,9 +856,10 @@ namespace Napi { : std::is_same::value ? napi_uint32_array : std::is_same::value ? napi_float32_array : std::is_same::value ? 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) +// Currently experimental guard with the definition of NAPI_EXPERIMENTAL. +// Once it is no longer experimental guard with the NAPI_VERSION in which it is +// released instead. +#ifdef NAPI_EXPERIMENTAL : 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 5d1e36367..a62ed3c30 100644 --- a/test/bigint.cc +++ b/test/bigint.cc @@ -1,11 +1,13 @@ +// Currently experimental guard with NODE_MAJOR_VERISION in which it was +// released. Once it is no longer experimental guard with the NAPI_VERSION +// in which it is released instead. +#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) { diff --git a/test/binding.cc b/test/binding.cc index e97df1350..403134bf6 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -1,4 +1,3 @@ -#define NAPI_EXPERIMENTAL #include "napi.h" using namespace Napi; @@ -14,9 +13,10 @@ 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 -// released in once it is no longer experimental -#if (NAPI_VERSION > 2147483646) +// Currently experimental guard with NODE_MAJOR_VERISION in which it was +// released. Once it is no longer experimental guard with the NAPI_VERSION +// in which it is released instead. +#if (NODE_MAJOR_VERSION >= 10) Object InitBigInt(Env env); #endif Object InitBuffer(Env env); @@ -63,9 +63,10 @@ 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) +// Currently experimental guard with NODE_MAJOR_VERISION in which it was +// released. Once it is no longer experimental guard with the NAPI_VERSION +// in which it is released instead. +#if (NODE_MAJOR_VERSION >= 10) exports.Set("bigint", InitBigInt(env)); #endif #if (NAPI_VERSION > 4) diff --git a/test/binding.gyp b/test/binding.gyp index b96febda6..aa575fba3 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -1,6 +1,7 @@ { 'variables': { - 'NAPI_VERSION%': "", + 'NAPI_VERSION%': "= 10) #define NAPI_EXPERIMENTAL +#endif #include "napi.h" using namespace Napi; @@ -65,9 +70,10 @@ 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) +// Currently experimental guard with NODE_MAJOR_VERISION in which it was +// released. Once it is no longer experimental guard with the NAPI_VERSION +// in which it is released instead. +#if (NODE_MAJOR_VERSION >= 10) } else if (arrayType == "bigint64") { return buffer.IsUndefined() ? NAPI_TYPEDARRAY_NEW(BigInt64Array, info.Env(), length, napi_bigint64_array) : @@ -101,9 +107,10 @@ 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) +// Currently experimental guard with NODE_MAJOR_VERISION in which it was +// released. Once it is no longer experimental guard with the NAPI_VERSION +// in which it is released instead. +#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 @@ -143,9 +150,10 @@ 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) +// Currently experimental guard with NODE_MAJOR_VERISION in which it was +// released. Once it is no longer experimental guard with the NAPI_VERSION +// in which it is released instead. +#if (NODE_MAJOR_VERSION >= 10) case napi_bigint64_array: return BigInt::New(info.Env(), array.As()[index]); case napi_biguint64_array: @@ -189,9 +197,10 @@ 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) +// Currently experimental guard with NODE_MAJOR_VERISION in which it was +// released. Once it is no longer experimental guard with the NAPI_VERSION +// in which it is released instead. +#if (NODE_MAJOR_VERSION >= 10) case napi_bigint64_array: { bool lossless; array.As()[index] = value.As().Int64Value(&lossless);