Skip to content

An experimental work-in-progress (WIP) WebAssembly runtime written in Gleam.

License

Notifications You must be signed in to change notification settings

BrendoCosta/gwr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GWR - Gleam WebAssembly Runtime

Package Version Hex Docs Package License Package Total Downloads Count Build Status Total Stars Count

Description

An experimental work-in-progress (WIP) WebAssembly runtime written in Gleam.

Purpose

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.

Installation

gleam add gwr

Usage

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.

Step 1 - Build code targeting Wasm

Example - from Rust

// 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

Example - from Wat

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

Step 2 - Run it from Gleam with GWR

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
}

Contributing

Contributions are welcome! Feel free to submit either issues or PRs, but keep in mind that your code needs to be covered by tests.

License

GWR source code is avaliable under the MIT license.

About

An experimental work-in-progress (WIP) WebAssembly runtime written in Gleam.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published