Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip creation of arguments object if possible #4087

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
hansl marked this conversation as resolved.
Show resolved Hide resolved
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
Loading