Skip to content

Instantly share code, notes, and snippets.

@satabin
Last active August 1, 2018 14:09
Show Gist options
  • Select an option

  • Save satabin/f88c9f50bb0fc3862969c5121999114b to your computer and use it in GitHub Desktop.

Select an option

Save satabin/f88c9f50bb0fc3862969c5121999114b to your computer and use it in GitHub Desktop.
Compiling text WASM module and run it
import swam._
import swam.text._
import swam.runtime._
import swam.runtime.functions._
import swam.runtime.formats.DefaultFormatters._
import cats.effect._
import java.nio.file.Paths
val tcompiler = new Compiler[IO]
val engine = new SwamEngine[IO]
def log(i: Int) = IO(println(s"got $i"))
val foo = Map("console" -> Map[String, Interface[IO, Type]]("log" -> importable(log _)))
val res = for {
mod <- engine.compile(tcompiler.stream(Paths.get("logged.wat")))
inst <- mod.newInstance(foo)
f <- inst.exports.asFunction1[Int, Int]("add42")
res <- f(3)
} yield res
println(res.unsafeRunSync())
(module
(import "console" "log" (func $log (param i32)))
(type (func (param i32) (result i32)))
(func $add42 (type 0)
get_local 0
call $log
i32.const 42
get_local 0
i32.add)
(export "add42" (func $add42)))
# You must be in the a clone of swam and have logged.wat and logged.sc at the root of the clone
# Clone swam from https://github.com/satabin/swam
mill -i runtime.repl -c 'import $file.logged'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment