JS->Rust binding generation for Rust compiled to Web Assembly (deprecated, use stdweb instead!)
MIT License
JavaScript wrapper generation for rust code targeting wasm32-unknown-unknown.
I believe in "release early, release often", so wasm-wrapper-gen
is available on crates.io as version 0.0.3.
That said, the project is still a work in progress, and breaking changes are to be expected.
The main idea:
fn fib_str(nth: u32) -> String {
let mut last = 0;
let mut current = 1u64;
for _ in 0..(nth as u64) {
let temp = current + last;
last = current;
current = temp;
}
format!("fibonacci sequence #{}: {}", nth, current)
}
js_fn! {
fn fib_str(_: u32) -> String => fib_str;
}
let module = new WebAssembly.Module(/*..*/);
let fib = new Fibonacci(module);
console.log(fib.fib_str(20));
There are multiple full example projects available in examples/
, each which tests a different aspect of wasm-wrapper-gen
. All of these should be directly copyable out of the repository as a starter if needed.
wasm-wrapper-gen
is composed of two interlocking parts:
wasm-wrapper-gen
provides the js_fn!()
macro which generates extern "C"
functionswasm-wrapper-gen-build
is a build-script utility which scrapes the source for usages of js_fn!()
and generates a JavaScript class using those exported functions.string.charCodeAt
and String.fromCharCode
, but that all String arguments and return values require one extraVec<u16>
separately from the String
or &'static str
bool
, u8
, u16
, u32
, usize
, i8
, i16
, i32
, isize
, f32
, f64
&[_]
, &mut [_]
, Vec<_>
where _
is any of the aboveString
(not &str
because passing strings in always requires more allocation for utf16->utf8 in rust)bool
, u8
, u16
, u32
, usize
, i8
, i16
, i32
, isize
, f32
, f64
Vec<_>
where _
is any of the aboveString
and &'static str
Next to do:
impl
blocks with self
arguments and creating wrapper JS typesFurther future: