Skip to content
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
8 changes: 6 additions & 2 deletions core/ast/src/function/ordinary_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::{
join_nodes,
operations::{ContainsSymbol, contains},
scope::{FunctionScopes, Scope},
scope_analyzer::{analyze_binding_escapes, collect_bindings},
scope_analyzer::{
analyze_binding_escapes, collect_bindings, optimize_scope_indices_function_constructor,
},
visitor::{VisitWith, Visitor, VisitorMut},
};
use boa_interner::{Interner, ToIndentedString};
Expand Down Expand Up @@ -281,7 +283,9 @@ impl FunctionExpression {
interner: &Interner,
) -> Result<(), &'static str> {
collect_bindings(self, strict, false, scope, interner)?;
analyze_binding_escapes(self, false, scope.clone(), interner)
analyze_binding_escapes(self, false, scope.clone(), interner)?;
optimize_scope_indices_function_constructor(self, scope);
Ok(())
Comment on lines +286 to +288
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got late to review this, but I'm wondering how this affects ordinary FunctionExpressions, since this not only touches all Function() calls, but also all (function() {}) style expressions.

Copy link
Member

@jedel1043 jedel1043 Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would just optimize scope indices two times instead of only one for (function(){}), so I think it would've been better to inline optimize_scope_indices_function_constructor into the specific place where it's used

if let Err(reason) =
function.analyze_scope(strict, context.realm().scope(), context.interner())
{
return Err(js_error!(SyntaxError: "failed to analyze function scope: {}", reason));
}

which avoids affecting all non-dynamic functions.

}
}

Expand Down
24 changes: 24 additions & 0 deletions core/ast/src/scope_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,30 @@ where
let _ = visitor.visit(node.into());
}

/// Like [`optimize_scope_indices`] but for a [`FunctionExpression`] compiled
/// by the `Function` constructor with `force_function_scope = true`.
///
/// The `Function` constructor always pushes a function scope at runtime, so
/// the optimizer must account for that even when the function scope would
/// otherwise be elided.
pub(crate) fn optimize_scope_indices_function_constructor(
node: &mut FunctionExpression,
scope: &Scope,
) {
let mut visitor = ScopeIndexVisitor {
index: scope.scope_index(),
};
let _ = visitor.visit_function_like(
&mut node.body,
&mut node.parameters,
&mut node.scopes,
&mut node.name_scope,
false,
// Always force the function scope for the Function constructor.
true,
);
}

struct ScopeIndexVisitor {
index: u32,
}
Expand Down
20 changes: 20 additions & 0 deletions core/engine/src/tests/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,23 @@ fn eval_out_of_scope() {
TestAction::assert_eq("f()", JsValue::null()),
]);
}

/// Regression test for issue #4531.
/// `Function` constructor with nested function containing lexical bindings
/// captured by a closure should not panic with "must be declarative environment".
#[test]
fn function_constructor_nested_lexical_binding() {
run_test_actions([TestAction::assert_eq(
indoc! {r#"
var code = "\
function f() {\
const a = 42;\
return () => { return a; };\
}\
return f()();\
";
Function(code)();
"#},
42,
)]);
}
Loading