Racket Wasmtime Bindings

I've been working on some bindings for Wasmtime since I've found DrRacket to be a great IDE when working with WASM. This gist is still missing quite a bit of what Wasmtime can do, but I think it's definitely enough for doing a lot of things in WASM since you can pass arbitrary data back and forth by reading/writing to the linear memory of the WASM function.

While usable for pedagogy, I think Wasmtime could be a great way to handle computation C/C++ libraries with Racket. Unlike raw FFI, you don't need to worry about cross compiling anything but Wasmtime and it's a whole lot safer to run in the same process as Racket. If you go through Racket -> Wasmtime -> Somelib, then even if Somelib has an access violation, that merely generates a trap which is easily recoverable whereas without Wasmtime, Racket would just crash. Additionally, though I don't do it in my example, you can do things like limiting how much memory Somelib can use and you can also limit how long Somelib can run for so that it plays nicer with cooperative multitasking.

Unfortunately, there doesn't seem to be an easy way to figure out the ABI when doing something like compiling C/C++ using Emscripten. The safest thing to do would probably be to expose Emscripten's malloc to Racket as an export and then just pass things back and forth in main memory. It seems to get really complicated for things like foo run(bar x, foobar y);.

Racket Wasmtime FFI Example (github.com)

3 Likes