Skip to content

Commit

Permalink
Implement optimization identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Aug 1, 2023
1 parent facc473 commit ed60d63
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
60 changes: 60 additions & 0 deletions boa_ast/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2156,3 +2156,63 @@ impl<'ast> Visitor<'ast> for ReturnsValueVisitor {
ControlFlow::Continue(())
}
}

/// Returns `true` if the given statement can optimize local variables.
#[must_use]
pub fn can_optimize_local_variables<'a, N>(node: &'a N) -> bool
where
&'a N: Into<NodeRef<'a>>,
{
CanOptimizeLocalVariables.visit(node.into()).is_continue()
}

/// The [`Visitor`] used for [`returns_value`].
#[derive(Debug)]
struct CanOptimizeLocalVariables;

impl<'ast> Visitor<'ast> for CanOptimizeLocalVariables {
type BreakTy = ();

fn visit_with(&mut self, _node: &'ast crate::statement::With) -> ControlFlow<Self::BreakTy> {
ControlFlow::Break(())
}

fn visit_call(&mut self, node: &'ast crate::expression::Call) -> ControlFlow<Self::BreakTy> {
if let Expression::Identifier(identifier) = node.function() {
if identifier.sym() == Sym::EVAL {
return ControlFlow::Break(());
}
}

try_break!(node.function().visit_with(self));

for arg in node.args() {
try_break!(arg.visit_with(self));
}

ControlFlow::Continue(())
}

fn visit_function(&mut self, _node: &'ast Function) -> ControlFlow<Self::BreakTy> {
ControlFlow::Break(())
}

fn visit_arrow_function(&mut self, _node: &'ast ArrowFunction) -> ControlFlow<Self::BreakTy> {
ControlFlow::Break(())
}

fn visit_async_function(&mut self, _node: &'ast AsyncFunction) -> ControlFlow<Self::BreakTy> {
ControlFlow::Break(())
}

fn visit_async_arrow_function(
&mut self,
_node: &'ast AsyncArrowFunction,
) -> ControlFlow<Self::BreakTy> {
ControlFlow::Break(())
}

fn visit_class(&mut self, _node: &'ast Class) -> ControlFlow<Self::BreakTy> {
ControlFlow::Break(())
}
}
18 changes: 17 additions & 1 deletion boa_engine/src/bytecompiler/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use crate::{
vm::{CodeBlock, CodeBlockFlags, Opcode},
Context,
};
use boa_ast::function::{FormalParameterList, FunctionBody};
use boa_ast::{
function::{FormalParameterList, FunctionBody},
operations::can_optimize_local_variables,
};
use boa_gc::Gc;
use boa_interner::Sym;

Expand Down Expand Up @@ -148,6 +151,19 @@ impl FunctionCompiler {
compiler.async_handler = Some(compiler.push_handler());
}

let can_optimize_params = can_optimize_local_variables(parameters);
let can_optimize_body = can_optimize_local_variables(body);
println!("Can optimize params: {can_optimize_params}");
println!("Can optimize body: {can_optimize_body}");

let can_optimize = can_optimize_params
&& can_optimize_body
&& !parameters.has_arguments()
&& !parameters.has_duplicates()
&& !parameters.has_rest_parameter()
&& parameters.is_simple();
println!("Can optimize function: {can_optimize}");

let (env_label, additional_env) = compiler.function_declaration_instantiation(
body,
parameters,
Expand Down

0 comments on commit ed60d63

Please sign in to comment.