Skip to content

Commit

Permalink
handler needs only a stack & remove try-catch stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
butterunderflow committed Dec 23, 2024
1 parent 2e894fb commit 8ee376d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 80 deletions.
22 changes: 3 additions & 19 deletions src/main/scala/wasm/MiniWasmFX.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ case class EvaluatorFX(module: ModuleInstance) {
type Trail[A] = List[(Cont[A], List[Int])] // trail items are pairs of continuation and tags
type MCont[A] = Stack => A

type Handler[A] = (Stack, Cont[A], Trail[A], MCont[A]) => A
type Handler[A] = Stack => A
type Handlers[A] = List[(Int, Handler[A])]

case class ContV[A](k: (Stack, Cont[A], Trail[A], MCont[A], Handlers[A]) => A) extends Value {
Expand Down Expand Up @@ -154,22 +154,6 @@ case class EvaluatorFX(module: ModuleInstance) {
case RefFunc(f) =>
// TODO: RefFuncV stores an applicable function, instead of a syntactic structure
kont(RefFuncV(f) :: stack, trail, mkont, hs)

// resumable try-catch exception handling:
// NOTE(GW): so far we haven't use trail at all, could consider removing it
case TryCatch(es1, es2) =>
val newHandler: Handler[Ans] = (s1, k1, _, m1) => evalList(es2, s1, frame, k1, List(), m1, List(), hs)
val m1: MCont[Ans] = (s1) => kont(s1, List(), mkont, hs)
evalList(es1, List(), frame, initK[Ans], List(), m1, List(), List((-1, newHandler)) ++ hs)
case Resume0() =>
val (resume: ContV[Ans]) :: newStack = stack
resume.k(List(), kont, List(), mkont, List())
case Throw() =>
val err :: newStack = stack
def kr(s: Stack, k1: Cont[Ans], t1: Trail[Ans], m1: MCont[Ans], h1: Handlers[Ans]): Ans =
kont(s, List(), s1 => k1(s1, List(), m1, hs), hs)
hs.head._2(List(err, ContV(kr)), initK[Ans], List(), mkont)

// WasmFX effect handlers:
case ContNew(ty) =>
val RefFuncV(f) :: newStack = stack
Expand Down Expand Up @@ -197,7 +181,7 @@ case class EvaluatorFX(module: ModuleInstance) {
hs.find(_._1 == tagId) match {
case Some((_, handler)) =>
// we don't need to pass trail here, because handler's trail was determined when resuming
handler(newStack, initK[Ans], List(), mkont)
handler(newStack)
case None => throw new Exception(s"no handler for tag $tagId")
}
case Resume(tyId, handler) =>
Expand All @@ -207,7 +191,7 @@ case class EvaluatorFX(module: ModuleInstance) {
val (inputs, restStack) = newStack.splitAt(inps.size)
val newHs: List[(Int, Handler[Ans])] = handler.map {
case Handler(tagId, labelId) =>
val hh: Handler[Ans] = (s1, _k1, _t1, m1) => brTable(labelId)(s1, trail, mkont/*???*/, hs)
val hh: Handler[Ans] = s1 => brTable(labelId)(s1, trail, mkont/*???*/, hs)
(tagId, hh)
}
val tags = handler.map(_.tag)
Expand Down
61 changes: 0 additions & 61 deletions src/test/scala/genwasym/TestFx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,67 +97,6 @@ class TestFx extends FunSuite {
testFile("./benchmarks/wasm/wasmfx/callref-strip.wast")
}

test("try-catch") {
testFileOutput("./benchmarks/wasm/trycatch/try_catch.wat", List(1, 2, 3, 4, 5))
}

test("try-catch-succ") {
// no exception was thrown
testFileOutput("./benchmarks/wasm/trycatch/try_catch_succ.wat", List(1, 3, 5))
}

test("try-catch-discard") {
// discard the resumption in the catch block
testFileOutput("./benchmarks/wasm/trycatch/try_catch_discard.wat", List(1, 42, 4, 5))
}

test("nested-try-catch") {
testFileOutput("./benchmarks/wasm/trycatch/nested_try_catch.wat", List(1, 2, 3, 4, 5, 6, 7, 8, 9))
}

test("try-catch-multishot") {
testFileOutput("./benchmarks/wasm/trycatch/multishot.wat", List(1, 2, 3, 4, 3, 5))
}

test("try-catch-deep-handler") {
testFileOutput("./benchmarks/wasm/trycatch/deep.wat", List(1, 2, 3, 2, 4, 4, 5))
}

test("try-catch-block") {
testFileOutput("./benchmarks/wasm/trycatch/try_catch_block.wat", List(1, 2, 3, 4, 5))
}

test("try-catch-br2") {
testFileOutput("./benchmarks/wasm/trycatch/try_catch_br2.wat", List(1, 2, 6, 4, 5))
}

test("try-catch-br") {
// break out of try block is not allowed
assertThrows[IndexOutOfBoundsException] {
testFileOutput("./benchmarks/wasm/trycatch/try_catch_br.wat", List(1, 2, 6))
}
}

test("try-catch-throw-twice") {
testFileOutput("./benchmarks/wasm/trycatch/throw_twice.wat", List(1, 2, 6, 2, 3, 4, 4, 5))
}

test("try-catch-throw-twice2") {
testFileOutput("./benchmarks/wasm/trycatch/throw_twice2.wat", List(1, 2, 6, 2, 3, 4, 4, 5))
}

test("try-catch-br3") {
testFileOutput("./benchmarks/wasm/trycatch/try_catch_br3.wat", List(1, 2, 3, 4, 5))
}

test("try-catch-br4") {
testFileOutput("./benchmarks/wasm/trycatch/try_catch_br4.wat", List(1, 2, 6, 2, 7, 4, 4, 5))
}

test("try-catch-catch-br") {
testFileOutput("./benchmarks/wasm/trycatch/try_catch_catch_br.wat", List(1, 2, 6, 4, 6, 5))
}

/* REAL WASMFX STUFF */
test("cont") {
// testFile("./benchmarks/wasm/wasmfx/callcont.wast", None, ExpInt(11))
Expand Down

0 comments on commit 8ee376d

Please sign in to comment.