Skip to content

Commit

Permalink
fix: use private block scope RegExp instead of shared module scope Re…
Browse files Browse the repository at this point in the history
…gExp
  • Loading branch information
Theo-Steiner committed Feb 22, 2023
1 parent 04b00ba commit 7a8ce5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
28 changes: 18 additions & 10 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
MathMaxApply,
NumberIsFinite,
ObjectSetPrototypeOf,
RegExp,
RegExpPrototypeExec,
StringPrototypeCodePointAt,
StringPrototypeEndsWith,
Expand Down Expand Up @@ -72,7 +73,7 @@ const kHistorySize = 30;
const kMaxUndoRedoStackSize = 2048;
const kMincrlfDelay = 100;
// \r\n, \n, or \r followed by something other than \n
const lineEnding = /\r?\n|\r(?!\n)/g;
const lineEndingPattern = '\r?\n|\r(?!\n)';

const kLineObjectStream = Symbol('line object stream');
const kQuestionCancel = Symbol('kQuestionCancel');
Expand Down Expand Up @@ -585,6 +586,7 @@ class Interface extends InterfaceConstructor {
}

// Run test() on the new string chunk, not on the entire line buffer.
const lineEnding = new RegExp(lineEndingPattern, 'g');
let newPartContainsEnding = RegExpPrototypeExec(lineEnding, string);
if (newPartContainsEnding !== null) {
if (this[kLine_buffer]) {
Expand Down Expand Up @@ -1322,18 +1324,24 @@ class Interface extends InterfaceConstructor {
// falls through
default:
if (typeof s === 'string' && s) {
/**
* Use Regular Expression scoped to this block, as lastIndex and the state for RegExpPrototypeExec
* will be overwritten if the same RegEx instance is reused in recursive function calls.
*/
const lineEnding = new RegExp(lineEndingPattern, 'g');
let nextMatch = RegExpPrototypeExec(lineEnding, s);
if (nextMatch !== null) {
this[kInsertString](StringPrototypeSlice(s, 0, nextMatch.index));
let { lastIndex } = lineEnding;
while ((nextMatch = RegExpPrototypeExec(lineEnding, s)) !== null) {
this[kLine]();

// If no line endings are found, just insert the string as is
if (nextMatch === null) {
this[kInsertString](s);
} else {
// Keep track of the end of the last match
let lastIndex = 0;
do {
this[kInsertString](StringPrototypeSlice(s, lastIndex, nextMatch.index));
this[kLine]();
({ lastIndex } = lineEnding);
}
if (lastIndex === s.length) this[kLine]();
} else {
this[kInsertString](s);
} while ((nextMatch = RegExpPrototypeExec(lineEnding, s)) !== null);
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ function REPLServer(prompt,
const matches = self._sawKeyPress ?
RegExpPrototypeExec(/^\s+/, cmd) : null;
// Preserve indentation in editorMode
if (matches && !self.loadMode) {
if (matches) {
const prefix = matches[0];
self.write(prefix);
self.line = prefix;
Expand Down Expand Up @@ -1780,11 +1780,9 @@ function defineDefaultCommands(repl) {
const stats = fs.statSync(file);
if (stats && stats.isFile()) {
_turnOnEditorMode(this);
this.loadMode = true;
const data = fs.readFileSync(file, 'utf8');
this.write(data);
_turnOffEditorMode(this);
this.loadMode = false;
this.write('\n');
} else {
this.output.write(
Expand All @@ -1793,7 +1791,6 @@ function defineDefaultCommands(repl) {
}
} catch {
this.output.write(`Failed to load: ${file}\n`);
this.loadMode = false;
}
this.displayPrompt();
}
Expand Down

0 comments on commit 7a8ce5f

Please sign in to comment.