Skip to content

Commit

Permalink
fix and test return behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraks committed Oct 10, 2024
1 parent a589f8b commit e2315b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
12 changes: 6 additions & 6 deletions benchmarks/wasm/return.wat
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(module
(type (;0;) (func))
(func (;0;) (type 0)
block ;; label = @1
(func (result i32)
block
i32.const 42
return
end
unreachable
)
(func (;1;) (type 0)
(func (result i32)
call 0
unreachable
)
(start 1)
)
)
6 changes: 4 additions & 2 deletions src/main/scala/wasm/MiniWasm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import gensym.wasm.memory._
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.HashMap

case class Trap() extends Exception

case class ModuleInstance(
types: List[FuncType],
funcs: HashMap[Int, WIR],
Expand Down Expand Up @@ -259,7 +261,7 @@ object Evaluator {
eval(rest, I32V(value) :: newStack, frame, kont, trail, ret)
case Nop =>
eval(rest, stack, frame, kont, trail, ret)
case Unreachable => throw new RuntimeException("Unreachable")
case Unreachable => throw Trap()
case Block(ty, inner) =>
val k: Cont[Ans] = (retStack) =>
eval(rest, retStack.take(ty.toList.size) ++ stack, frame, kont, trail, ret)
Expand Down Expand Up @@ -302,7 +304,7 @@ object Evaluator {
eval(rest, retStack.take(ty.out.size) ++ newStack, frame, kont, trail, ret)
// We push newK on the trail since function creates a new block to escape
// (more or less like `return`)
eval(body, List(), newFrame, newK, newK :: trail, ret+1)
eval(body, List(), newFrame, newK, newK :: trail, 0)
case Call(f) if frame.module.funcs(f).isInstanceOf[Import] =>
frame.module.funcs(f) match {
case Import("console", "log", _) =>
Expand Down
7 changes: 6 additions & 1 deletion src/test/scala/genwasym/TestEval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ class TestEval extends FunSuite {
test("fact") { testFile("./benchmarks/wasm/fact.wat", None, Some(120)) }
test("loop") { testFile("./benchmarks/wasm/loop.wat", None, Some(10)) }
test("even-odd") { testFile("./benchmarks/wasm/even_odd.wat", None, Some(1)) }
test("return") { testFile("./benchmarks/wasm/return.wat", None, None) }
test("load") { testFile("./benchmarks/wasm/load.wat", None, Some(1)) }
test("btree") { testFile("./benchmarks/wasm/btree/2o1u-unlabeled.wat") }
test("fib") { testFile("./benchmarks/wasm/fib.wat", None, Some(144)) }
test("tribonacci") { testFile("./benchmarks/wasm/tribonacci.wat", None, Some(504)) }

test("return") {
intercept[gensym.wasm.miniwasm.Trap] {
testFile("./benchmarks/wasm/return.wat", None, None)
}
}

// TODO: add wasm spec tests? How to utilize wast files?
}

0 comments on commit e2315b6

Please sign in to comment.