Skip to content

Commit

Permalink
box regex constants
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jan 15, 2024
1 parent 2e1f336 commit 1a0bf22
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion crates/dash_decompiler/src/decompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,10 @@ impl fmt::Display for DisplayConstant<'_, '_> {
),
Constant::Null => f.write_str("null"),
Constant::Undefined => f.write_str("undefined"),
Constant::Regex(_, _, source) => write!(f, "{source}"),
Constant::Regex(regex) => {
let (_, _, sym) = &**regex;
write!(f, "{}", self.0.resolve(*sym))
}
}
}
}
6 changes: 4 additions & 2 deletions crates/dash_middle/src/compiler/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ pub enum Constant {
Identifier(Symbol),
Boolean(bool),
Function(Rc<Function>),
Regex(dash_regex::ParsedRegex, dash_regex::Flags, Symbol),
// Boxed because this otherwise bloats the enum way too much.
// This makes evaluating regex constants slower but they're *far* less common than e.g. number literals
Regex(Box<(dash_regex::ParsedRegex, dash_regex::Flags, Symbol)>),
Null,
Undefined,
}
Expand Down Expand Up @@ -146,7 +148,7 @@ impl Constant {
LiteralExpr::Boolean(b) => Self::Boolean(*b),
LiteralExpr::Null => Self::Null,
LiteralExpr::Undefined => Self::Undefined,
LiteralExpr::Regex(regex, flags, source) => Self::Regex(regex.clone(), *flags, *source),
LiteralExpr::Regex(regex, flags, source) => Self::Regex(Box::new((regex.clone(), *flags, *source))),
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/dash_vm/src/gc/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ unsafe impl Trace for Constant {
Constant::Identifier(sym) => sym.trace(cx),
Constant::Boolean(_) => {}
Constant::Function(func) => func.trace(cx),
Constant::Regex(_, _, sym) => sym.trace(cx),
Constant::Regex(s) => {
let (_, _, sym) = &**s;
sym.trace(cx);
}
Constant::Null => {}
Constant::Undefined => {}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/dash_vm/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ impl Value {
Constant::String(s) => Value::String(s.into()),
Constant::Undefined => Value::undefined(),
Constant::Null => Value::null(),
Constant::Regex(nodes, flags, source) => {
Constant::Regex(regex) => {
let (nodes, flags, source) = *regex;
let regex = RegExp::new(nodes, flags, source.into(), vm);
Value::Object(vm.register(regex))
}
Expand Down

0 comments on commit 1a0bf22

Please sign in to comment.