Skip to content

Truly independent web browser

LibJS: Write the error messages of the parser into the work zone

Andreas Kling83bf8119daad
diff --git a/Libraries/LibJS/Rust/src/host.rs b/Libraries/LibJS/Rust/src/host.rs
index 75979ee705c..87261636884 100644
--- a/Libraries/LibJS/Rust/src/host.rs
+++ b/Libraries/LibJS/Rust/src/host.rs
@@ -26,8 +26,8 @@ pub fn js_number_to_utf16(value: f64) -> Utf16String {
/// Check a regex pattern+flags with the host's regex engine, so that an invalid
/// one becomes an early error. Returns the error message if it is invalid.
-pub fn validate_regex(pattern: &[u16], flags: &[u16]) -> Result<(), Utf16String> {
- implementation::validate_regex(pattern, flags).map_err(|message| Utf16String::from_utf8(&message))
+pub fn validate_regex(pattern: &[u16], flags: &[u16]) -> Result<(), String> {
+ implementation::validate_regex(pattern, flags)
}
/// StringToNumber(str), which the host implements.
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rs
index fd72b4f25d5..59c827f59c2 100644
--- a/Libraries/LibJS/Rust/src/parser/base.rs
+++ b/Libraries/LibJS/Rust/src/parser/base.rs
@@ -26,6 +26,8 @@ use ak::Zone;
use ak::ZoneMap;
use ak::ZoneSet;
use ak::ZoneVec;
+use core::fmt;
+use core::fmt::Write as _;
pub const USE_DEAD: u32 = u32::MAX;
/// On the function of a use: the use is inside a `with`.
@@ -211,10 +213,53 @@ pub(crate) struct FunctionState {
pub summary_ordinal: u32,
}
-/// The first syntax error, at a source offset.
-pub struct PendingError {
+/// How the parser checks a regular expression literal with the regex engine of the runtime that embeds it, so that an
+/// invalid one becomes an early error. Gives the message of the error, written into the zone, if it is invalid.
+pub type ValidateRegex = for<'z> fn(zone: &'z Zone, pattern: &[u16], flags: &[u16]) -> Result<(), &'z str>;
+
+/// The first syntax error, at a source offset, with its message in the work zone or in the program.
+pub struct PendingError<'z> {
pub pos: u32,
- pub message: String,
+ pub message: &'z str,
+}
+
+/// Text that is written into a zone, for the message of an error.
+pub(crate) struct ZoneText<'z>(ZoneVec<'z, u8>);
+
+impl<'z> ZoneText<'z> {
+ pub(crate) fn new(zone: &'z Zone) -> Self {
+ Self(ZoneVec::new_in(zone))
+ }
+
+ pub(crate) fn push_str(&mut self, text: &str) {
+ self.0.extend_from_slice(text.as_bytes());
+ }
+
+ pub(crate) fn finish(self) -> &'z str {
+ core::str::from_utf8(self.0.into_slice()).expect("the text is made of strings")
+ }
+}
+
+impl fmt::Write for ZoneText<'_> {
+ fn write_str(&mut self, text: &str) -> fmt::Result {
+ self.push_str(text);
+ Ok(())
+ }
+}
+
+/// UTF-8 that may have bytes in it that are not, which show as U+FFFD, like `String::from_utf8_lossy()` does it.
+pub(crate) struct LossyText<'t>(pub(crate) &'t [u8]);
+
+impl fmt::Display for LossyText<'_> {
+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
+ for chunk in self.0.utf8_chunks() {
+ formatter.write_str(chunk.valid())?;
+ if !chunk.invalid().is_empty() {
+ formatter.write_char(char::REPLACEMENT_CHARACTER)?;
+ }
+ }
+ Ok(())
+ }
}
/// What the first parse of a top level went by, which parsing it again has to go by too.
@@ -256,6 +301,7 @@ pub struct Parser<'a, 'k, U: Unit> {
pub(crate) inline_argument_limit: usize,
/// Whether anything uses what scopes declare and what code refers to, which a sweep with a summary does not.
pub(crate) tracks_scopes: bool,
+ pub(crate) validate_regex: ValidateRegex,
/// How many bindings of the function being parsed have a place in its summary.
pub(crate) summary_ordinal: u32,
pub(crate) src: *const U,
@@ -368,7 +414,7 @@ pub struct Parser<'a, 'k, U: Unit> {
pub(crate) export_names: ZoneSet<'a, u32>,
pub(crate) export_locals: ZoneVec<'a, (u32, u32)>,
- pub err: Option<PendingError>,
+ pub err: Option<PendingError<'a>>,
/// Whether this is the code of an eval, whose private names the code around it may declare.
pub initiated_by_eval: bool,
/// The private names that no class in this code declares, for an eval.
@@ -393,7 +439,13 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
///
/// # Safety
/// `source` must be followed by at least 64 units of zeros within the same allocation.
- pub unsafe fn new(zone: &'a Zone, source: &'a [U], module: bool, mode: Mode) -> Self {
+ pub unsafe fn new(
+ zone: &'a Zone,
+ source: &'a [U],
+ module: bool,
+ mode: Mode,
+ validate_regex: ValidateRegex,
+ ) -> Self {
let interner = Interner::new(zone, source.len());
// NB: A lazy parse skips the code of functions, which is most of the code of most sources, so a tree of the
// whole source is only the most it is likely to have.
@@ -421,6 +473,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
index,
estimated_nodes,
estimated_uses,
+ validate_regex,
);
parser.ast.likely_most_nodes = whole_source_nodes;
parser.pending_captures.reserve(estimated_functions * 2);
@@ -460,6 +513,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
first_pass_index: &FunctionIndex,
source_length: u32,
estimated_nodes: usize,
+ validate_regex: ValidateRegex,
) -> Self {
let mut parser = Self::with_capacities(
zone,
@@ -472,6 +526,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
FunctionIndexBuilder::with_capacity(zone, 0),
estimated_nodes,
256,
+ validate_regex,
);
parser.stamp = stamp;
parser.first_pass_index = first_pass_index;
@@ -490,6 +545,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
index: FunctionIndexBuilder<'a>,
estimated_nodes: usize,
estimated_uses: usize,
+ validate_regex: ValidateRegex,
) -> Self {
let mut parser = Self {
zone,
@@ -502,6 +558,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
arguments_in_array: false,
inline_argument_limit: crate::bytecode::operand::MAX_OPERAND_LIST_LENGTH,
tracks_scopes: true,
+ validate_regex,
summary_ordinal: 0,
src: source,
len: length as u32,
@@ -855,15 +912,21 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
}
}
+ /// The text of `message` in the work zone, for an error.
#[cold]
#[inline(never)]
- pub(crate) fn fail(&mut self, pos: u32, message: &'static str) {
- self.fail_with(pos, message.to_string());
+ pub(crate) fn message(&self, message: fmt::Arguments<'_>) -> &'a str {
+ if let Some(text) = message.as_str() {
+ return text;
+ }
+ let mut text = ZoneText::new(self.zone);
+ text.write_fmt(message).expect("writing to a zone does not fail");
+ text.finish()
}
#[cold]
#[inline(never)]
- pub(crate) fn fail_with(&mut self, pos: u32, message: String) {
+ pub(crate) fn fail(&mut self, pos: u32, message: &'a str) {
if self.err.is_none() {
self.err = Some(PendingError { pos, message });
}
@@ -881,21 +944,22 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
#[inline(never)]
pub(crate) fn unexpected(&mut self) {
let message = self.unexpected_token_message();
- self.fail_with(self.tok_start, message);
+ self.fail(self.tok_start, message);
}
#[cold]
#[inline(never)]
pub(crate) fn expected(&mut self, what: &str) {
- let message = format!("{}. Expected {what}", self.unexpected_token_message());
- self.fail_with(self.tok_start, message);
+ let message = self.message(format_args!("{}. Expected {what}", self.unexpected_token_message()));
+ self.fail(self.tok_start, message);
}
/// Fails for a token where a list goes on with a comma or ends.
#[cold]
#[inline(never)]
fn expected_comma_or(&mut self, close: T) {
- self.expected(&format!("',' or {}", close.spelling()));
+ let what = self.message(format_args!("',' or {}", close.spelling()));
+ self.expected(what);
}
/// What a list expects after an item: a comma, or the token that ends the list, which the caller looks for.
@@ -909,17 +973,22 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
}
/// Calls the current token what the source calls it.
- fn unexpected_token_message(&self) -> String {
+ fn unexpected_token_message(&self) -> &'a str {
const LONGEST_TOKEN_SHOWN: u32 = 40;
if self.tok == T::Eof {
- return "Unexpected end of input".to_string();
+ return "Unexpected end of input";
}
let end = self.tok_end.min(self.tok_start + LONGEST_TOKEN_SHOWN);
- let token: String = char::decode_utf16((self.tok_start..end).map(|index| self.at(index as usize) as u16))
- .map(|unit| unit.unwrap_or(char::REPLACEMENT_CHARACTER))
- .collect();
- let ellipsis = if end < self.tok_end { "..." } else { "" };
- format!("Unexpected token '{token}{ellipsis}'")
+ let mut text = ZoneText::new(self.zone);
+ text.push_str("Unexpected token '");
+ for unit in char::decode_utf16((self.tok_start..end).map(|index| self.at(index as usize) as u16)) {
+ text.push_str(unit.unwrap_or(char::REPLACEMENT_CHARACTER).encode_utf8(&mut [0; 4]));
+ }
+ if end < self.tok_end {
+ text.push_str("...");
+ }
+ text.push_str("'");
+ text.finish()
}
#[inline(always)]
@@ -1666,8 +1735,11 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
}
fn fail_redeclaration(&mut self, name: u32, pos: u32) {
- let name = String::from_utf8_lossy(self.interner.text(name)).into_owned();
- self.fail_with(pos, format!("Identifier '{name}' already declared"));
+ let message = self.message(format_args!(
+ "Identifier '{}' already declared",
+ LossyText(self.interner.text(name))
+ ));
+ self.fail(pos, message);
}
pub(crate) fn declare(&mut self, name: u32, bind: Bind, pos: u32) {
diff --git a/Libraries/LibJS/Rust/src/parser/lexer.rs b/Libraries/LibJS/Rust/src/parser/lexer.rs
index 9ada4aa2efc..60e3703c479 100644
--- a/Libraries/LibJS/Rust/src/parser/lexer.rs
+++ b/Libraries/LibJS/Rust/src/parser/lexer.rs
@@ -889,20 +889,22 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
for &flag in flags {
if flag >= 128 || !b"dgimsuvy".contains(&(flag as u8)) {
let flag = char::from_u32(flag as u32).unwrap_or('?');
- self.fail_with(self.tok_start, format!("Invalid RegExp flag '{flag}'"));
+ let message = self.message(format_args!("Invalid RegExp flag '{flag}'"));
+ self.fail(self.tok_start, message);
return;
}
if seen[flag as usize] {
let flag = flag as u8 as char;
- self.fail_with(self.tok_start, format!("Repeated RegExp flag '{flag}'"));
+ let message = self.message(format_args!("Repeated RegExp flag '{flag}'"));
+ self.fail(self.tok_start, message);
return;
}
seen[flag as usize] = true;
}
let pattern: &[u16] =
zone.alloc_slice_from_iter((start + 1..self.regex_body_end as usize).map(|i| self.at(i) as u16));
- if let Err(message) = crate::host::validate_regex(pattern, flags) {
- self.fail_with(start as u32, String::from_utf16_lossy(&message.to_utf16()));
+ if let Err(message) = (self.validate_regex)(zone, pattern, flags) {
+ self.fail(start as u32, message);
return;
}
self.token(T::Regex, p);
diff --git a/Libraries/LibJS/Rust/src/parser/mod.rs b/Libraries/LibJS/Rust/src/parser/mod.rs
index 6cb46d03c48..80250296a8f 100644
--- a/Libraries/LibJS/Rust/src/parser/mod.rs
+++ b/Libraries/LibJS/Rust/src/parser/mod.rs
@@ -135,6 +135,13 @@ pub struct ScriptData {
retained_code_size: AtomicUsize,
}
+/// Checks a regular expression literal with the regex engine of the runtime, for the parser (see `ValidateRegex`).
+fn validate_regex<'z>(zone: &'z Zone, pattern: &[u16], flags: &[u16]) -> Result<(), &'z str> {
+ crate::host::validate_regex(pattern, flags).map_err(|message| {
+ core::str::from_utf8(zone.alloc_slice_copy(message.as_bytes())).expect("a copy of a string is one")
+ })
+}
+
/// 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;
@@ -284,6 +291,7 @@ impl ScriptData {
&self.index,
u32_from_usize(source.len()),
estimated_nodes,
+ validate_regex,
)
};
parser.ast.likely_most_nodes = whole_function_nodes;
@@ -557,6 +565,7 @@ fn parse_code_units<'z, U: Unit + Default, R>(
} else {
Mode::Lazy
},
+ validate_regex,
)
};
parser.strict |= options.starts_in_strict_mode;
@@ -567,7 +576,7 @@ fn parse_code_units<'z, U: Unit + Default, R>(
let lines = LineTable::new(zone, units, options.initial_line_number);
let position = lines.position(error.pos.min(u32_from_usize(units.len())));
return Err(vec![ParseError {
- message: error.message,
+ message: error.message.to_owned(),
line: position.line,
column: position.column,
}]);
diff --git a/Libraries/LibJS/Rust/src/parser/stmt.rs b/Libraries/LibJS/Rust/src/parser/stmt.rs
index 8f453c6aaa4..300dbc99bb4 100644
--- a/Libraries/LibJS/Rust/src/parser/stmt.rs
+++ b/Libraries/LibJS/Rust/src/parser/stmt.rs
@@ -393,8 +393,11 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
let base = self.labels_base as usize;
let shadowed = self.label_indices.get(&name).copied();
if shadowed.is_some_and(|index| index as usize >= base) {
- let label = String::from_utf8_lossy(self.interner.text(name)).into_owned();
- self.fail_with(start, format!("Label '{label}' has already been declared"));
+ let message = self.message(format_args!(
+ "Label '{}' has already been declared",
+ LossyText(self.interner.text(name))
+ ));
+ self.fail(start, message);
return 0;
}
let kind = match self.tok {
@@ -467,23 +470,25 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
if !found {
let message = if name == NONE {
if is_break {
- "Unlabeled 'break' not allowed outside of a loop or switch statement".to_string()
+ "Unlabeled 'break' not allowed outside of a loop or switch statement"
} else {
- "'continue' not allowed outside of a loop".to_string()
+ "'continue' not allowed outside of a loop"
}
} else {
- let label = String::from_utf8_lossy(self.interner.text(name)).into_owned();
let is_in_scope = self
.label_indices
.get(&name)
.is_some_and(|&index| index as usize >= base);
if is_in_scope {
- "labelled continue statement cannot use non iterating statement".to_string()
+ "labelled continue statement cannot use non iterating statement"
} else {
- format!("Label '{label}' not found")
+ self.message(format_args!(
+ "Label '{}' not found",
+ LossyText(self.interner.text(name))
+ ))
}
};
- self.fail_with(name_pos, message);
+ self.fail(name_pos, message);
}
self.ast
.push(if is_break { tag::BREAK } else { tag::CONTINUE }, start, name, 0)
@@ -1061,11 +1066,11 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
let is_generator = self.eat(T::Star);
let (name, name_pos) = self.parse_ident();
if is_generator && is_async && (name == N_AWAIT || name == N_YIELD) {
- let text = String::from_utf8_lossy(self.interner.text(name)).into_owned();
- self.fail_with(
- name_pos,
- format!("async generator function is not allowed to be called '{text}'"),
- );
+ let message = self.message(format_args!(
+ "async generator function is not allowed to be called '{}'",
+ LossyText(self.interner.text(name))
+ ));
+ self.fail(name_pos, message);
}
if !hanging {
let bind = if self.strict || is_generator || is_async {
@@ -1418,11 +1423,11 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
}
return;
}
- let name = String::from_utf8_lossy(self.interner.text(name)).into_owned();
- self.fail_with(
- pos,
- format!("Reference to undeclared private field or method '#{name}'"),
- );
+ let message = self.message(format_args!(
+ "Reference to undeclared private field or method '#{}'",
+ LossyText(self.interner.text(name))
+ ));
+ self.fail(pos, message);
}
/// `kind` is 0 for a field or a method, 1 and 2 for instance accessors, 3 and 4 for static ones.