fix: count pass 注册顶层 var 名,hoisted 函数声明可解析外层 var

This commit is contained in:
2026-06-30 16:37:50 +08:00
parent a34c289d47
commit 930b14b3f0
4 changed files with 50 additions and 15 deletions
@@ -58,6 +58,20 @@ fn while_then_hoisted_function() {
assert_eq!(eval("var n=0; while(n<3){n=n+1;} function f(){return 100} n + f()"), "103");
}
// A hoisted function declaration is emitted before the `var` statements it closes over,
// so the count pass must register top-level `var` names too (not just function names);
// otherwise the hoisted body cannot resolve the outer var ("Identifier not defined").
#[test]
fn hoisted_function_reads_outer_var() {
assert_eq!(eval("var s=3; function f(){return s} f()"), "3");
}
#[test]
fn hoisted_function_reads_outer_var_and_function() {
assert_eq!(eval("var s=3; function g(){return 5} function f(){return g()+s} f()"), "8");
}
#[test]
fn coalesce_nonsimple_then_for() {
assert_eq!(