wasm-wrapper-gen
JavaScript wrapper generation for rust code targeting wasm32-unknown-unknown.
This repository is currently very WIP, but there's a full working example Cargo project in examples/simple_summation/.
General overview:
wasm-wrapper-gen is composed of two interlocking parts:
wasm-wrapper-genprovides thejs_fn!()macro which generatesextern "C"functionswasm-wrapper-gen-buildis a build-script utility which scrapes the source for usages ofjs_fn!()and generates a JavaScript file which binds to those exported functions.
Note: this assumes little-endian hardware (the majority of modern hardware).
Currently supported:
- Argument types:
bool,u8,u16,u32,usize,i8,i16,i32,isize,f32,f64&[_],&mut [_],Vec<_>where_is any of the above
- Return types:
bool,u8,u16,u32,usize,i8,i16,i32,isize,f32,f64Vec<_>where_is any of the above
- Full automatic memory management and freeing unless rust function panics
- Configuration to use either a single DataView or a TypedArray instance per argument to access arrays
- Configurable output JS indentation
Unimplemented:
-
Next to do:
- Add support for making an async constructor rather than sync one.
- Add support for
implblocks withselfarguments and creating wrapper JS types which manage allocation sanely.
-
Further future:
- Make real tests and figure out how to do a build.rs script which only runs for tests
- Arbitrary argument types implementing some serialization trait
- Macro to wrap individual structs in separate JavaScript classes which all reference the same WebAssembly.Instance