Skip to content

Commit 46dc5d7

Browse files
lib: prefer call() over apply() if argument list is not array
PR-URL: #60796 Reviewed-By: René <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 7fd3688 commit 46dc5d7

File tree

23 files changed

+75
-82
lines changed

23 files changed

+75
-82
lines changed

lib/assert.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ const {
2727
ArrayPrototypePush,
2828
ArrayPrototypeSlice,
2929
Error,
30+
FunctionPrototypeCall,
3031
NumberIsNaN,
3132
ObjectAssign,
3233
ObjectDefineProperty,
3334
ObjectIs,
3435
ObjectKeys,
3536
ObjectPrototypeIsPrototypeOf,
36-
ReflectApply,
3737
RegExpPrototypeExec,
3838
String,
3939
StringPrototypeIndexOf,
@@ -544,7 +544,7 @@ function expectedException(actual, expected, message, fn) {
544544
throwError = true;
545545
} else {
546546
// Check validation functions return value.
547-
const res = ReflectApply(expected, {}, [actual]);
547+
const res = FunctionPrototypeCall(expected, {}, actual);
548548
if (res !== true) {
549549
if (!message) {
550550
generatedMessage = true;
@@ -689,7 +689,7 @@ function hasMatchingError(actual, expected) {
689689
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
690690
return false;
691691
}
692-
return ReflectApply(expected, {}, [actual]) === true;
692+
return FunctionPrototypeCall(expected, {}, actual) === true;
693693
}
694694

695695
function expectsNoError(stackStartFn, actual, error, message) {

lib/dgram.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const {
2929
FunctionPrototypeCall,
3030
ObjectDefineProperty,
3131
ObjectSetPrototypeOf,
32-
ReflectApply,
3332
SymbolAsyncDispose,
3433
SymbolDispose,
3534
} = primordials;
@@ -436,7 +435,7 @@ Socket.prototype.connect = function(port, address, callback) {
436435
return;
437436
}
438437

439-
ReflectApply(_connect, this, [port, address, callback]);
438+
FunctionPrototypeCall(_connect, this, port, address, callback);
440439
};
441440

442441

lib/fs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const {
2929
ArrayPrototypePush,
3030
BigIntPrototypeToString,
3131
Boolean,
32+
FunctionPrototypeCall,
3233
MathMax,
3334
Number,
3435
ObjectDefineProperties,
@@ -366,7 +367,7 @@ function readFile(path, options, callback) {
366367
}
367368
if (context.isUserFd) {
368369
process.nextTick(function tick(context) {
369-
ReflectApply(readFileAfterOpen, { context }, [null, path]);
370+
FunctionPrototypeCall(readFileAfterOpen, { context }, null, path);
370371
}, context);
371372
return;
372373
}

lib/internal/child_process.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,7 @@ function setupChannel(target, channel, serializationMode) {
805805
obj = handleConversion[message.type];
806806

807807
// convert TCP object to native handle object
808-
handle = ReflectApply(handleConversion[message.type].send,
809-
target, [message, handle, options]);
808+
handle = FunctionPrototypeCall(handleConversion[message.type].send, target, message, handle, options);
810809

811810
// If handle was sent twice, or it is impossible to get native handle
812811
// out of it - just send a text without the handle.

lib/internal/cluster/child.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayPrototypeJoin,
55
FunctionPrototype,
6+
FunctionPrototypeCall,
67
ObjectAssign,
78
ReflectApply,
89
SafeMap,
@@ -58,7 +59,7 @@ cluster._setupWorker = function() {
5859
if (message.act === 'newconn')
5960
onconnection(message, handle);
6061
else if (message.act === 'disconnect')
61-
ReflectApply(_disconnect, worker, [true]);
62+
FunctionPrototypeCall(_disconnect, worker, true);
6263
}
6364
};
6465

@@ -287,7 +288,7 @@ function _disconnect(primaryInitiated) {
287288
Worker.prototype.disconnect = function() {
288289
if (this.state !== 'disconnecting' && this.state !== 'destroying') {
289290
this.state = 'disconnecting';
290-
ReflectApply(_disconnect, this, []);
291+
FunctionPrototypeCall(_disconnect, this);
291292
}
292293

293294
return this;

lib/internal/cluster/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
FunctionPrototypeCall,
45
ObjectSetPrototypeOf,
56
ReflectApply,
67
} = primordials;
@@ -16,7 +17,7 @@ function Worker(options) {
1617
if (!(this instanceof Worker))
1718
return new Worker(options);
1819

19-
ReflectApply(EventEmitter, this, []);
20+
FunctionPrototypeCall(EventEmitter, this);
2021

2122
if (options === null || typeof options !== 'object')
2223
options = kEmptyObject;

lib/internal/crypto/cipher.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

33
const {
4+
FunctionPrototypeCall,
45
NumberIsInteger,
56
ObjectSetPrototypeOf,
6-
ReflectApply,
77
} = primordials;
88

99
const {
@@ -117,15 +117,15 @@ function createCipherBase(cipher, credential, options, isEncrypt, iv) {
117117
this[kHandle] = new CipherBase(isEncrypt, cipher, credential, iv, authTagLength);
118118
this._decoder = null;
119119

120-
ReflectApply(LazyTransform, this, [options]);
120+
FunctionPrototypeCall(LazyTransform, this, options);
121121
}
122122

123123
function createCipherWithIV(cipher, key, options, isEncrypt, iv) {
124124
validateString(cipher, 'cipher');
125125
const encoding = getStringOption(options, 'encoding');
126126
key = prepareSecretKey(key, encoding);
127127
iv = iv === null ? null : getArrayBufferOrView(iv, 'iv');
128-
ReflectApply(createCipherBase, this, [cipher, key, options, isEncrypt, iv]);
128+
FunctionPrototypeCall(createCipherBase, this, cipher, key, options, isEncrypt, iv);
129129
}
130130

131131
// The Cipher class is part of the legacy Node.js crypto API. It exposes
@@ -215,7 +215,7 @@ function Cipheriv(cipher, key, iv, options) {
215215
if (!(this instanceof Cipheriv))
216216
return new Cipheriv(cipher, key, iv, options);
217217

218-
ReflectApply(createCipherWithIV, this, [cipher, key, options, true, iv]);
218+
FunctionPrototypeCall(createCipherWithIV, this, cipher, key, options, true, iv);
219219
}
220220

221221
function addCipherPrototypeFunctions(constructor) {
@@ -244,7 +244,7 @@ function Decipheriv(cipher, key, iv, options) {
244244
if (!(this instanceof Decipheriv))
245245
return new Decipheriv(cipher, key, iv, options);
246246

247-
ReflectApply(createCipherWithIV, this, [cipher, key, options, false, iv]);
247+
FunctionPrototypeCall(createCipherWithIV, this, cipher, key, options, false, iv);
248248
}
249249

250250
ObjectSetPrototypeOf(Decipheriv.prototype, LazyTransform.prototype);

lib/internal/crypto/hash.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
const {
4+
FunctionPrototypeCall,
45
ObjectSetPrototypeOf,
5-
ReflectApply,
66
StringPrototypeReplace,
77
StringPrototypeToLowerCase,
88
Symbol,
@@ -105,7 +105,7 @@ function Hash(algorithm, options) {
105105
if (!isCopy && xofLen === undefined) {
106106
maybeEmitDeprecationWarning(algorithm);
107107
}
108-
ReflectApply(LazyTransform, this, [options]);
108+
FunctionPrototypeCall(LazyTransform, this, options);
109109
}
110110

111111
ObjectSetPrototypeOf(Hash.prototype, LazyTransform.prototype);
@@ -169,7 +169,7 @@ function Hmac(hmac, key, options) {
169169
this[kState] = {
170170
[kFinalized]: false,
171171
};
172-
ReflectApply(LazyTransform, this, [options]);
172+
FunctionPrototypeCall(LazyTransform, this, options);
173173
}
174174

175175
ObjectSetPrototypeOf(Hmac.prototype, LazyTransform.prototype);

lib/internal/crypto/sig.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const {
44
FunctionPrototypeCall,
55
ObjectSetPrototypeOf,
6-
ReflectApply,
76
} = primordials;
87

98
const {
@@ -59,7 +58,7 @@ function Sign(algorithm, options) {
5958
this[kHandle] = new _Sign();
6059
this[kHandle].init(algorithm);
6160

62-
ReflectApply(Writable, this, [options]);
61+
FunctionPrototypeCall(Writable, this, options);
6362
}
6463

6564
ObjectSetPrototypeOf(Sign.prototype, Writable.prototype);
@@ -219,7 +218,7 @@ function Verify(algorithm, options) {
219218
this[kHandle] = new _Verify();
220219
this[kHandle].init(algorithm);
221220

222-
ReflectApply(Writable, this, [options]);
221+
FunctionPrototypeCall(Writable, this, options);
223222
}
224223

225224
ObjectSetPrototypeOf(Verify.prototype, Writable.prototype);

lib/internal/crypto/webcrypto.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ArrayPrototypeIncludes,
5+
FunctionPrototypeCall,
56
JSONParse,
67
JSONStringify,
78
ObjectDefineProperties,
@@ -84,7 +85,7 @@ async function digest(algorithm, data) {
8485

8586
algorithm = normalizeAlgorithm(algorithm, 'digest');
8687

87-
return await ReflectApply(asyncDigest, this, [algorithm, data]);
88+
return await FunctionPrototypeCall(asyncDigest, this, algorithm, data);
8889
}
8990

9091
function randomUUID() {
@@ -377,10 +378,10 @@ async function deriveKey(
377378
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
378379
}
379380

380-
return ReflectApply(
381+
return FunctionPrototypeCall(
381382
importKeySync,
382383
this,
383-
['raw-secret', bits, derivedKeyAlgorithm, extractable, keyUsages],
384+
'raw-secret', bits, derivedKeyAlgorithm, extractable, keyUsages,
384385
);
385386
}
386387

@@ -889,10 +890,10 @@ async function importKey(
889890

890891
algorithm = normalizeAlgorithm(algorithm, 'importKey');
891892

892-
return ReflectApply(
893+
return FunctionPrototypeCall(
893894
importKeySync,
894895
this,
895-
[format, keyData, algorithm, extractable, keyUsages],
896+
format, keyData, algorithm, extractable, keyUsages,
896897
);
897898
}
898899

@@ -926,7 +927,7 @@ async function wrapKey(format, key, wrappingKey, algorithm) {
926927
} catch {
927928
algorithm = normalizeAlgorithm(algorithm, 'encrypt');
928929
}
929-
let keyData = await ReflectApply(exportKey, this, [format, key]);
930+
let keyData = await FunctionPrototypeCall(exportKey, this, format, key);
930931

931932
if (format === 'jwk') {
932933
const ec = new TextEncoder();
@@ -1023,10 +1024,10 @@ async function unwrapKey(
10231024
}
10241025
}
10251026

1026-
return ReflectApply(
1027+
return FunctionPrototypeCall(
10271028
importKeySync,
10281029
this,
1029-
[format, keyData, unwrappedKeyAlgo, extractable, keyUsages],
1030+
format, keyData, unwrappedKeyAlgo, extractable, keyUsages,
10301031
);
10311032
}
10321033

@@ -1349,10 +1350,10 @@ async function encapsulateKey(encapsulationAlgorithm, encapsulationKey, sharedKe
13491350
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
13501351
}
13511352

1352-
const sharedKey = ReflectApply(
1353+
const sharedKey = FunctionPrototypeCall(
13531354
importKeySync,
13541355
this,
1355-
['raw-secret', encapsulateBits.sharedKey, normalizedSharedKeyAlgorithm, extractable, usages],
1356+
'raw-secret', encapsulateBits.sharedKey, normalizedSharedKeyAlgorithm, extractable, usages,
13561357
);
13571358

13581359
const encapsulatedKey = {
@@ -1469,10 +1470,10 @@ async function decapsulateKey(
14691470
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
14701471
}
14711472

1472-
return ReflectApply(
1473+
return FunctionPrototypeCall(
14731474
importKeySync,
14741475
this,
1475-
['raw-secret', decapsulatedBits, normalizedSharedKeyAlgorithm, extractable, usages],
1476+
'raw-secret', decapsulatedBits, normalizedSharedKeyAlgorithm, extractable, usages,
14761477
);
14771478
}
14781479

0 commit comments

Comments
 (0)