Skip to content

Commit

Permalink
test: add tests for bound apply variants of varargs primordials
Browse files Browse the repository at this point in the history
PR-URL: #37005
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
ExE-Boss authored and targos committed Feb 2, 2021
1 parent ed58065 commit 1f819ec
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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),
});
}
Expand Down
3 changes: 2 additions & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const {
MathCeil,
MathFloor,
MathMax,
MathMaxApply,
NumberIsFinite,
NumberIsNaN,
ObjectDefineProperty,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const {
ArrayPrototypePush,
Error,
FunctionPrototypeBind,
MathMax,
MathMaxApply,
NumberIsFinite,
NumberIsNaN,
ObjectDefineProperties,
Expand Down Expand Up @@ -788,7 +788,7 @@ function createConvenienceMethod(ctor, sync) {
};
}

const kMaxBrotliParam = MathMax(...ArrayPrototypeMap(
const kMaxBrotliParam = MathMaxApply(ArrayPrototypeMap(
ObjectKeys(constants),
(key) => (StringPrototypeStartsWith(key, 'BROTLI_PARAM_') ?
constants[key] :
Expand Down
74 changes: 74 additions & 0 deletions test/parallel/test-primordials-apply.js
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 1f819ec

Please sign in to comment.