Fix panic in Function constructor with nested lexical bindings (#4531)#4645
Conversation
…ev#4531) Added optimize_scope_indices_function_constructor() to account for force_function_scope=true in the Function constructor's scope analysis. Closes boa-dev#4531
Test262 conformance changes
|
|
@Deepak-negi11 Could you fix the formatting issue? Everything else looks fine, so once that passes I'll approve the merge. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4645 +/- ##
==========================================
+ Coverage 47.24% 57.02% +9.78%
==========================================
Files 476 549 +73
Lines 46892 60138 +13246
==========================================
+ Hits 22154 34295 +12141
- Misses 24738 25843 +1105 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| analyze_binding_escapes(self, false, scope.clone(), interner)?; | ||
| optimize_scope_indices_function_constructor(self, scope); | ||
| Ok(()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
boa/core/engine/src/builtins/function/mod.rs
Lines 650 to 654 in 957e6f8
which avoids affecting all non-dynamic functions.
Fixes #4531
Problem
Function("function f() { const a = 42; return () => a; } return f()()")()panics with "index out of bounds" / "must be declarative environment".Root Cause
The
Functionconstructor compiles withforce_function_scope=true, butFunctionExpression::analyze_scopewas missing the scope index optimization step. The genericoptimize_scope_indicesalso didn't account for the forced function scope, causing compile-time scope indices to mismatch the runtime environment stack.Fix
Added
optimize_scope_indices_function_constructor()inscope_analyzer.rsthat calls visit_function_like directly with force_function_scope=true and call it from FunctionExpression::analyze_scope.Test
Added a regression test function_constructor_nested_lexical_binding.