|
|
use std::mem::take; |
|
|
|
|
|
use swc_core::ecma::atoms::atom; |
|
|
|
|
|
use super::{ConstantNumber, ConstantValue, JsValue, LogicalOperator, LogicalProperty, ObjectPart}; |
|
|
use crate::analyzer::JsValueUrlKind; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn early_replace_builtin(value: &mut JsValue) -> bool { |
|
|
match value { |
|
|
|
|
|
JsValue::Call(_, box callee, args) => { |
|
|
let args_have_side_effects = || args.iter().any(|arg| arg.has_side_effects()); |
|
|
match callee { |
|
|
|
|
|
&mut JsValue::Unknown { |
|
|
original_value: _, |
|
|
reason: _, |
|
|
has_side_effects, |
|
|
} => { |
|
|
let has_side_effects = has_side_effects || args_have_side_effects(); |
|
|
value.make_unknown(has_side_effects, "unknown callee"); |
|
|
true |
|
|
} |
|
|
|
|
|
|
|
|
JsValue::Constant(_) |
|
|
| JsValue::Url(_, _) |
|
|
| JsValue::WellKnownObject(_) |
|
|
| JsValue::Array { .. } |
|
|
| JsValue::Object { .. } |
|
|
| JsValue::Alternatives { .. } |
|
|
| JsValue::Concat(_, _) |
|
|
| JsValue::Add(_, _) |
|
|
| JsValue::Not(_, _) => { |
|
|
let has_side_effects = args_have_side_effects(); |
|
|
value.make_unknown(has_side_effects, "non-function callee"); |
|
|
true |
|
|
} |
|
|
_ => false, |
|
|
} |
|
|
} |
|
|
|
|
|
JsValue::MemberCall(_, box obj, box prop, args) => { |
|
|
let args_have_side_effects = || args.iter().any(|arg| arg.has_side_effects()); |
|
|
match obj { |
|
|
|
|
|
&mut JsValue::Unknown { |
|
|
original_value: _, |
|
|
reason: _, |
|
|
has_side_effects, |
|
|
} => { |
|
|
let side_effects = |
|
|
has_side_effects || prop.has_side_effects() || args_have_side_effects(); |
|
|
value.make_unknown(side_effects, "unknown callee object"); |
|
|
true |
|
|
} |
|
|
|
|
|
_ => match prop { |
|
|
|
|
|
&mut JsValue::Unknown { |
|
|
original_value: _, |
|
|
reason: _, |
|
|
has_side_effects, |
|
|
} => { |
|
|
let side_effects = has_side_effects || args_have_side_effects(); |
|
|
value.make_unknown(side_effects, "unknown callee property"); |
|
|
true |
|
|
} |
|
|
_ => false, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
&mut JsValue::Member( |
|
|
_, |
|
|
box JsValue::Unknown { |
|
|
original_value: _, |
|
|
reason: _, |
|
|
has_side_effects, |
|
|
}, |
|
|
box ref mut prop, |
|
|
) => { |
|
|
let side_effects = has_side_effects || prop.has_side_effects(); |
|
|
value.make_unknown(side_effects, "unknown object"); |
|
|
true |
|
|
} |
|
|
_ => false, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn replace_builtin(value: &mut JsValue) -> bool { |
|
|
match value { |
|
|
JsValue::Add(_, list) => { |
|
|
|
|
|
let mut sum = 0f64; |
|
|
for arg in list { |
|
|
let JsValue::Constant(ConstantValue::Num(num)) = arg else { |
|
|
return false; |
|
|
}; |
|
|
sum += num.0; |
|
|
} |
|
|
*value = JsValue::Constant(ConstantValue::Num(ConstantNumber(sum))); |
|
|
true |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
JsValue::Member(_, box obj, prop) => match obj { |
|
|
|
|
|
|
|
|
|
|
|
JsValue::Alternatives { |
|
|
total_nodes: _, |
|
|
values, |
|
|
logical_property: _, |
|
|
} => { |
|
|
*value = JsValue::alternatives( |
|
|
take(values) |
|
|
.into_iter() |
|
|
.map(|alt| JsValue::member(Box::new(alt), prop.clone())) |
|
|
.collect(), |
|
|
); |
|
|
true |
|
|
} |
|
|
|
|
|
&mut JsValue::Array { |
|
|
ref mut items, |
|
|
mutable, |
|
|
.. |
|
|
} => { |
|
|
fn items_to_alternatives(items: &mut Vec<JsValue>, prop: &mut JsValue) -> JsValue { |
|
|
items.push(JsValue::unknown( |
|
|
JsValue::member(Box::new(JsValue::array(Vec::new())), Box::new(take(prop))), |
|
|
false, |
|
|
"unknown array prototype methods or values", |
|
|
)); |
|
|
JsValue::alternatives(take(items)) |
|
|
} |
|
|
match &mut **prop { |
|
|
|
|
|
|
|
|
JsValue::Constant(ConstantValue::Num(num @ ConstantNumber(_))) => { |
|
|
if let Some(index) = num.as_u32_index() { |
|
|
if index < items.len() { |
|
|
*value = items.swap_remove(index); |
|
|
if mutable { |
|
|
value.add_unknown_mutations(true); |
|
|
} |
|
|
true |
|
|
} else { |
|
|
*value = JsValue::unknown( |
|
|
JsValue::member(Box::new(take(obj)), Box::new(take(prop))), |
|
|
false, |
|
|
"invalid index", |
|
|
); |
|
|
true |
|
|
} |
|
|
} else { |
|
|
value.make_unknown(false, "non-num constant property on array"); |
|
|
true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
JsValue::Constant(_) => { |
|
|
value.make_unknown(false, "non-num constant property on array"); |
|
|
true |
|
|
} |
|
|
|
|
|
|
|
|
JsValue::Alternatives { |
|
|
total_nodes: _, |
|
|
values, |
|
|
logical_property: _, |
|
|
} => { |
|
|
*value = JsValue::alternatives( |
|
|
take(values) |
|
|
.into_iter() |
|
|
.map(|alt| JsValue::member(Box::new(obj.clone()), Box::new(alt))) |
|
|
.collect(), |
|
|
); |
|
|
true |
|
|
} |
|
|
|
|
|
|
|
|
_ => { |
|
|
*value = items_to_alternatives(items, prop); |
|
|
true |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
&mut JsValue::Object { |
|
|
ref mut parts, |
|
|
mutable, |
|
|
.. |
|
|
} => { |
|
|
fn parts_to_alternatives( |
|
|
parts: &mut Vec<ObjectPart>, |
|
|
prop: &mut Box<JsValue>, |
|
|
include_unknown: bool, |
|
|
) -> JsValue { |
|
|
let mut values = Vec::new(); |
|
|
for part in parts { |
|
|
match part { |
|
|
ObjectPart::KeyValue(_, value) => { |
|
|
values.push(take(value)); |
|
|
} |
|
|
ObjectPart::Spread(_) => { |
|
|
values.push(JsValue::unknown( |
|
|
JsValue::member( |
|
|
Box::new(JsValue::object(vec![take(part)])), |
|
|
prop.clone(), |
|
|
), |
|
|
true, |
|
|
"spread object", |
|
|
)); |
|
|
} |
|
|
} |
|
|
} |
|
|
if include_unknown { |
|
|
values.push(JsValue::unknown( |
|
|
JsValue::member( |
|
|
Box::new(JsValue::object(Vec::new())), |
|
|
Box::new(take(prop)), |
|
|
), |
|
|
true, |
|
|
"unknown object prototype methods or values", |
|
|
)); |
|
|
} |
|
|
JsValue::alternatives(values) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn potential_values_to_alternatives( |
|
|
mut potential_values: Vec<usize>, |
|
|
parts: &mut Vec<ObjectPart>, |
|
|
prop: &mut Box<JsValue>, |
|
|
include_unknown: bool, |
|
|
) -> JsValue { |
|
|
|
|
|
let mut potential_values = take(parts) |
|
|
.into_iter() |
|
|
.enumerate() |
|
|
.filter(|(i, _)| { |
|
|
if potential_values.last() == Some(i) { |
|
|
potential_values.pop(); |
|
|
true |
|
|
} else { |
|
|
false |
|
|
} |
|
|
}) |
|
|
.map(|(_, part)| part) |
|
|
.collect(); |
|
|
parts_to_alternatives(&mut potential_values, prop, include_unknown) |
|
|
} |
|
|
|
|
|
match &mut **prop { |
|
|
|
|
|
|
|
|
JsValue::Constant(ConstantValue::Str(_)) => { |
|
|
let prop_str = prop.as_str().unwrap(); |
|
|
let mut potential_values = Vec::new(); |
|
|
for (i, part) in parts.iter_mut().enumerate().rev() { |
|
|
match part { |
|
|
ObjectPart::KeyValue(key, val) => { |
|
|
if let Some(key) = key.as_str() { |
|
|
if key == prop_str { |
|
|
if potential_values.is_empty() { |
|
|
*value = take(val); |
|
|
} else { |
|
|
potential_values.push(i); |
|
|
*value = potential_values_to_alternatives( |
|
|
potential_values, |
|
|
parts, |
|
|
prop, |
|
|
false, |
|
|
); |
|
|
} |
|
|
if mutable { |
|
|
value.add_unknown_mutations(true); |
|
|
} |
|
|
return true; |
|
|
} |
|
|
} else { |
|
|
potential_values.push(i); |
|
|
} |
|
|
} |
|
|
ObjectPart::Spread(_) => { |
|
|
value.make_unknown(true, "spread object"); |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
if potential_values.is_empty() { |
|
|
*value = JsValue::FreeVar(atom!("undefined")); |
|
|
} else { |
|
|
*value = potential_values_to_alternatives( |
|
|
potential_values, |
|
|
parts, |
|
|
prop, |
|
|
true, |
|
|
); |
|
|
} |
|
|
if mutable { |
|
|
value.add_unknown_mutations(true); |
|
|
} |
|
|
true |
|
|
} |
|
|
|
|
|
|
|
|
JsValue::Alternatives { |
|
|
total_nodes: _, |
|
|
values, |
|
|
logical_property: _, |
|
|
} => { |
|
|
*value = JsValue::alternatives( |
|
|
take(values) |
|
|
.into_iter() |
|
|
.map(|alt| JsValue::member(Box::new(obj.clone()), Box::new(alt))) |
|
|
.collect(), |
|
|
); |
|
|
true |
|
|
} |
|
|
_ => { |
|
|
*value = parts_to_alternatives(parts, prop, true); |
|
|
true |
|
|
} |
|
|
} |
|
|
} |
|
|
_ => false, |
|
|
}, |
|
|
|
|
|
JsValue::MemberCall(_, box obj, box prop, args) => { |
|
|
match obj { |
|
|
|
|
|
JsValue::Array { items, mutable, .. } => { |
|
|
|
|
|
if let Some(str) = prop.as_str() { |
|
|
match str { |
|
|
|
|
|
"concat" => { |
|
|
if args.iter().all(|arg| { |
|
|
matches!( |
|
|
arg, |
|
|
JsValue::Array { .. } |
|
|
| JsValue::Constant(_) |
|
|
| JsValue::Url(_, JsValueUrlKind::Absolute) |
|
|
| JsValue::Concat(..) |
|
|
| JsValue::Add(..) |
|
|
| JsValue::WellKnownObject(_) |
|
|
| JsValue::WellKnownFunction(_) |
|
|
| JsValue::Function(..) |
|
|
) |
|
|
}) { |
|
|
for arg in args { |
|
|
match arg { |
|
|
JsValue::Array { |
|
|
items: inner, |
|
|
mutable: inner_mutable, |
|
|
.. |
|
|
} => { |
|
|
items.extend(take(inner)); |
|
|
*mutable |= *inner_mutable; |
|
|
} |
|
|
JsValue::Constant(_) |
|
|
| JsValue::Url(_, JsValueUrlKind::Absolute) |
|
|
| JsValue::Concat(..) |
|
|
| JsValue::Add(..) |
|
|
| JsValue::WellKnownObject(_) |
|
|
| JsValue::WellKnownFunction(_) |
|
|
| JsValue::Function(..) => { |
|
|
items.push(take(arg)); |
|
|
} |
|
|
_ => { |
|
|
unreachable!(); |
|
|
} |
|
|
} |
|
|
} |
|
|
obj.update_total_nodes(); |
|
|
*value = take(obj); |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
"map" => { |
|
|
if let Some(func) = args.first() { |
|
|
*value = JsValue::array( |
|
|
take(items) |
|
|
.into_iter() |
|
|
.enumerate() |
|
|
.map(|(i, item)| { |
|
|
JsValue::call( |
|
|
Box::new(func.clone()), |
|
|
vec![ |
|
|
item, |
|
|
JsValue::Constant(ConstantValue::Num( |
|
|
ConstantNumber(i as f64), |
|
|
)), |
|
|
], |
|
|
) |
|
|
}) |
|
|
.collect(), |
|
|
); |
|
|
return true; |
|
|
} |
|
|
} |
|
|
_ => {} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
JsValue::Alternatives { |
|
|
total_nodes: _, |
|
|
values, |
|
|
logical_property: _, |
|
|
} => { |
|
|
*value = JsValue::alternatives( |
|
|
take(values) |
|
|
.into_iter() |
|
|
.map(|alt| { |
|
|
JsValue::member_call( |
|
|
Box::new(alt), |
|
|
Box::new(prop.clone()), |
|
|
args.clone(), |
|
|
) |
|
|
}) |
|
|
.collect(), |
|
|
); |
|
|
return true; |
|
|
} |
|
|
_ => {} |
|
|
} |
|
|
|
|
|
|
|
|
if obj.is_string() == Some(true) |
|
|
&& let Some(str) = prop.as_str() |
|
|
{ |
|
|
|
|
|
if str == "concat" { |
|
|
let mut values = vec![take(obj)]; |
|
|
values.extend(take(args)); |
|
|
|
|
|
*value = JsValue::concat(values); |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*value = JsValue::call( |
|
|
Box::new(JsValue::member(Box::new(take(obj)), Box::new(take(prop)))), |
|
|
take(args), |
|
|
); |
|
|
true |
|
|
} |
|
|
|
|
|
|
|
|
JsValue::Call( |
|
|
_, |
|
|
box JsValue::Alternatives { |
|
|
total_nodes: _, |
|
|
values, |
|
|
logical_property: _, |
|
|
}, |
|
|
args, |
|
|
) => { |
|
|
*value = JsValue::alternatives( |
|
|
take(values) |
|
|
.into_iter() |
|
|
.map(|alt| JsValue::call(Box::new(alt), args.clone())) |
|
|
.collect(), |
|
|
); |
|
|
true |
|
|
} |
|
|
|
|
|
JsValue::Object { parts, mutable, .. } => { |
|
|
|
|
|
if parts |
|
|
.iter() |
|
|
.any(|part| matches!(part, ObjectPart::Spread(JsValue::Object { .. }))) |
|
|
{ |
|
|
let old_parts = take(parts); |
|
|
for part in old_parts { |
|
|
if let ObjectPart::Spread(JsValue::Object { |
|
|
parts: inner_parts, |
|
|
mutable: inner_mutable, |
|
|
.. |
|
|
}) = part |
|
|
{ |
|
|
parts.extend(inner_parts); |
|
|
*mutable |= inner_mutable; |
|
|
} else { |
|
|
parts.push(part); |
|
|
} |
|
|
} |
|
|
value.update_total_nodes(); |
|
|
true |
|
|
} else { |
|
|
false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
JsValue::Logical(_, op, parts) => { |
|
|
let len = parts.len(); |
|
|
let input_parts: Vec<JsValue> = take(parts); |
|
|
*parts = Vec::with_capacity(len); |
|
|
let mut part_properties = Vec::with_capacity(len); |
|
|
for (i, part) in input_parts.into_iter().enumerate() { |
|
|
|
|
|
if i == len - 1 { |
|
|
|
|
|
|
|
|
parts.push(part); |
|
|
break; |
|
|
} |
|
|
let property = match op { |
|
|
LogicalOperator::And => part.is_truthy(), |
|
|
LogicalOperator::Or => part.is_falsy(), |
|
|
LogicalOperator::NullishCoalescing => part.is_nullish(), |
|
|
}; |
|
|
|
|
|
match property { |
|
|
Some(true) => { |
|
|
|
|
|
continue; |
|
|
} |
|
|
Some(false) => { |
|
|
|
|
|
part_properties.push(property); |
|
|
parts.push(part); |
|
|
break; |
|
|
} |
|
|
None => { |
|
|
|
|
|
part_properties.push(property); |
|
|
parts.push(part); |
|
|
continue; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if parts.len() == 1 { |
|
|
*value = parts.pop().unwrap(); |
|
|
true |
|
|
} else { |
|
|
|
|
|
let last_part = parts.last().unwrap(); |
|
|
let property = match op { |
|
|
LogicalOperator::And => last_part.is_truthy(), |
|
|
LogicalOperator::Or => last_part.is_falsy(), |
|
|
LogicalOperator::NullishCoalescing => last_part.is_nullish(), |
|
|
}; |
|
|
part_properties.push(property); |
|
|
let (any_unset, all_set) = |
|
|
part_properties |
|
|
.iter() |
|
|
.fold((false, true), |(any_unset, all_set), part| match part { |
|
|
Some(true) => (any_unset, all_set), |
|
|
Some(false) => (true, false), |
|
|
None => (any_unset, false), |
|
|
}); |
|
|
let property = match op { |
|
|
LogicalOperator::Or => { |
|
|
if any_unset { |
|
|
Some(LogicalProperty::Truthy) |
|
|
} else if all_set { |
|
|
Some(LogicalProperty::Falsy) |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} |
|
|
LogicalOperator::And => { |
|
|
if any_unset { |
|
|
Some(LogicalProperty::Falsy) |
|
|
} else if all_set { |
|
|
Some(LogicalProperty::Truthy) |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} |
|
|
LogicalOperator::NullishCoalescing => { |
|
|
if any_unset { |
|
|
Some(LogicalProperty::NonNullish) |
|
|
} else if all_set { |
|
|
Some(LogicalProperty::Nullish) |
|
|
} else { |
|
|
None |
|
|
} |
|
|
} |
|
|
}; |
|
|
if let Some(property) = property { |
|
|
*value = JsValue::alternatives_with_additional_property(take(parts), property); |
|
|
true |
|
|
} else { |
|
|
*value = JsValue::alternatives(take(parts)); |
|
|
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 |
|
|
} |
|
|
} |
|
|
|
|
|
JsValue::Binary(..) => { |
|
|
if let Some(v) = value.is_truthy() { |
|
|
let v = if v { |
|
|
ConstantValue::True |
|
|
} else { |
|
|
ConstantValue::False |
|
|
}; |
|
|
*value = JsValue::Constant(v); |
|
|
true |
|
|
} else { |
|
|
false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
JsValue::Not(_, inner) => match inner.is_truthy() { |
|
|
Some(true) => { |
|
|
*value = JsValue::Constant(ConstantValue::False); |
|
|
true |
|
|
} |
|
|
Some(false) => { |
|
|
*value = JsValue::Constant(ConstantValue::True); |
|
|
true |
|
|
} |
|
|
None => false, |
|
|
}, |
|
|
|
|
|
JsValue::Iterated(_, iterable) => { |
|
|
if let JsValue::Array { items, mutable, .. } = &mut **iterable { |
|
|
let mut new_value = JsValue::alternatives(take(items)); |
|
|
if *mutable { |
|
|
new_value.add_unknown_mutations(true); |
|
|
} |
|
|
*value = new_value; |
|
|
true |
|
|
} else { |
|
|
false |
|
|
} |
|
|
} |
|
|
|
|
|
JsValue::Awaited(_, operand) => { |
|
|
if let JsValue::Promise(_, inner) = &mut **operand { |
|
|
*value = take(inner); |
|
|
true |
|
|
} else { |
|
|
*value = take(operand); |
|
|
true |
|
|
} |
|
|
} |
|
|
|
|
|
_ => false, |
|
|
} |
|
|
} |
|
|
|