-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repl: refactor to avoid unsafe array iteration
PR-URL: #36663 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
5 changed files
with
146 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { spawn } = require('child_process'); | ||
|
||
const replProcess = spawn(process.argv0, ['--interactive'], { | ||
stdio: ['pipe', 'pipe', 'inherit'], | ||
windowsHide: true, | ||
}); | ||
|
||
replProcess.on('error', common.mustNotCall()); | ||
|
||
const replReadyState = (async function* () { | ||
let ready; | ||
const SPACE = ' '.charCodeAt(); | ||
const BRACKET = '>'.charCodeAt(); | ||
const DOT = '.'.charCodeAt(); | ||
replProcess.stdout.on('data', (data) => { | ||
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) => | ||
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()) | ||
)); | ||
} | ||
|
||
async function main() { | ||
await writeLn( | ||
'const ArrayIteratorPrototype =' + | ||
' Object.getPrototypeOf(Array.prototype[Symbol.iterator]());' | ||
); | ||
await writeLn('delete Array.prototype[Symbol.iterator];'); | ||
await writeLn('delete ArrayIteratorPrototype.next;'); | ||
|
||
await writeLn( | ||
'for(const x of [3, 2, 1]);', | ||
/Uncaught TypeError: \[3,2,1\] is not iterable/ | ||
); | ||
await writeLn('.exit'); | ||
|
||
assert(!replProcess.connected); | ||
} | ||
|
||
main().then(common.mustCall()); |