Skip to content

Truly independent web browser

LibJS: Stop lexing once a parse has failed

Shannon Boothe7491dce4767
diff --git a/Libraries/LibJS/Parser/Rust/src/base.rs b/Libraries/LibJS/Parser/Rust/src/base.rs
index c99b6e592d3..6189c2ff00c 100644
--- a/Libraries/LibJS/Parser/Rust/src/base.rs
+++ b/Libraries/LibJS/Parser/Rust/src/base.rs
@@ -37,9 +37,6 @@ pub const USE_IN_WITH: u32 = 1 << 31;
/// How many zero code units the lexer needs after the source.
pub const SOURCE_PADDING: usize = 64;
-/// What the lexer finds past the end of a source without padding once a parse has given up on it.
-static NO_MORE_SOURCE: [u16; 64] = [0; 64];
-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Mode {
/// Only keep the tree of top-level code and of functions that are likely called right away.
@@ -947,10 +944,6 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
if self.err.is_none() {
self.err = Some(PendingError { pos, message });
}
- if self.is_reparsing() {
- // The end of the source may have no padding, and the lexer is about to read it.
- self.src = NO_MORE_SOURCE.as_ptr().cast::<U>().wrapping_sub(self.len as usize);
- }
self.tok = T::Eof;
self.tok_start = self.len;
self.tok_end = self.len;
diff --git a/Libraries/LibJS/Parser/Rust/src/lexer.rs b/Libraries/LibJS/Parser/Rust/src/lexer.rs
index 28b8fd89389..fba04d819d9 100644
--- a/Libraries/LibJS/Parser/Rust/src/lexer.rs
+++ b/Libraries/LibJS/Parser/Rust/src/lexer.rs
@@ -89,6 +89,11 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
}
pub(crate) fn lex(&mut self) {
+ // NB: A parse that has failed lexes no further into the source, whose end may have no padding.
+ if self.err.is_some() {
+ self.tok = T::Eof;
+ return;
+ }
let mut p = self.pos as usize;
let mut newline = false;
let c = loop {
@@ -922,6 +927,9 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
/// Whether the directive prologue at `start` contains "use strict". This looks ahead in the source so that the
/// rest of the prologue is parsed knowing it.
pub(crate) fn has_use_strict_directive(&mut self, start: usize) -> bool {
+ if self.err.is_some() {
+ return false;
+ }
let mut p = start;
loop {
p = self.skip_trivia_raw(p).0;
diff --git a/Tests/LibJS/Runtime/function-compile-near-stack-limit.js b/Tests/LibJS/Runtime/function-compile-near-stack-limit.js
index 800ed91e385..6d6349f337e 100644
--- a/Tests/LibJS/Runtime/function-compile-near-stack-limit.js
+++ b/Tests/LibJS/Runtime/function-compile-near-stack-limit.js
@@ -13,8 +13,20 @@ function callAtEveryStackDepth(callback) {
const deeplyNested = "(".repeat(1900) + "1" + ")".repeat(1900);
+test("compiling a function that runs out of stack inside a property with a string key", () => {
+ const lazy = new Function(`return { "a-string-key": ${deeplyNested} };`);
+ callAtEveryStackDepth(lazy);
+ expect(lazy()).toEqual({ "a-string-key": 1 });
+});
+
test("compiling a function that runs out of stack before the constructor of a class", () => {
const lazy = new Function(`class C { static x = ${deeplyNested}; constructor() {} } return C;`);
callAtEveryStackDepth(lazy);
expect(lazy().x).toBe(1);
});
+
+test("compiling a function that runs out of stack inside an arrow function", () => {
+ const lazy = new Function(`return (a => ${deeplyNested})();`);
+ callAtEveryStackDepth(lazy);
+ expect(lazy()).toBe(1);
+});