From b81f4480a3870dc1c8a214376d19da7e80b39285 Mon Sep 17 00:00:00 2001 From: raisinten Date: Tue, 8 Dec 2020 18:31:09 +0530 Subject: [PATCH 01/20] lib: refactor to remove for-let-of loop in repl.js --- lib/repl.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 3b95bd7f29c85f..dee34136020dd4 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1229,8 +1229,8 @@ function complete(line, callback) { paths = ArrayPrototypeConcat(module.paths, CJSModule.globalPaths); } - for (let dir of paths) { - dir = path.resolve(dir, subdir); + for (let i = 0; i < paths.length; i++) { + const dir = path.resolve(paths[i], subdir); const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; for (const dirent of dirents) { if (RegExpPrototypeTest(versionedFileNamesRe, dirent.name) || From f5d6c3394b6a10b0a982c4b73cc6c96c2d7874b9 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Thu, 10 Dec 2020 20:58:33 +0530 Subject: [PATCH 02/20] Apply suggestion from code review Co-authored-by: Antoine du Hamel --- lib/repl.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index dee34136020dd4..bb9840b9ba4dcb 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1229,8 +1229,8 @@ function complete(line, callback) { paths = ArrayPrototypeConcat(module.paths, CJSModule.globalPaths); } - for (let i = 0; i < paths.length; i++) { - const dir = path.resolve(paths[i], subdir); + ArrayPrototypeForEach(paths, (dir) => { + dir = path.resolve(dir, subdir); const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; for (const dirent of dirents) { if (RegExpPrototypeTest(versionedFileNamesRe, dirent.name) || From b6b3c5b1d1c64922b0d785e32cc43597b2648ba9 Mon Sep 17 00:00:00 2001 From: raisinten Date: Thu, 10 Dec 2020 21:01:59 +0530 Subject: [PATCH 03/20] lib: require ArrayPrototypeForEach in repl.js --- lib/repl.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/repl.js b/lib/repl.js index bb9840b9ba4dcb..276939ee52f65f 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -46,6 +46,7 @@ const { ArrayPrototypeConcat, ArrayPrototypeFilter, ArrayPrototypeFindIndex, + ArrayPrototypeForEach, ArrayPrototypeIncludes, ArrayPrototypeJoin, ArrayPrototypeMap, From 0a167a11b781f2193fe0bba8a7f8eee08bb8ac95 Mon Sep 17 00:00:00 2001 From: raisinten Date: Thu, 10 Dec 2020 21:09:57 +0530 Subject: [PATCH 04/20] lib: fix syntax error repl.js --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 276939ee52f65f..dfabb559c5abd8 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1258,7 +1258,7 @@ function complete(line, callback) { } } } - } + }); if (group.length) { ArrayPrototypePush(completionGroups, group); } From 3a9491ee5920f3f9f7159e106f51fae7b81a71b0 Mon Sep 17 00:00:00 2001 From: raisinten Date: Thu, 10 Dec 2020 21:12:49 +0530 Subject: [PATCH 05/20] lib: replace another loop repl.js --- lib/repl.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index dfabb559c5abd8..87474810191f6a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -663,7 +663,7 @@ function REPLServer(prompt, let matched = false; errStack = ''; - for (const line of lines) { + ArrayPrototypePush(lines, line => { if (!matched && RegExpPrototypeTest(/^\[?([A-Z][a-z0-9_]*)*Error/, line)) { errStack += writer.options.breakLength >= line.length ? @@ -673,7 +673,7 @@ function REPLServer(prompt, } else { errStack += line; } - } + }); if (!matched) { const ln = lines.length === 1 ? ' ' : ':\n'; errStack = `Uncaught${ln}${errStack}`; @@ -1230,7 +1230,7 @@ function complete(line, callback) { paths = ArrayPrototypeConcat(module.paths, CJSModule.globalPaths); } - ArrayPrototypeForEach(paths, (dir) => { + ArrayPrototypeForEach(paths, dir => { dir = path.resolve(dir, subdir); const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; for (const dirent of dirents) { From 846ac65cd7a5c8450514bc578f4657ed4bf590eb Mon Sep 17 00:00:00 2001 From: raisinten Date: Thu, 10 Dec 2020 21:22:58 +0530 Subject: [PATCH 06/20] lib: fix lint error repl.js --- lib/repl.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 87474810191f6a..7f871f05ce0c41 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -663,7 +663,7 @@ function REPLServer(prompt, let matched = false; errStack = ''; - ArrayPrototypePush(lines, line => { + ArrayPrototypePush(lines, (line) => { if (!matched && RegExpPrototypeTest(/^\[?([A-Z][a-z0-9_]*)*Error/, line)) { errStack += writer.options.breakLength >= line.length ? @@ -1230,7 +1230,7 @@ function complete(line, callback) { paths = ArrayPrototypeConcat(module.paths, CJSModule.globalPaths); } - ArrayPrototypeForEach(paths, dir => { + ArrayPrototypeForEach(paths, (dir) => { dir = path.resolve(dir, subdir); const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; for (const dirent of dirents) { From 9367f9b6dcec01c62cf6e589cf82fce87dd42b5b Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Fri, 11 Dec 2020 18:46:07 +0530 Subject: [PATCH 07/20] Apply suggestions from code review Co-authored-by: Antoine du Hamel --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 7f871f05ce0c41..f3ee4dc9ecf7d4 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -663,7 +663,7 @@ function REPLServer(prompt, let matched = false; errStack = ''; - ArrayPrototypePush(lines, (line) => { + ArrayPrototypeForEach(lines, (line) => { if (!matched && RegExpPrototypeTest(/^\[?([A-Z][a-z0-9_]*)*Error/, line)) { errStack += writer.options.breakLength >= line.length ? From 61cb3b7fb1809d645829950c3efacc7776141b1c Mon Sep 17 00:00:00 2001 From: raisinten Date: Fri, 11 Dec 2020 19:08:00 +0530 Subject: [PATCH 08/20] lib: replace all for loops with ArrayPrototypeForEach in repl.js --- lib/repl.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index f3ee4dc9ecf7d4..0ab11c73e36a7c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -754,9 +754,7 @@ function REPLServer(prompt, const prioritizedSigintQueue = new SafeSet(); self.on('SIGINT', function onSigInt() { if (prioritizedSigintQueue.size > 0) { - for (const task of prioritizedSigintQueue) { - task(); - } + ArrayPrototypeForEach(prioritizedSigintQueue, task => task()); return; } @@ -1010,13 +1008,13 @@ REPLServer.prototype.createContext = function() { }, () => { context = vm.createContext(); }); - for (const name of ObjectGetOwnPropertyNames(global)) { + ArrayPrototypeForEach(ObjectGetOwnPropertyNames(global), (name) => { // Only set properties that do not already exist as a global builtin. if (!globalBuiltins.has(name)) { ObjectDefineProperty(context, name, ObjectGetOwnPropertyDescriptor(global, name)); } - } + }); context.global = context; const _console = new Console(this.output); ObjectDefineProperty(context, 'console', { @@ -1350,11 +1348,11 @@ function complete(line, callback) { if (memberGroups.length) { expr += chaining; - for (const group of memberGroups) { + ArrayPrototypeForEach(memberGroups, (group) => { ArrayPrototypePush(completionGroups, ArrayPrototypeMap(group, (member) => `${expr}${member}`)); - } + }); if (filter) { filter = `${expr}${filter}`; } @@ -1373,7 +1371,7 @@ function complete(line, callback) { // Filter, sort (within each group), uniq and merge the completion groups. if (completionGroups.length && filter) { const newCompletionGroups = []; - for (const group of completionGroups) { + ArrayPrototypeForEach(completionGroups, (group) => { const filteredGroup = ArrayPrototypeFilter( group, (str) => StringPrototypeStartsWith(str, filter) @@ -1381,7 +1379,7 @@ function complete(line, callback) { if (filteredGroup.length) { ArrayPrototypePush(newCompletionGroups, filteredGroup); } - } + }); completionGroups = newCompletionGroups; } @@ -1390,20 +1388,20 @@ function complete(line, callback) { const uniqueSet = new SafeSet(['']); // Completion group 0 is the "closest" (least far up the inheritance // chain) so we put its completions last: to be closest in the REPL. - for (const group of completionGroups) { + ArrayPrototypeForEach(completionGroups, (group) => { ArrayPrototypeSort(group, (a, b) => (b > a ? 1 : -1)); const setSize = uniqueSet.size; - for (const entry of group) { + ArrayPrototypeSort(group, (entry) => { if (!uniqueSet.has(entry)) { ArrayPrototypeUnshift(completions, entry); uniqueSet.add(entry); } - } + }); // Add a separator between groups. if (uniqueSet.size !== setSize) { ArrayPrototypeUnshift(completions, ''); } - } + }); // Remove obsolete group entry, if present. if (completions[0] === '') { @@ -1567,14 +1565,13 @@ function defineDefaultCommands(repl) { const longestNameLength = MathMax( ...ArrayPrototypeMap(names, (name) => name.length) ); - for (let n = 0; n < names.length; n++) { - const name = names[n]; + ArrayPrototypeForEach(names, (name) => { const cmd = this.commands[name]; const spaces = StringPrototypeRepeat(' ', longestNameLength - name.length + 3); const line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`; this.output.write(line); - } + }; this.output.write('\nPress Ctrl+C to abort current expression, ' + 'Ctrl+D to exit the REPL\n'); this.displayPrompt(); From 10c1759567e5e34a214c5d9bfb874af3b8caee5d Mon Sep 17 00:00:00 2001 From: raisinten Date: Fri, 11 Dec 2020 19:13:09 +0530 Subject: [PATCH 09/20] lib: fix syntax error repl.js --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 0ab11c73e36a7c..00cb07e86909e1 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1571,7 +1571,7 @@ function defineDefaultCommands(repl) { StringPrototypeRepeat(' ', longestNameLength - name.length + 3); const line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`; this.output.write(line); - }; + }); this.output.write('\nPress Ctrl+C to abort current expression, ' + 'Ctrl+D to exit the REPL\n'); this.displayPrompt(); From 669f93e109e12db874b2a6d9e1c099fe436895c7 Mon Sep 17 00:00:00 2001 From: raisinten Date: Fri, 11 Dec 2020 19:37:19 +0530 Subject: [PATCH 10/20] lib: fix lint error --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 00cb07e86909e1..689eac6a9ae45b 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -754,7 +754,7 @@ function REPLServer(prompt, const prioritizedSigintQueue = new SafeSet(); self.on('SIGINT', function onSigInt() { if (prioritizedSigintQueue.size > 0) { - ArrayPrototypeForEach(prioritizedSigintQueue, task => task()); + ArrayPrototypeForEach(prioritizedSigintQueue, (task) => task()); return; } From dc678b029ccb5c7ecc72228c5a7727c3bc01db3d Mon Sep 17 00:00:00 2001 From: raisinten Date: Wed, 16 Dec 2020 20:05:37 +0530 Subject: [PATCH 11/20] lib: replace ArrayPrototypeSort with ArrayPrototypeForEach in repl.js --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 689eac6a9ae45b..b2ab7e1f5b55bb 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1391,7 +1391,7 @@ function complete(line, callback) { ArrayPrototypeForEach(completionGroups, (group) => { ArrayPrototypeSort(group, (a, b) => (b > a ? 1 : -1)); const setSize = uniqueSet.size; - ArrayPrototypeSort(group, (entry) => { + ArrayPrototypeForEach(group, (entry) => { if (!uniqueSet.has(entry)) { ArrayPrototypeUnshift(completions, entry); uniqueSet.add(entry); From ddfbfc4a367728ea0489287b1528346440736ccf Mon Sep 17 00:00:00 2001 From: raisinten Date: Fri, 18 Dec 2020 20:03:15 +0530 Subject: [PATCH 12/20] test: add unsafe array iteration test to repl --- .../test-repl-unsafe-array-iteration.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/parallel/test-repl-unsafe-array-iteration.js diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js new file mode 100644 index 00000000000000..4016c389116d81 --- /dev/null +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -0,0 +1,42 @@ +'use strict'; +const common = require('../common'); +const ArrayStream = require('../common/arraystream'); +const assert = require('assert'); +const repl = require('repl'); + +function run(input) { + const inputStream = new ArrayStream(); + const outputStream = new ArrayStream(); + let output = ''; + + outputStream.write = (data) => { output += data.replace('\r', ''); }; + + const r = repl.start({ + input: inputStream, + output: outputStream, + terminal: true + }); + + r.on('exit', common.mustCall(() => { + const actual = output.split('\n'); + console.log(actual); + + // Validate that the for loop returns undefined + assert.ok(actual[actual.length - 2].includes('undefined')); + })); + + inputStream.run(input); + r.close(); +} + +run([ + 'delete Array.prototype[Symbol.iterator];', + 'for(const x of [3, 2, 1]);' +]); + +run([ + 'const ArrayIteratorPrototype =', + ' Object.getPrototypeOf(Array.prototype[Symbol.iterator]());', + 'delete ArrayIteratorPrototype.next;', + 'for(const x of [3, 2, 1]);' +]); From 77a6be5dc84f430f536a7a0aad4de9231b3ca81b Mon Sep 17 00:00:00 2001 From: raisinten Date: Fri, 18 Dec 2020 20:54:57 +0530 Subject: [PATCH 13/20] test: add notStrictEqual check instead for repl.js --- test/parallel/test-repl-unsafe-array-iteration.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js index 4016c389116d81..e73491beb90c49 100644 --- a/test/parallel/test-repl-unsafe-array-iteration.js +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -4,7 +4,7 @@ const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); -function run(input) { +function run(input, expectation) { const inputStream = new ArrayStream(); const outputStream = new ArrayStream(); let output = ''; @@ -19,10 +19,9 @@ function run(input) { r.on('exit', common.mustCall(() => { const actual = output.split('\n'); - console.log(actual); // Validate that the for loop returns undefined - assert.ok(actual[actual.length - 2].includes('undefined')); + assert.notStrictEqual(actual[actual.length - 2], expectation); })); inputStream.run(input); @@ -32,11 +31,11 @@ function run(input) { run([ 'delete Array.prototype[Symbol.iterator];', 'for(const x of [3, 2, 1]);' -]); +], 'Uncaught TypeError: [3,2,1] is not iterable'); run([ 'const ArrayIteratorPrototype =', ' Object.getPrototypeOf(Array.prototype[Symbol.iterator]());', 'delete ArrayIteratorPrototype.next;', 'for(const x of [3, 2, 1]);' -]); +], 'Uncaught TypeError: undefined is not a function'); From 6c199913c0230b97d83547f09009b2f549eca493 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Fri, 18 Dec 2020 21:07:32 +0530 Subject: [PATCH 14/20] Update test/parallel/test-repl-unsafe-array-iteration.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl-unsafe-array-iteration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js index e73491beb90c49..c1cc3755b94d26 100644 --- a/test/parallel/test-repl-unsafe-array-iteration.js +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -21,7 +21,7 @@ function run(input, expectation) { const actual = output.split('\n'); // Validate that the for loop returns undefined - assert.notStrictEqual(actual[actual.length - 2], expectation); + assert.strictEqual(actual[actual.length - 2], expectation); })); inputStream.run(input); From ccc860aaaab16bb6d65d6676215fed6759d1e451 Mon Sep 17 00:00:00 2001 From: raisinten Date: Sat, 19 Dec 2020 20:28:45 +0530 Subject: [PATCH 15/20] test: add test using child_process instead --- .../test-repl-unsafe-array-iteration.js | 44 +++++++------------ 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js index c1cc3755b94d26..763582979b8769 100644 --- a/test/parallel/test-repl-unsafe-array-iteration.js +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -1,41 +1,27 @@ 'use strict'; const common = require('../common'); -const ArrayStream = require('../common/arraystream'); const assert = require('assert'); -const repl = require('repl'); +const { spawn } = require('child_process'); function run(input, expectation) { - const inputStream = new ArrayStream(); - const outputStream = new ArrayStream(); - let output = ''; + const node = spawn('node'); - outputStream.write = (data) => { output += data.replace('\r', ''); }; - - const r = repl.start({ - input: inputStream, - output: outputStream, - terminal: true - }); - - r.on('exit', common.mustCall(() => { - const actual = output.split('\n'); + node.stderr.on('data', common.mustCall((data) => { + assert.ok(data.includes(expectation)); + })); - // Validate that the for loop returns undefined - assert.strictEqual(actual[actual.length - 2], expectation); + node.on('close', common.mustCall((code) => { + assert.strictEqual(code, 1); })); - inputStream.run(input); - r.close(); + node.stdin.write(input); + node.stdin.end(); } -run([ - 'delete Array.prototype[Symbol.iterator];', - 'for(const x of [3, 2, 1]);' -], 'Uncaught TypeError: [3,2,1] is not iterable'); +run('delete Array.prototype[Symbol.iterator];', + 'TypeError: Found non-callable @@iterator'); -run([ - 'const ArrayIteratorPrototype =', - ' Object.getPrototypeOf(Array.prototype[Symbol.iterator]());', - 'delete ArrayIteratorPrototype.next;', - 'for(const x of [3, 2, 1]);' -], 'Uncaught TypeError: undefined is not a function'); +run('const ArrayIteratorPrototype =' + + 'Object.getPrototypeOf(Array.prototype[Symbol.iterator]());' + + 'delete ArrayIteratorPrototype.next;', + 'TypeError: fn is not a function'); From 81776d6e9b3d04cc1a5f544a2c3dcaf60722fdcd Mon Sep 17 00:00:00 2001 From: raisinten Date: Sat, 19 Dec 2020 21:31:15 +0530 Subject: [PATCH 16/20] test: remove error code check --- test/parallel/test-repl-unsafe-array-iteration.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js index 763582979b8769..e3885ee8030093 100644 --- a/test/parallel/test-repl-unsafe-array-iteration.js +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -10,10 +10,6 @@ function run(input, expectation) { assert.ok(data.includes(expectation)); })); - node.on('close', common.mustCall((code) => { - assert.strictEqual(code, 1); - })); - node.stdin.write(input); node.stdin.end(); } From a45f1ed65e3f58d9cb996ead89003cfd1074e184 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 19 Dec 2020 21:36:38 +0530 Subject: [PATCH 17/20] Update test/parallel/test-repl-unsafe-array-iteration.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl-unsafe-array-iteration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js index e3885ee8030093..77e15ccbb61ae4 100644 --- a/test/parallel/test-repl-unsafe-array-iteration.js +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -4,7 +4,7 @@ const assert = require('assert'); const { spawn } = require('child_process'); function run(input, expectation) { - const node = spawn('node'); + const node = spawn(process.argv0); node.stderr.on('data', common.mustCall((data) => { assert.ok(data.includes(expectation)); From c61b0b9fbc84a830256659fc23948946868d2e83 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 19 Dec 2020 21:55:16 +0530 Subject: [PATCH 18/20] Update test/parallel/test-repl-unsafe-array-iteration.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl-unsafe-array-iteration.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js index 77e15ccbb61ae4..5701f2094788f8 100644 --- a/test/parallel/test-repl-unsafe-array-iteration.js +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -6,9 +6,8 @@ const { spawn } = require('child_process'); function run(input, expectation) { const node = spawn(process.argv0); - node.stderr.on('data', common.mustCall((data) => { - assert.ok(data.includes(expectation)); - })); + node.stderr.on('data', common.mustNotCall()); + node.on('exit', common.mustCall((code) => assert.strictEqual(code, 0))); node.stdin.write(input); node.stdin.end(); From 71350bd3ba21a17bc44b6ae1497df4ee2e422464 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 20 Dec 2020 18:48:23 +0530 Subject: [PATCH 19/20] Update test/parallel/test-repl-unsafe-array-iteration.js Co-authored-by: Antoine du Hamel --- .../test-repl-unsafe-array-iteration.js | 63 +++++++++++++++---- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js index 5701f2094788f8..9f1b618c581542 100644 --- a/test/parallel/test-repl-unsafe-array-iteration.js +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -3,20 +3,59 @@ const common = require('../common'); const assert = require('assert'); const { spawn } = require('child_process'); -function run(input, expectation) { - const node = spawn(process.argv0); +const replProcess = spawn(process.argv0, ['--interactive'], { + stdio: ['pipe', 'pipe', 'inherit'], + windowsHide: true, +}); - node.stderr.on('data', common.mustNotCall()); - node.on('exit', common.mustCall((code) => assert.strictEqual(code, 0))); +replProcess.on('error', common.mustNotCall()); - node.stdin.write(input); - node.stdin.end(); +const replReadyState = (async function* () { + let ready; + const replReadyBuffer = Buffer.from('> '); + replProcess.stdout.on('data', (data) => { + ready = Buffer.compare(data, replReadyBuffer) === 0; + }); + + const processCrashed = new Promise((resolve, reject) => + replProcess.on('exit', reject) + ); + while (true) { + await Promise.race([new Promise(setImmediate), processCrashed]); + if (ready) { + ready = false; + yield; + } + } +})(); +async function writeLn(data, expectedOutput) { + await replReadyState.next(); + if (expectedOutput) { + replProcess.stdout.once('data', common.mustCall((data) => + assert.match(data.toString('utf8'), expectedOutput) + )); + } + await new Promise((resolve, reject) => replProcess.stdin.write( + `${data}\n`, + (err) => (err ? reject(err) : resolve()) + )); } -run('delete Array.prototype[Symbol.iterator];', - 'TypeError: Found non-callable @@iterator'); +async function main() { + await writeLn( + 'const ArrayIteratorPrototype =' + + ' Object.getPrototypeOf(Array.prototype[Symbol.iterator]());' + ); + await writeLn('delete ArrayIteratorPrototype.next;'); + await writeLn('delete Array.prototype[Symbol.iterator];'); + + await writeLn( + 'for(const x of [3, 2, 1]);', + /Uncaught TypeError: \[3,2,1\] is not iterable/ + ); + await writeLn('.exit'); + + assert(!replProcess.connected); +} -run('const ArrayIteratorPrototype =' + - 'Object.getPrototypeOf(Array.prototype[Symbol.iterator]());' + - 'delete ArrayIteratorPrototype.next;', - 'TypeError: fn is not a function'); +main().then(common.mustCall()); From 1ec7036d4ac0ea8b36de3b4550f5d11088f1b5a9 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 21 Dec 2020 18:27:11 +0530 Subject: [PATCH 20/20] Update test/parallel/test-repl-unsafe-array-iteration.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl-unsafe-array-iteration.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-repl-unsafe-array-iteration.js b/test/parallel/test-repl-unsafe-array-iteration.js index 9f1b618c581542..faeb2b159c7ad2 100644 --- a/test/parallel/test-repl-unsafe-array-iteration.js +++ b/test/parallel/test-repl-unsafe-array-iteration.js @@ -12,9 +12,16 @@ replProcess.on('error', common.mustNotCall()); const replReadyState = (async function* () { let ready; - const replReadyBuffer = Buffer.from('> '); + const SPACE = ' '.charCodeAt(); + const BRACKET = '>'.charCodeAt(); + const DOT = '.'.charCodeAt(); replProcess.stdout.on('data', (data) => { - ready = Buffer.compare(data, replReadyBuffer) === 0; + ready = data[data.length - 1] === SPACE && ( + data[data.length - 2] === BRACKET || ( + data[data.length - 2] === DOT && + data[data.length - 3] === DOT && + data[data.length - 4] === DOT + )); }); const processCrashed = new Promise((resolve, reject) =>