Skip to content

Commit 1f819ec

Browse files
ExE-Bosstargos
authored andcommitted
test: add tests for bound apply variants of varargs primordials
PR-URL: #37005 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent ed58065 commit 1f819ec

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

lib/internal/per_context/primordials.js

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ function copyPropsRenamed(src, dest, prefix) {
8181
ReflectDefineProperty(dest, name, desc);
8282
if (varargsMethods.includes(name)) {
8383
ReflectDefineProperty(dest, `${name}Apply`, {
84+
// `src` is bound as the `this` so that the static `this` points
85+
// to the object it was defined on,
86+
// e.g.: `ArrayOfApply` gets a `this` of `Array`:
8487
value: applyBind(desc.value, src),
8588
});
8689
}

lib/readline.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const {
4343
MathCeil,
4444
MathFloor,
4545
MathMax,
46+
MathMaxApply,
4647
NumberIsFinite,
4748
NumberIsNaN,
4849
ObjectDefineProperty,
@@ -626,7 +627,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
626627
// Apply/show completions.
627628
const completionsWidth = ArrayPrototypeMap(completions,
628629
(e) => getStringWidth(e));
629-
const width = MathMax(...completionsWidth) + 2; // 2 space padding
630+
const width = MathMaxApply(completionsWidth) + 2; // 2 space padding
630631
let maxColumns = MathFloor(this.columns / width) || 1;
631632
if (maxColumns === Infinity) {
632633
maxColumns = 1;

lib/zlib.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const {
2828
ArrayPrototypePush,
2929
Error,
3030
FunctionPrototypeBind,
31-
MathMax,
31+
MathMaxApply,
3232
NumberIsFinite,
3333
NumberIsNaN,
3434
ObjectDefineProperties,
@@ -788,7 +788,7 @@ function createConvenienceMethod(ctor, sync) {
788788
};
789789
}
790790

791-
const kMaxBrotliParam = MathMax(...ArrayPrototypeMap(
791+
const kMaxBrotliParam = MathMaxApply(ArrayPrototypeMap(
792792
ObjectKeys(constants),
793793
(key) => (StringPrototypeStartsWith(key, 'BROTLI_PARAM_') ?
794794
constants[key] :
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
6+
const assert = require('assert');
7+
const {
8+
ArrayOfApply,
9+
ArrayPrototypePushApply,
10+
ArrayPrototypeUnshiftApply,
11+
MathMaxApply,
12+
MathMinApply,
13+
StringPrototypeConcatApply,
14+
TypedArrayOfApply,
15+
} = require('internal/test/binding').primordials;
16+
17+
{
18+
const arr1 = [1, 2, 3];
19+
const arr2 = ArrayOfApply(arr1);
20+
21+
assert.deepStrictEqual(arr2, arr1);
22+
assert.notStrictEqual(arr2, arr1);
23+
}
24+
25+
{
26+
const array = [1, 2, 3];
27+
const i32Array = TypedArrayOfApply(Int32Array, array);
28+
29+
assert(i32Array instanceof Int32Array);
30+
assert.strictEqual(i32Array.length, array.length);
31+
for (let i = 0, { length } = array; i < length; i++) {
32+
assert.strictEqual(i32Array[i], array[i], `i32Array[${i}] === array[${i}]`);
33+
}
34+
}
35+
36+
{
37+
const arr1 = [1, 2, 3];
38+
const arr2 = [4, 5, 6];
39+
40+
const expected = [...arr1, ...arr2];
41+
42+
assert.strictEqual(ArrayPrototypePushApply(arr1, arr2), expected.length);
43+
assert.deepStrictEqual(arr1, expected);
44+
}
45+
46+
{
47+
const arr1 = [1, 2, 3];
48+
const arr2 = [4, 5, 6];
49+
50+
const expected = [...arr2, ...arr1];
51+
52+
assert.strictEqual(ArrayPrototypeUnshiftApply(arr1, arr2), expected.length);
53+
assert.deepStrictEqual(arr1, expected);
54+
}
55+
56+
{
57+
const array = [1, 2, 3];
58+
assert.strictEqual(MathMaxApply(array), 3);
59+
assert.strictEqual(MathMinApply(array), 1);
60+
}
61+
62+
{
63+
let hint;
64+
const obj = { [Symbol.toPrimitive](h) {
65+
hint = h;
66+
return '[object Object]';
67+
} };
68+
69+
const args = ['foo ', obj, ' bar'];
70+
const result = StringPrototypeConcatApply('', args);
71+
72+
assert.strictEqual(hint, 'string');
73+
assert.strictEqual(result, 'foo [object Object] bar');
74+
}

0 commit comments

Comments
 (0)