emlite-bind
emlite-bind is a Rust project that offers bindngs to Web APIs for use with wasm. It's backend agnostic, and should work with wasm32-unknown-unknown, wasm32-wasi, wasm32-wasip1 and wasm32-unknown-emscripten. It depends on the emlite library to make this happen. emlite-bind consists of 2 crates:
- jsbind
- webbind
jsbind currently provides the minimal set of basic JS types needed for webbind. More types are planned later.
webbind wraps the webidl api in Rust. And it currently wraps most of the DOM API's.
Usage of the crate should not require an extra js glue step, but with the caveat that the emlite.js version be compatible!
Requirement
A recent enough Rust compiler with your required target added. wasm32-wasip2 wasn't tested against emlite or emlite-bind.
Usage
Add jsbind and webbind to your project:
[]
= "0.1"
= "0.1"
Create a .cargo/config.toml with the following flags:
[]
= ["-Clink-args=--no-entry --allow-undefined --export-dynamic --export-if-defined=main --export-table --import-memory --export-memory --strip-all"]
[]
= ["-Clink-args=--no-entry --allow-undefined --export-dynamic --export-if-defined=main --export-if-defined=_start --export-table --import-memory --export-memory --strip-all"]
[]
= ["-Clink-args=-sERROR_ON_UNDEFINED_SYMBOLS=0 -sALLOW_MEMORY_GROWTH=1 -sEXPORTED_RUNTIME_METHODS=wasmTable,UTF8ToString,lengthBytesUTF8,stringToUTF8 -sEXPORTED_FUNCTIONS=_malloc,_main -Wl,--strip-all -sMODULARIZE=1 -sEXPORT_ES6=1"]
# This is required for better stripping of the generated wasm file
[]
= true
Usage in your Rust source files:
use *;
use HTMLButtonElement;
use Node;
use PointerEvent;
use window;
Run the build with your required target:
Load the generated wasm module in your index.html:
Document
If targeting wasi, you will need a wasi shim. The following example uses @bjorn3/browser_wasi_shim:
Document
In the above html examples, emlite.js is fetched from unpkg, you can alternatively vendor the source, or create a node project and npm i emlite
. If creating a node project, it's advisable to use a bundler (such as webpack for example).
If targeting emscripten, the link flags in .cargo/config.toml instruct the emscripten toolchain to generate an ES6 module. As such, you would need to initialize emscripten's js module:
Document
If you don't emscripten the -sMODULARIZE=1 -sEXPORT_ES6=1
flags, then you would just need to import the script in the script tag:
Testing
This repo comes with an index.html which you can use for testing purposes. Note that loading wasm in the browser requires an http server.
# after cloning and cd'ing into this repo
# start a server
Alternatively you can run:
# after cloning and cd'ing into this repo