#statically #linker #embedding #stop #automating #binaries

nightly macro bin wasmir

A library for automating statically linking WASM binaries

13 releases

0.1.13 Jun 10, 2021
0.1.12 Jun 9, 2021

#735 in WebAssembly

Download history 2/week @ 2024-02-25 1/week @ 2024-03-10 167/week @ 2024-03-31

168 downloads per month

MIT/Apache

13KB
249 lines

wasmir

A library for embedding high-performance WASM code directly in a Rust program. This package was created for people who absolutely hate writing Javascript. The goal of this library is to reduce the amount of overhead required to implement WASM by automatically compiling WASM modules and statically linking them to your binary. You will need to have wasm-bindgen installed. If your project stops building, please submit an issue.

Usage

Add wasmir as a dependency to your Cargo.toml:

wasmir = "0.1.13"

Code must be declared inside a module. The typical usage is as follows:

use wasmir::wasmir;

#[wasmir]
mod my_module {
   use wasm_bindgen::prelude::*;
   
   #[wasm_bindgen]
   extern "C" {
      pub fn alert(s: &str);
   }
   
   #[wasm_bindgen]
   pub fn greet(name: &str) {
      unsafe {
         alert(&format!("Hello, {}!", name));
      }
   }
}

Once the proc_macro does its work, the above module will then contain two binary blob constants, wasm and loader. Serve loader from "my_module.js" and wasm from "my_module_bg.wasm" Then, in index.js, include the following code:

import init from './my_module_bg.js';
import {greet} from './my_module_bg.js';

function run() {
   greet(\"World\");
}

init().then(run)

You can also specify WASM-dependencies like so:

#[wasmir(
[dependencies]
wasm-bindgen = "*"
[dependencies.web-sys]
version = "*"
features = ["Document", "Node", "Element"]
)]

Dependencies

~1–1.8MB
~37K SLoC