Fix stdlib drop and add bytecode return/break test

This commit is contained in:
Sergey Chernov 2026-01-28 08:54:54 +03:00
parent 8dfdbaa0a0
commit 7b3d92beb9
2 changed files with 33 additions and 1 deletions

View File

@ -5102,4 +5102,32 @@ class ScriptTest {
assertEquals(ObjInt(3), r1)
assertEquals(ObjInt(4), r2)
}
@Test
fun testReturnBreakValueBytecodeDisasm() = runTest {
val scope = Script.newScope()
scope.eval("""
fun firstPositive() {
for (i in 0..5)
if (i > 0) return i
-1
}
fun firstEvenOrMinus() {
val r = for (i in 1..7)
if (i % 2 == 0) break i
r
}
""".trimIndent())
val disasmReturn = scope.disassembleSymbol("firstPositive")
val disasmBreak = scope.disassembleSymbol("firstEvenOrMinus")
println("[DEBUG_LOG] firstPositive disasm:\n$disasmReturn")
println("[DEBUG_LOG] firstEvenOrMinus disasm:\n$disasmBreak")
assertFalse(disasmReturn.contains("not a compiled body"))
assertFalse(disasmBreak.contains("not a compiled body"))
assertFalse(disasmReturn.contains("EVAL_FALLBACK"))
assertFalse(disasmBreak.contains("EVAL_FALLBACK"))
assertEquals(ObjInt(1), scope.eval("firstPositive()"))
assertEquals(ObjInt(2), scope.eval("firstEvenOrMinus()"))
}
}

View File

@ -65,7 +65,11 @@ fun Iterable.filterNotNull(): List {
/* Skip the first N elements of this iterable. */
fun Iterable.drop(n) {
var cnt = 0
filter { cnt++ >= n }
val result = []
for( item in this ) {
if( cnt++ >= n ) result.add(item)
}
result
}
/* Return the first element or throw if the iterable is empty. */