Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ed41fd1

Browse filesBrowse files
Trotttargos
authored andcommitted
child_process: revise argument processing
execFile() and fork() have complicated argument processing. Clarify code and avoid using `arguments`. PR-URL: #41280 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1985284 commit ed41fd1
Copy full SHA for ed41fd1

File tree

Expand file treeCollapse file tree

1 file changed

+38
-36
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+38
-36
lines changed
Open diff view settings
Collapse file

‎lib/child_process.js‎

Copy file name to clipboardExpand all lines: lib/child_process.js
+38-36Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,27 @@ const MAX_BUFFER = 1024 * 1024;
111111
* }} [options]
112112
* @returns {ChildProcess}
113113
*/
114-
function fork(modulePath /* , args, options */) {
114+
function fork(modulePath, args = [], options) {
115115
modulePath = getValidatedPath(modulePath, 'modulePath');
116116

117117
// Get options and args arguments.
118118
let execArgv;
119-
let options = {};
120-
let args = [];
121-
let pos = 1;
122-
if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
123-
args = arguments[pos++];
124-
}
125119

126-
if (pos < arguments.length && arguments[pos] == null) {
127-
pos++;
120+
if (args == null) {
121+
args = [];
122+
} else if (typeof args !== 'object') {
123+
throw new ERR_INVALID_ARG_VALUE('args', args);
124+
} else if (!ArrayIsArray(args)) {
125+
options = args;
126+
args = [];
128127
}
129128

130-
if (pos < arguments.length && arguments[pos] != null) {
131-
if (typeof arguments[pos] !== 'object') {
132-
throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]);
133-
}
134-
135-
options = { ...arguments[pos++] };
129+
if (options == null) {
130+
options = {};
131+
} else if (typeof options !== 'object') {
132+
throw new ERR_INVALID_ARG_VALUE('options', options);
133+
} else {
134+
options = { ...options };
136135
}
137136

138137
// Prepare arguments for fork:
@@ -276,31 +275,34 @@ ObjectDefineProperty(exec, promisify.custom, {
276275
* ) => any} [callback]
277276
* @returns {ChildProcess}
278277
*/
279-
function execFile(file /* , args, options, callback */) {
280-
let args = [];
281-
let callback;
282-
let options;
283-
284-
// Parse the optional positional parameters.
285-
let pos = 1;
286-
if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
287-
args = arguments[pos++];
288-
} else if (pos < arguments.length && arguments[pos] == null) {
289-
pos++;
290-
}
291-
292-
if (pos < arguments.length && typeof arguments[pos] === 'object') {
293-
options = arguments[pos++];
294-
} else if (pos < arguments.length && arguments[pos] == null) {
295-
pos++;
278+
function execFile(file, args = [], options, callback) {
279+
if (args == null) {
280+
args = [];
281+
} else if (typeof args === 'object') {
282+
if (!ArrayIsArray(args)) {
283+
callback = options;
284+
options = args;
285+
args = [];
286+
}
287+
} else if (typeof args === 'function') {
288+
callback = args;
289+
options = {};
290+
args = [];
291+
} else {
292+
throw new ERR_INVALID_ARG_VALUE('args', args);
296293
}
297294

298-
if (pos < arguments.length && typeof arguments[pos] === 'function') {
299-
callback = arguments[pos++];
295+
if (options == null) {
296+
options = {};
297+
} else if (typeof options === 'function') {
298+
callback = options;
299+
options = {};
300+
} else if (typeof options !== 'object') {
301+
throw new ERR_INVALID_ARG_VALUE('options', options);
300302
}
301303

302-
if (!callback && pos < arguments.length && arguments[pos] != null) {
303-
throw new ERR_INVALID_ARG_VALUE('args', arguments[pos]);
304+
if (callback && typeof callback !== 'function') {
305+
throw new ERR_INVALID_ARG_VALUE('callback', callback);
304306
}
305307

306308
options = {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.