Skip to content

Commit

Permalink
Merge dd80952 into 338e264
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Nov 17, 2023
2 parents 338e264 + dd80952 commit decec71
Show file tree
Hide file tree
Showing 18 changed files with 15,571 additions and 6,483 deletions.
11 changes: 11 additions & 0 deletions crates/turbopack-ecmascript/src/analyzer/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,17 @@ pub fn replace_builtin(value: &mut JsValue) -> bool {
true
}
}
JsValue::Tenary(_, test, cons, alt) => {
if test.is_truthy() == Some(true) {
*value = take(cons);
true
} else if test.is_falsy() == Some(true) {
*value = take(alt);
true
} else {
false
}
}
// match a binary operator like `a == b`
JsValue::Binary(..) => {
if let Some(v) = value.is_truthy() {
Expand Down
6 changes: 5 additions & 1 deletion crates/turbopack-ecmascript/src/analyzer/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,11 @@ impl EvalContext {
self.eval(alt)
}
} else {
JsValue::alternatives(vec![self.eval(cons), self.eval(alt)])
JsValue::tenary(
Box::new(test),
Box::new(self.eval(cons)),
Box::new(self.eval(alt)),
)
}
}

Expand Down
59 changes: 59 additions & 0 deletions crates/turbopack-ecmascript/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ pub enum JsValue {
/// A member access `obj[prop]`
/// `(total_node_count, obj, prop)`
Member(usize, Box<JsValue>, Box<JsValue>),
/// A tenary operator `test ? cons : alt`
/// `(total_node_count, test, cons, alt)`
Tenary(usize, Box<JsValue>, Box<JsValue>, Box<JsValue>),

// PLACEHOLDERS
// ----------------------------
Expand Down Expand Up @@ -573,6 +576,7 @@ impl Display for JsValue {
.join(op.joiner())
),
JsValue::Binary(_, a, op, b) => write!(f, "({}{}{})", a, op.joiner(), b),
JsValue::Tenary(_, test, cons, alt) => write!(f, "({} ? {} : {})", test, cons, alt),
JsValue::Call(_, callee, list) => write!(
f,
"{}({})",
Expand Down Expand Up @@ -685,6 +689,7 @@ impl JsValue {
| JsValue::Binary(..)
| JsValue::Call(..)
| JsValue::SuperCall(..)
| JsValue::Tenary(..)
| JsValue::MemberCall(..) => JsValueMetaKind::Operation,
JsValue::Variable(..)
| JsValue::Argument(..)
Expand Down Expand Up @@ -724,6 +729,15 @@ impl JsValue {
)
}

pub fn tenary(test: Box<JsValue>, cons: Box<JsValue>, alt: Box<JsValue>) -> Self {
Self::Tenary(
1 + test.total_nodes() + cons.total_nodes() + alt.total_nodes(),
test,
cons,
alt,
)
}

pub fn equal(a: JsValue, b: JsValue) -> Self {
Self::Binary(
1 + a.total_nodes() + b.total_nodes(),
Expand Down Expand Up @@ -864,6 +878,7 @@ impl JsValue {
| JsValue::Not(c, _)
| JsValue::Logical(c, _, _)
| JsValue::Binary(c, _, _, _)
| JsValue::Tenary(c, _, _, _)
| JsValue::Call(c, _, _)
| JsValue::SuperCall(c, _)
| JsValue::MemberCall(c, _, _, _)
Expand Down Expand Up @@ -899,6 +914,9 @@ impl JsValue {
JsValue::Binary(c, a, _, b) => {
*c = 1 + a.total_nodes() + b.total_nodes();
}
JsValue::Tenary(c, test, cons, alt) => {
*c = 1 + test.total_nodes() + cons.total_nodes() + alt.total_nodes();
}
JsValue::Not(c, r) => {
*c = 1 + r.total_nodes();
}
Expand Down Expand Up @@ -1009,6 +1027,10 @@ impl JsValue {
make_max_unknown([&mut **o, &mut **p].into_iter().chain(args.iter_mut()));
self.update_total_nodes();
}
JsValue::Tenary(_, test, cons, alt) => {
make_max_unknown([&mut **test, &mut **cons, &mut **alt].into_iter());
self.update_total_nodes();
}
JsValue::Member(_, o, p) => {
make_max_unknown([&mut **o, &mut **p].into_iter());
self.update_total_nodes();
Expand Down Expand Up @@ -1224,6 +1246,12 @@ impl JsValue {
op.joiner(),
b.explain_internal_inner(hints, indent_depth, depth, unknown_depth),
),
JsValue::Tenary(_, test, cons, alt) => format!(
"({} ? {} : {})",
test.explain_internal_inner(hints, indent_depth, depth, unknown_depth),
cons.explain_internal_inner(hints, indent_depth, depth, unknown_depth),
alt.explain_internal_inner(hints, indent_depth, depth, unknown_depth),
),
JsValue::Not(_, value) => format!(
"!({})",
value.explain_internal_inner(hints, indent_depth, depth, unknown_depth)
Expand Down Expand Up @@ -1873,6 +1901,7 @@ impl JsValue {
| JsValue::Call(..)
| JsValue::MemberCall(..)
| JsValue::Member(..)
| JsValue::Tenary(..)
| JsValue::SuperCall(..) => None,
}
}
Expand Down Expand Up @@ -2128,6 +2157,16 @@ macro_rules! for_each_children_async {
$value.update_total_nodes();
($value, m1 || m2)
}
JsValue::Tenary(_, box test, box cons, box alt) => {
let (v, m1) = $visit_fn(take(test), $($args),+).await?;
*test = v;
let (v, m2) = $visit_fn(take(cons), $($args),+).await?;
*cons = v;
let (v, m3) = $visit_fn(take(alt), $($args),+).await?;
*alt = v;
$value.update_total_nodes();
($value, m1 || m2 || m3)
}
JsValue::Member(_, box obj, box prop) => {
let (v, m1) = $visit_fn(take(obj), $($args),+).await?;
*obj = v;
Expand Down Expand Up @@ -2398,6 +2437,16 @@ impl JsValue {
}
modified
}
JsValue::Tenary(_, test, cons, alt) => {
let m1 = visitor(test);
let m2 = visitor(cons);
let m3 = visitor(alt);
let modified = m1 || m2 || m3;
if modified {
self.update_total_nodes();
}
modified
}
JsValue::Member(_, obj, prop) => {
let m1 = visitor(obj);
let m2 = visitor(prop);
Expand Down Expand Up @@ -2558,6 +2607,11 @@ impl JsValue {
visitor(a);
visitor(b);
}
JsValue::Tenary(_, test, cons, alt) => {
visitor(test);
visitor(cons);
visitor(alt);
}
JsValue::Constant(_)
| JsValue::FreeVar(_)
| JsValue::Variable(_)
Expand Down Expand Up @@ -2893,6 +2947,11 @@ impl JsValue {
o.hash(state);
b.similar_hash(state, depth - 1);
}
JsValue::Tenary(_, test, cons, alt) => {
test.similar_hash(state, depth - 1);
cons.similar_hash(state, depth - 1);
alt.similar_hash(state, depth - 1);
}
JsValue::Module(ModuleValue {
module: v,
annotations: a,
Expand Down
Loading

0 comments on commit decec71

Please sign in to comment.