Skip to content

Truly independent web browser

LibJS: Let the parser core name nothing of the code around it

Andreas Kling19821c9eb9e6
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rs
index a816cd5448b..d96bb8e80ee 100644
--- a/Libraries/LibJS/Rust/src/parser/base.rs
+++ b/Libraries/LibJS/Rust/src/parser/base.rs
@@ -34,6 +34,9 @@ pub const USE_DEAD: u32 = u32::MAX;
/// On the function of a use: the use is inside a `with`.
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];
@@ -435,9 +438,19 @@ pub(crate) const MAX_SUBSCRIPT_CHAIN_LENGTH: u32 = 10000;
pub(crate) const RECURSION_LIMIT_MESSAGE: &str = "Maximum parser recursion depth exceeded";
+/// A function could not be parsed or compiled, because the stack that is left does not hold it.
+#[derive(Clone, Copy, Debug)]
+pub struct OutOfStack;
+
+/// The address that the stack can only go `bytes` below where it is now.
+pub fn stack_limit_for_budget(bytes: usize) -> usize {
+ ak::stack::limit_for_budget(bytes)
+}
+
impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
/// A parser for the first parse of `source`, with its work data in `zone`. `initial_names` are the tables of an
- /// interner with the names that every interner starts with.
+ /// interner with the names that every interner starts with. `vm_stack_limit` is how low a VM lets the stack of this
+ /// thread go, if that can be told.
///
/// # Safety
/// `source` must be followed by at least 64 units of zeros within the same allocation.
@@ -448,6 +461,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
mode: Mode,
initial_names: &NameTables<'_>,
validate_regex: ValidateRegex,
+ vm_stack_limit: Option<usize>,
) -> Self {
let interner = Interner::new(zone, source.len(), initial_names);
// NB: A lazy parse skips the code of functions, which is most of the code of most sources, so a tree of the
@@ -479,6 +493,8 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
validate_regex,
);
parser.ast.likely_most_nodes = whole_source_nodes;
+ // NB: Code can start parsing with less stack left than the budget, as eval near the end of the stack does.
+ parser.stack_limit = stack_limit_for_budget(DEFAULT_STACK_BUDGET).max(vm_stack_limit.unwrap_or(0));
parser.pending_captures.reserve(estimated_functions * 2);
if source.len() >= u32::MAX as usize - 64 {
parser.len = 0;
@@ -546,7 +562,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
estimated_uses: usize,
validate_regex: ValidateRegex,
) -> Self {
- let mut parser = Self {
+ Self {
zone,
row_cursor: core::cell::Cell::new(0),
marks: false,
@@ -555,7 +571,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
block_mark: NONE,
last_function_of_block: 0,
arguments_in_array: false,
- inline_argument_limit: crate::bytecode::operand::MAX_OPERAND_LIST_LENGTH,
+ inline_argument_limit: 0,
tracks_scopes: true,
validate_regex,
summary_ordinal: 0,
@@ -641,11 +657,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
initiated_by_eval: false,
eval_referenced_private_names: ZoneVec::new_in(zone),
_marker: core::marker::PhantomData,
- };
- // NB: Code can start parsing with less stack left than the budget, as eval near the end of the stack does.
- parser.stack_limit = super::stack_limit_for_budget(DEFAULT_STACK_BUDGET)
- .max(super::vm_stack_limit_of_this_thread().unwrap_or(0));
- parser
+ }
}
/// Allows what `context` allows from the start of the program.
@@ -691,11 +703,12 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
&mut self,
context: ProgramContext,
index: FunctionIndexView<'a>,
- ) -> Result<(Ast<'a>, u32), super::OutOfStack> {
+ inline_argument_limit: usize,
+ ) -> Result<(Ast<'a>, u32), OutOfStack> {
// NB: The rows are those of the tree, and marks, which some code has many of.
let nodes = self.ast.tag.len();
let rows = Ast::with_capacities(self.zone, nodes + nodes / 2, self.ast.extra.len());
- let tree = std::mem::replace(&mut self.ast, rows);
+ let tree = core::mem::replace(&mut self.ast, rows);
let private_names = self.eval_referenced_private_names.take();
let (first_token_start, strict, has_top_level_await) =
(self.first_token_start, self.strict, self.has_top_level_await);
@@ -704,10 +717,11 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
self.first_pass_index = Some(index);
self.tracks_scopes = false;
self.marks = true;
+ self.inline_argument_limit = inline_argument_limit;
let node = self.reparse_program(context);
self.first_pass_index = None;
let error = self.err.take();
- let rows = std::mem::replace(&mut self.ast, tree);
+ let rows = core::mem::replace(&mut self.ast, tree);
self.eval_referenced_private_names = private_names;
self.first_token_start = first_token_start;
self.strict = strict;
@@ -719,7 +733,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
error.message, RECURSION_LIMIT_MESSAGE,
"the first parse found no errors"
);
- Err(super::OutOfStack)
+ Err(OutOfStack)
}
}
}
@@ -2177,7 +2191,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
};
self.summary_ordinal = state.summary_ordinal;
if !self.is_reparsing() {
- if self.tok_end as usize + super::SOURCE_PADDING > self.len as usize {
+ if self.tok_end as usize + SOURCE_PADDING > self.len as usize {
flags |= function_flag::REACHES_SOURCE_END;
}
let row = state.row as usize;
diff --git a/Libraries/LibJS/Rust/src/parser/columns.rs b/Libraries/LibJS/Rust/src/parser/columns.rs
index c37d184a2ae..a347e4195c6 100644
--- a/Libraries/LibJS/Rust/src/parser/columns.rs
+++ b/Libraries/LibJS/Rust/src/parser/columns.rs
@@ -10,7 +10,7 @@
//!
//! Names and literals are not copied: an identifier operand is an interned name id, and a literal is its source offset.
-use std::cell::Cell;
+use core::cell::Cell;
use ak::Zone;
use ak::ZoneVec;
diff --git a/Libraries/LibJS/Rust/src/parser/intern.rs b/Libraries/LibJS/Rust/src/parser/intern.rs
index 38e9e2d48b5..d064cb2595c 100644
--- a/Libraries/LibJS/Rust/src/parser/intern.rs
+++ b/Libraries/LibJS/Rust/src/parser/intern.rs
@@ -404,7 +404,7 @@ impl<'z> Interner<'z> {
#[cfg(test)]
mod tests {
use super::Interner;
- use crate::parser::kept::KeptNames;
+ use super::NameTablesMut;
use ak::Zone;
#[test]
@@ -444,10 +444,19 @@ mod tests {
assert_eq!(interner.intern_bytes(name), *id);
}
- let mut kept = KeptNames::of(
- &Interner::with_names(&zone, (0..interner.len()).map(|id| interner.text(id as u32))).tables(),
- );
- let mut reparsed = Interner::of_tables(&zone, kept.tables_mut());
+ // NB: The tables of a reparse context are a copy of those that an interner makes with the names.
+ let made = Interner::with_names(&zone, (0..interner.len()).map(|id| interner.text(id as u32)));
+ let tables = made.tables();
+ let kept = NameTablesMut {
+ rows: zone.alloc_slice_copy(tables.rows),
+ shift: tables.shift,
+ count: tables.count,
+ long_rows: zone.alloc_slice_copy(tables.long_rows),
+ long_count: tables.long_count,
+ text: zone.alloc_slice_copy(tables.text),
+ spans: zone.alloc_slice_copy(tables.spans),
+ };
+ let mut reparsed = Interner::of_tables(&zone, kept);
for (name, id) in &expected {
assert_eq!(reparsed.intern_bytes(name), *id);
assert_eq!(reparsed.text(*id), name);
diff --git a/Libraries/LibJS/Rust/src/parser/mod.rs b/Libraries/LibJS/Rust/src/parser/mod.rs
index 33e89583694..5db6d14c944 100644
--- a/Libraries/LibJS/Rust/src/parser/mod.rs
+++ b/Libraries/LibJS/Rust/src/parser/mod.rs
@@ -40,7 +40,10 @@ use crate::work_zone::with_work_zone;
use ak::Zone;
use ak::ZoneVec;
use base::Mode;
+pub use base::OutOfStack;
use base::Parser;
+use base::SOURCE_PADDING;
+pub use base::stack_limit_for_budget;
use columns::Ast;
use columns::AstColumns;
use columns::FunctionIndexBuilder;
@@ -60,9 +63,6 @@ use std::sync::Mutex;
use std::sync::Weak;
use unit::Unit;
-/// How many zero code units the lexer needs after the source.
-const SOURCE_PADDING: usize = 64;
-
/// A function of a source, as the row that the first parse of the source gave it.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct FunctionRow(pub(crate) u32);
@@ -149,15 +149,6 @@ fn validate_regex<'z>(zone: &'z Zone, pattern: &[u16], flags: &[u16]) -> Result<
})
}
-/// A function could not be parsed or compiled, because the stack that is left does not hold it.
-#[derive(Clone, Copy, Debug)]
-pub struct OutOfStack;
-
-/// The address that the stack can only go `bytes` below where it is now.
-pub fn stack_limit_for_budget(bytes: usize) -> usize {
- ak::stack::limit_for_budget(bytes)
-}
-
/// How low a VM lets the stack of this thread go, or `None` if that cannot be told.
pub(crate) fn vm_stack_limit_of_this_thread() -> Option<usize> {
ak::stack::lowest_address_of_this_thread()
@@ -582,6 +573,7 @@ fn parse_code_units<'z, U: Unit + Default, R>(
},
&KeptNames::initial(zone).tables(),
validate_regex,
+ vm_stack_limit_of_this_thread(),
)
};
parser.strict |= options.starts_in_strict_mode;
@@ -603,7 +595,11 @@ fn parse_code_units<'z, U: Unit + Default, R>(
let lines = LineTable::new(zone, units, options.initial_line_number);
let index = core::mem::replace(&mut parser.index, FunctionIndexBuilder::with_capacity(zone, 0)).finish();
let top_level_rows = parser
- .parse_top_level_rows(options.program_context(), index)
+ .parse_top_level_rows(
+ options.program_context(),
+ index,
+ crate::bytecode::operand::MAX_OPERAND_LIST_LENGTH,
+ )
.map(|(rows, node)| (rows.into_columns(), node));
let default_export_name = Name(parser.interner.intern_bytes(b"*default*"));
let default_constructor_parameter_name = Name(parser.interner.intern_bytes(b"args"));
diff --git a/Libraries/LibJS/Rust/src/parser/stmt.rs b/Libraries/LibJS/Rust/src/parser/stmt.rs
index 35d4de46daa..f854030cc7f 100644
--- a/Libraries/LibJS/Rust/src/parser/stmt.rs
+++ b/Libraries/LibJS/Rust/src/parser/stmt.rs
@@ -1719,7 +1719,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
} else {
reserved_row
};
- let reaches_source_end = self.tok_end as usize + super::SOURCE_PADDING > self.len as usize;
+ let reaches_source_end = self.tok_end as usize + SOURCE_PADDING > self.len as usize;
let index = &mut self.index;
index.params_start[row as usize] = key;
index.end[row as usize] = self.prev_end;