js-on-sx: hoist var across nested blocks; var-decls become set!
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 42s

JS var is function-scoped, but the transpiler only collected
top-level vars and re-emitted (define) everywhere; for-body var
shadowed the outer (un-hoisted) scope. Three-part fix:
1. js-collect-var-names recurses into js-block/js-for/js-while
   /js-do-while/js-if/js-try/js-switch/js-for-of-in;
2. var-kind decls emit (set! ...) instead of (define ...) since
   the binding is already created at function scope;
3. js-block uses js-transpile-stmt-list (no re-hoist) instead of
   js-transpile-stmts.
built-ins/Array: 17/45 → 18/45, String: 77/99 → 78/99.
conformance.sh: 148/148.
This commit is contained in:
2026-05-08 02:21:54 +00:00
parent 24a67fae97
commit 0df2b1c7b2
4 changed files with 93 additions and 56 deletions

View File

@@ -116,7 +116,8 @@
((js-tag? ast "js-arrow")
(js-transpile-arrow (nth ast 1) (nth ast 2)))
((js-tag? ast "js-program") (js-transpile-stmts (nth ast 1)))
((js-tag? ast "js-block") (js-transpile-stmts (nth ast 1)))
((js-tag? ast "js-block")
(cons (js-sym "begin") (js-transpile-stmt-list (nth ast 1))))
((js-tag? ast "js-exprstmt") (js-transpile (nth ast 1)))
((js-tag? ast "js-empty") nil)
((js-tag? ast "js-var")
@@ -509,11 +510,55 @@
(stmts)
(cond
((empty? stmts) (list))
((and (list? (first stmts)) (js-tag? (first stmts) "js-var") (= (nth (first stmts) 1) "var"))
(else
(append
(js-collect-var-decl-names (nth (first stmts) 2))
(js-collect-var-names (rest stmts))))
(else (js-collect-var-names (rest stmts))))))
(js-collect-var-names-stmt (first stmts))
(js-collect-var-names (rest stmts)))))))
(define
js-collect-var-names-stmt
(fn
(stmt)
(cond
((not (list? stmt)) (list))
((and (js-tag? stmt "js-var") (= (nth stmt 1) "var"))
(js-collect-var-decl-names (nth stmt 2)))
((js-tag? stmt "js-block") (js-collect-var-names (nth stmt 1)))
((js-tag? stmt "js-for")
(append
(js-collect-var-names-stmt (nth stmt 1))
(js-collect-var-names-stmt (nth stmt 4))))
((js-tag? stmt "js-for-of-in")
(js-collect-var-names-stmt (nth stmt 4)))
((js-tag? stmt "js-while")
(js-collect-var-names-stmt (nth stmt 2)))
((js-tag? stmt "js-do-while")
(js-collect-var-names-stmt (nth stmt 1)))
((js-tag? stmt "js-if")
(append
(js-collect-var-names-stmt (nth stmt 2))
(if (>= (len stmt) 4) (js-collect-var-names-stmt (nth stmt 3)) (list))))
((js-tag? stmt "js-try")
(append
(js-collect-var-names-stmt (nth stmt 1))
(if (and (>= (len stmt) 3) (list? (nth stmt 2)))
(js-collect-var-names-stmt (nth (nth stmt 2) 2))
(list))
(if (>= (len stmt) 4) (js-collect-var-names-stmt (nth stmt 3)) (list))))
((js-tag? stmt "js-switch")
(js-collect-var-names-cases (nth stmt 2)))
(else (list)))))
(define
js-collect-var-names-cases
(fn
(cases)
(cond
((empty? cases) (list))
(else
(append
(js-collect-var-names (nth (first cases) 2))
(js-collect-var-names-cases (rest cases)))))))
(define
js-dedup-names
@@ -985,12 +1030,12 @@
(define
js-transpile-var
(fn (kind decls) (cons (js-sym "begin") (js-vardecl-forms decls))))
(fn (kind decls) (cons (js-sym "begin") (js-vardecl-forms decls (= kind "var")))))
(define
js-vardecl-forms
(fn
(decls)
(decls is-var)
(cond
((empty? decls) (list))
(else
@@ -1000,10 +1045,10 @@
((js-tag? d "js-vardecl")
(cons
(list
(js-sym "define")
(js-sym (if is-var "set!" "define"))
(js-sym (nth d 1))
(js-transpile (nth d 2)))
(js-vardecl-forms (rest decls))))
(js-vardecl-forms (rest decls) is-var)))
((js-tag? d "js-vardecl-obj")
(let
((names (nth d 1))
@@ -1014,7 +1059,7 @@
(js-vardecl-obj-forms
names
tmp-sym
(js-vardecl-forms (rest decls))))))
(js-vardecl-forms (rest decls) is-var)))))
((js-tag? d "js-vardecl-arr")
(let
((names (nth d 1))
@@ -1026,7 +1071,7 @@
names
tmp-sym
0
(js-vardecl-forms (rest decls))))))
(js-vardecl-forms (rest decls) is-var)))))
(else (error "js-vardecl-forms: unexpected decl"))))))))
(define