An experimental work-in-progress (WIP) WebAssembly runtime written in Gleam.
Nowadays, many languages support Wasm as a target, from mainstream ones like C++ and Rust, as well as newer ones like Odin and Grain. The purpose of this project is to use WebAssembly to create an alternative interoperability layer to Erlang's virtual machine NIFs.
gleam add gwr
Important
Currently the project is in an extremely early stage of development, it is only possible to run a simple sum function. Keep in mind that code and APIs may change dramatically.
// sum.rs
#![no_std]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> !
{
loop {}
}
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32
{
x + y
}
rustc --crate-type cdylib --target wasm32-unknown-unknown -C debuginfo=none -C panic=abort -C strip=symbols -C opt-level=3 ./sum.rs -o ./sum.wasm
Using the wat2wasm tool from wabt.
;; sum.wat
(module
(type $t0 (func (param i32 i32) (result i32)))
(func $sum (export "sum") (type $t0) (param $p0 i32) (param $p1 i32) (result i32)
(i32.add (local.get $p0) (local.get $p1))
)
)
wat2wasm -o ./sum.wasm ./sum.wat
Using the simplifile package to read the module file.
gleam add simplifile
import gwr/gwr
import gwr/execution/runtime
import simplifile
pub fn main()
{
let assert Ok(module_data) = simplifile.read_bits(from: "sum.wasm")
let assert Ok(instance) = gwr.create(from: module_data)
let assert Ok(#(instance, result)) = gwr.call(instance, "sum", [runtime.Integer32(4), runtime.Integer32(2)])
let assert [runtime.Integer32(6)] = result
}
Contributions are welcome! Feel free to submit either issues or PRs, but keep in mind that your code needs to be covered by tests.
GWR source code is avaliable under the MIT license.