Optimize .type_of() and .is_shared().

This commit is contained in:
Stephen Chung 2022-11-21 16:27:12 +08:00
parent 56631b1b66
commit 3feff3618a

View File

@ -923,6 +923,22 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
}
// var.rhs
(Expr::Variable(..), rhs) => optimize_expr(rhs, state, true),
// const.type_of()
(lhs, Expr::MethodCall(x, pos)) if lhs.is_constant() && x.name == KEYWORD_TYPE_OF && x.args.is_empty() => {
if let Some(value) = lhs.get_literal_value() {
state.set_dirty();
let typ = state.engine.map_type_name(value.type_name()).into();
*expr = Expr::from_dynamic(typ, *pos);
}
}
// const.is_shared()
#[cfg(not(feature = "no_closure"))]
(lhs, Expr::MethodCall(x, pos)) if lhs.is_constant() && x.name == crate::engine::KEYWORD_IS_SHARED && x.args.is_empty() => {
if let Some(..) = lhs.get_literal_value() {
state.set_dirty();
*expr = Expr::from_dynamic(Dynamic::FALSE, *pos);
}
}
// lhs.rhs
(lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, true); }
}