Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brendocosta/gwr
An experimental work-in-progress (WIP) WebAssembly runtime written in Gleam.
https://github.com/brendocosta/gwr
erlang gleam runtime wasm webassembly
Last synced: about 1 month ago
JSON representation
An experimental work-in-progress (WIP) WebAssembly runtime written in Gleam.
- Host: GitHub
- URL: https://github.com/brendocosta/gwr
- Owner: BrendoCosta
- License: mit
- Created: 2024-08-12T01:28:59.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-10-17T15:21:40.000Z (3 months ago)
- Last Synced: 2024-11-15T22:13:00.984Z (about 2 months ago)
- Topics: erlang, gleam, runtime, wasm, webassembly
- Language: Gleam
- Homepage:
- Size: 211 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GWR - Gleam WebAssembly Runtime
[![Package Version](https://img.shields.io/hexpm/v/gwr)](https://hex.pm/packages/gwr)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/gwr/)
[![Package License](https://img.shields.io/hexpm/l/gwr)](https://hex.pm/packages/gwr)
[![Package Total Downloads Count](https://img.shields.io/hexpm/dt/gwr)](https://hex.pm/packages/gwr)
[![Build Status](https://img.shields.io/github/actions/workflow/status/BrendoCosta/gwr/test.yml)](https://hex.pm/packages/gwr)
[![Total Stars Count](https://img.shields.io/github/stars/BrendoCosta/gwr)](https://hex.pm/packages/gwr)## 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
```sh
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
```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
}
```
```sh
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](https://github.com/WebAssembly/wabt).
```wasm
;; 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))
)
)
```
```sh
wat2wasm -o ./sum.wasm ./sum.wat
```### Step 2 - Run it from Gleam with GWR
Using the [simplifile](https://hex.pm/packages/simplifile) package to read the module file.
```sh
gleam add simplifile
``````gleam
import gwr/gwr
import gwr/execution/runtime
import simplifilepub 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](/LICENSE).