From f3d2e6ac298ddaf123e29f7f0e66049c312a5f3b Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Fri, 22 Jan 2021 07:00:00 +0100 Subject: [PATCH] =?UTF-8?q?test:=20add=C2=A0tests=20for=C2=A0`bound=C2=A0a?= =?UTF-8?q?pply`=C2=A0variants=20of=C2=A0varargs=C2=A0`primordials`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/37005 Reviewed-By: Antoine du Hamel Reviewed-By: Rich Trott --- lib/internal/per_context/primordials.js | 3 + lib/readline.js | 3 +- lib/zlib.js | 4 +- test/parallel/test-primordials-apply.js | 74 +++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-primordials-apply.js diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 73d0c30b0bfbc8..b3663c7c60b1bb 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -79,6 +79,9 @@ function copyPropsRenamed(src, dest, prefix) { ReflectDefineProperty(dest, name, desc); if (varargsMethods.includes(name)) { ReflectDefineProperty(dest, `${name}Apply`, { + // `src` is bound as the `this` so that the static `this` points + // to the object it was defined on, + // e.g.: `ArrayOfApply` gets a `this` of `Array`: value: applyBind(desc.value, src), }); } diff --git a/lib/readline.js b/lib/readline.js index ba131aa53bd30d..f8b9dbb61ff2c2 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -43,6 +43,7 @@ const { MathCeil, MathFloor, MathMax, + MathMaxApply, NumberIsFinite, NumberIsNaN, ObjectDefineProperty, @@ -626,7 +627,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) { // Apply/show completions. const completionsWidth = ArrayPrototypeMap(completions, (e) => getStringWidth(e)); - const width = MathMax(...completionsWidth) + 2; // 2 space padding + const width = MathMaxApply(completionsWidth) + 2; // 2 space padding let maxColumns = MathFloor(this.columns / width) || 1; if (maxColumns === Infinity) { maxColumns = 1; diff --git a/lib/zlib.js b/lib/zlib.js index 8242bbf7f779d0..daabf7b5ca7489 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -28,7 +28,7 @@ const { ArrayPrototypePush, Error, FunctionPrototypeBind, - MathMax, + MathMaxApply, NumberIsFinite, NumberIsNaN, ObjectDefineProperties, @@ -796,7 +796,7 @@ function createConvenienceMethod(ctor, sync) { }; } -const kMaxBrotliParam = MathMax(...ArrayPrototypeMap( +const kMaxBrotliParam = MathMaxApply(ArrayPrototypeMap( ObjectKeys(constants), (key) => (StringPrototypeStartsWith(key, 'BROTLI_PARAM_') ? constants[key] : diff --git a/test/parallel/test-primordials-apply.js b/test/parallel/test-primordials-apply.js new file mode 100644 index 00000000000000..0901a87ba18777 --- /dev/null +++ b/test/parallel/test-primordials-apply.js @@ -0,0 +1,74 @@ +// Flags: --expose-internals +'use strict'; + +require('../common'); + +const assert = require('assert'); +const { + ArrayOfApply, + ArrayPrototypePushApply, + ArrayPrototypeUnshiftApply, + MathMaxApply, + MathMinApply, + StringPrototypeConcatApply, + TypedArrayOfApply, +} = require('internal/test/binding').primordials; + +{ + const arr1 = [1, 2, 3]; + const arr2 = ArrayOfApply(arr1); + + assert.deepStrictEqual(arr2, arr1); + assert.notStrictEqual(arr2, arr1); +} + +{ + const array = [1, 2, 3]; + const i32Array = TypedArrayOfApply(Int32Array, array); + + assert(i32Array instanceof Int32Array); + assert.strictEqual(i32Array.length, array.length); + for (let i = 0, { length } = array; i < length; i++) { + assert.strictEqual(i32Array[i], array[i], `i32Array[${i}] === array[${i}]`); + } +} + +{ + const arr1 = [1, 2, 3]; + const arr2 = [4, 5, 6]; + + const expected = [...arr1, ...arr2]; + + assert.strictEqual(ArrayPrototypePushApply(arr1, arr2), expected.length); + assert.deepStrictEqual(arr1, expected); +} + +{ + const arr1 = [1, 2, 3]; + const arr2 = [4, 5, 6]; + + const expected = [...arr2, ...arr1]; + + assert.strictEqual(ArrayPrototypeUnshiftApply(arr1, arr2), expected.length); + assert.deepStrictEqual(arr1, expected); +} + +{ + const array = [1, 2, 3]; + assert.strictEqual(MathMaxApply(array), 3); + assert.strictEqual(MathMinApply(array), 1); +} + +{ + let hint; + const obj = { [Symbol.toPrimitive](h) { + hint = h; + return '[object Object]'; + } }; + + const args = ['foo ', obj, ' bar']; + const result = StringPrototypeConcatApply('', args); + + assert.strictEqual(hint, 'string'); + assert.strictEqual(result, 'foo [object Object] bar'); +}