Skip to content

Commit

Permalink
Skip creation of arguments object if possible (#4087)
Browse files Browse the repository at this point in the history
* Skip creation of arguments object if possible

* fix lints
  • Loading branch information
raskad authored Dec 19, 2024
1 parent 12faeca commit d870057
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions core/ast/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct Binding {
lex: bool,
strict: bool,
escapes: bool,
accessed: bool,
}

/// A scope maps bound identifiers to their binding positions.
Expand Down Expand Up @@ -259,6 +260,7 @@ impl Scope {
.iter_mut()
.find(|b| &b.name == name)
{
binding.accessed = true;
if crossed_function_border || eval_or_with {
binding.escapes = true;
}
Expand Down Expand Up @@ -296,6 +298,7 @@ impl Scope {
lex: !function_scope,
strict: false,
escapes: self.is_global(),
accessed: false,
});
BindingLocator::declarative(
name,
Expand All @@ -320,6 +323,7 @@ impl Scope {
lex: true,
strict,
escapes: self.is_global(),
accessed: false,
});
}

Expand Down Expand Up @@ -583,6 +587,37 @@ impl FunctionScopes {
&self.function_scope
}

/// Returns if the arguments object is accessed in this function.
#[must_use]
pub fn arguments_object_accessed(&self) -> bool {
if self
.function_scope
.inner
.bindings
.borrow()
.first()
.filter(|b| b.name == "arguments" && b.accessed)
.is_some()
{
return true;
}

if let Some(scope) = &self.parameters_eval_scope {
if scope
.inner
.bindings
.borrow()
.first()
.filter(|b| b.name == "arguments" && b.accessed)
.is_some()
{
return true;
}
}

false
}

/// Returns the parameters eval scope for this function.
#[must_use]
pub fn parameters_eval_scope(&self) -> Option<&Scope> {
Expand Down
4 changes: 4 additions & 0 deletions core/engine/src/bytecompiler/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,10 @@ impl ByteCompiler<'_> {
}
}

if arguments_object_needed {
arguments_object_needed = scopes.arguments_object_accessed();
}

// 19-20
drop(self.push_declarative_scope(scopes.parameters_eval_scope()));

Expand Down

0 comments on commit d870057

Please sign in to comment.