Fix block capture sync for bytecode locals

This commit is contained in:
sergeych committed 2026-01-30 15:39:03 +03:00
1 parent 89cf2c1612
commit ecf64dcbc3
3 files changed
+17 -15

No files matched your search

@@ -2116,6 +2116,7 @@ class BytecodeCompiler(
compileRefWithFallback(ref, null, target.pos)
}
}
is VarDeclStatement -> emitVarDecl(target)
is IfStatement -> compileIfStatement(target)
is net.sergeych.lyng.ForInStatement -> {
val resultSlot = emitForIn(target, false) ?: return null
@@ -1522,6 +1522,19 @@ class CmdFrame(
}
}
private fun shouldSyncLocalCaptures(captures: List<String>): Boolean {
if (captures.isEmpty()) return false
val localNames = fn.localSlotNames
if (localNames.isEmpty()) return false
for (capture in captures) {
for (local in localNames) {
if (local == null) continue
if (local == capture) return true
}
}
return false
}
private fun resolveModuleScope(scope: Scope): Scope {
var current: Scope? = scope
var last: Scope = scope
@@ -1545,7 +1558,7 @@ class CmdFrame(
} else {
emptyList()
}
if (captures.isNotEmpty() && fn.localSlotNames.isNotEmpty()) {
if (shouldSyncLocalCaptures(captures)) {
syncFrameToScope()
}
if (scope.skipScopeCreation) {
@@ -1584,8 +1597,8 @@ class CmdFrame(
?: error("Scope stack underflow in POP_SCOPE")
val captures = captureStack.removeLastOrNull() ?: emptyList()
scopeDepth -= 1
if (captures.isNotEmpty() && fn.localSlotNames.isNotEmpty()) {
syncScopeToFrame()
if (shouldSyncLocalCaptures(captures)) {
syncFrameToScope()
}
}