Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 25, 2024
1 parent a73c5af commit f2c85b3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
17 changes: 9 additions & 8 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ pub trait ConstantEvaluation<'a> {
UnaryOperator::UnaryNegation => {
self.eval_to_number(&unary_expr.argument).map(|v| -v)
}
UnaryOperator::LogicalNot => {
self.get_boolean_value(expr).map(|b| if b { 1_f64 } else { 0_f64 })
}
// UnaryOperator::LogicalNot => {
// self.get_boolean_value(expr).map(|b| if b { 1_f64 } else { 0_f64 })
// }
UnaryOperator::Void => Some(f64::NAN),
_ => None,
},
Expand Down Expand Up @@ -180,6 +180,7 @@ pub trait ConstantEvaluation<'a> {
Expression::Identifier(ident) => self.resolve_binding(ident),
Expression::NumericLiteral(lit) => Some(ConstantValue::Number(lit.value)),
Expression::NullLiteral(_) => Some(ConstantValue::Null),
Expression::BooleanLiteral(lit) => Some(ConstantValue::Boolean(lit.value)),
Expression::StringLiteral(lit) => {
Some(ConstantValue::String(Cow::Borrowed(lit.value.as_str())))
}
Expand Down Expand Up @@ -354,11 +355,11 @@ pub trait ConstantEvaluation<'a> {
}
UnaryOperator::LogicalNot => {
// Don't fold !0 and !1 back to false.
if let Expression::NumericLiteral(n) = &expr.argument {
if n.value.is_zero() || n.value.is_one() {
return None;
}
}
// if let Expression::NumericLiteral(n) = &expr.argument {
// if n.value.is_zero() || n.value.is_one() {
// return None;
// }
// }
self.get_boolean_value(&expr.argument).map(|b| !b).map(ConstantValue::Boolean)
}
UnaryOperator::UnaryPlus => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ use crate::{node_util::Ctx, CompressOptions, CompressorPass};
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java>
pub struct PeepholeSubstituteAlternateSyntax {
options: CompressOptions,

/// Do not compress syntaxes that are hard to analyze inside the fixed loop.
/// e.g. Do not compress `undefined -> void 0`, `true` -> `!0`.
/// Opposite of `late` in Closure Compier.
in_fixed_loop: bool,

// states
in_define_export: bool,

changed: bool,
}

Expand Down Expand Up @@ -83,13 +91,13 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
self.changed = true;
}
}
if !Self::compress_undefined(expr, ctx) {
self.compress_boolean(expr, ctx);
}
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let ctx = Ctx(ctx);
// self.try_compress_boolean(expr, ctx);
dbg!(&expr);
self.try_compress_undefined(expr, ctx);
match expr {
Expression::NewExpression(new_expr) => {
if let Some(new_expr) = Self::try_fold_new_expression(new_expr, ctx) {
Expand Down Expand Up @@ -135,19 +143,22 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
}

impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
pub fn new(options: CompressOptions) -> Self {
Self { options, in_define_export: false, changed: false }
pub fn new(in_fixed_loop: bool, options: CompressOptions) -> Self {
Self { options, in_fixed_loop, in_define_export: false, changed: false }
}

/* Utilities */

/// Transforms `undefined` => `void 0`
fn compress_undefined(expr: &mut Expression<'a>, ctx: Ctx<'a, 'b>) -> bool {
fn try_compress_undefined(&mut self, expr: &mut Expression<'a>, ctx: Ctx<'a, 'b>) {
if self.in_fixed_loop {
return;
}
if ctx.is_expression_undefined(expr) {
dbg!("in");
*expr = ctx.ast.void_0(expr.span());
return true;
};
false
self.changed = true;
}
}

/// Test `Object.defineProperty(exports, ...)`
Expand Down Expand Up @@ -187,8 +198,11 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
/// Transforms boolean expression `true` => `!0` `false` => `!1`.
/// Enabled by `compress.booleans`.
/// Do not compress `true` in `Object.defineProperty(exports, 'Foo', {enumerable: true, ...})`.
fn compress_boolean(&mut self, expr: &mut Expression<'a>, ctx: Ctx<'a, 'b>) -> bool {
let Expression::BooleanLiteral(lit) = expr else { return false };
fn try_compress_boolean(&mut self, expr: &mut Expression<'a>, ctx: Ctx<'a, 'b>) {
if self.in_fixed_loop {
return;
}
let Expression::BooleanLiteral(lit) = expr else { return };
if self.options.booleans && !self.in_define_export {
let parent = ctx.ancestry.parent();
let no_unary = {
Expand Down Expand Up @@ -217,9 +231,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
} else {
ctx.ast.expression_unary(SPAN, UnaryOperator::LogicalNot, num)
};
true
} else {
false
self.changed = true;
}
}

Expand Down Expand Up @@ -562,7 +574,8 @@ mod test {

fn test(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let mut pass = super::PeepholeSubstituteAlternateSyntax::new(CompressOptions::default());
let mut pass =
super::PeepholeSubstituteAlternateSyntax::new(true, CompressOptions::default());
tester::test(&allocator, source_text, expected, &mut pass);
}

Expand Down
9 changes: 8 additions & 1 deletion crates/oxc_minifier/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ impl<'a> Compressor<'a> {
&mut PeepholeRemoveDeadCode::new(),
// TODO: MinimizeExitPoints
&mut PeepholeMinimizeConditions::new(),
&mut PeepholeSubstituteAlternateSyntax::new(self.options),
&mut PeepholeSubstituteAlternateSyntax::new(
/* in_fixed_loop */ true,
self.options,
),
&mut PeepholeReplaceKnownMethods::new(),
&mut PeepholeFoldConstants::new(),
];
Expand All @@ -75,6 +78,10 @@ impl<'a> Compressor<'a> {
// Passes listed in `getFinalization` in `DefaultPassConfig`
ExploitAssigns::new().build(program, &mut ctx);
CollapseVariableDeclarations::new(self.options).build(program, &mut ctx);

// Late latePeepholeOptimizations
PeepholeSubstituteAlternateSyntax::new(/* in_fixed_loop */ false, self.options)
.build(program, &mut ctx);
}

fn dead_code_elimination(program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/node_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a, 'b> Ctx<'a, 'b> {
}
ConstantValue::String(s) => self.ast.expression_string_literal(span, s),
ConstantValue::Boolean(b) => self.ast.expression_boolean_literal(span, b),
ConstantValue::Undefined => self.ast.void_0(span),
ConstantValue::Undefined => self.ast.expression_identifier_reference(span, "undefined"),
ConstantValue::Null => self.ast.expression_null_literal(span),
}
}
Expand Down

0 comments on commit f2c85b3

Please sign in to comment.