toml
[dependencies]
js_ffi = "0.0.1"
A simple FFI library for calling javascript from web assembly with Rust * low magic * minimal * no macros * ready for web references * compatible with async-await * works with web assembly languages other than Rust
```rust use js_ffi::*; use executor::Executor;
const FUNCTIONLOG: i32 = 0; const FUNCTIONSETTIMEOUT: i32 = 1; const FUNCTIONALERT: i32 = 2;
pub fn main() -> () { register(FUNCTIONLOG,"console.log"); register(FUNCTIONSETTIMEOUT,"window.setTimeout"); register(FUNCTIONALERT,"window.alert"); Executor::spawn(async { consolelog("hey"); windowsettimeout(1000).await; windowalert("you"); }); }
pub fn consolelog(msg: &str) { call1(UNDEFINED, FUNCTIONLOG, TYPESTRING, to_string(msg)); }
pub fn windowalert(msg: &str) { call1(UNDEFINED, FUNCTIONALERT, TYPESTRING, to_string(msg)); }
pub fn windowsettimeout(millis: i32) -> CallbackFuture { let (future, id) = CallbackFuture::create(); jscall2( UNDEFINED, FUNCTIONSETTIMEOUT, TYPEFUNCTION, id, TYPENUM, millis as f32, ); future } ```
```html
``` ## How it works The basic premise is that you `register` the JavaScript functions you want to have access to from Rust to a constant number function handle. Then you can use `call_*` to send execute the function with arguments depending on the argument count you want to send (e.g. `call_1`, `call_7`). The idea is you can quickly create wrapper functions for exactly what you need. When calling the function you specify the object to call the function of (or undefined if you just want to call the function), the function id to call you registered with, and pairs of argument type and arguments afer. `call_*(