From 3feff3618a027443bb4fd768753adc7500ee1732 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 21 Nov 2022 16:27:12 +0800 Subject: [PATCH] Optimize .type_of() and .is_shared(). --- src/optimizer.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/optimizer.rs b/src/optimizer.rs index a43fe984..39ae36c0 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -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); } }