js_ffi
[]
= "0.0.5"
A simple FFI library for calling javascript from web assembly with Rust
- #![no_std] + alloc for uber small wasm ( unfortunately async-await does't work with no_std yet)
- low magic
- minimal
- no macros
- ready for web references
- compatible with async-await
- works with web assembly languages other than Rust
Simple Example
use *;
use Executor;
const FUNCTION_LOG: i32 = 0;
const FUNCTION_SET_TIMEOUT: i32 = 1;
const FUNCTION_ALERT: i32 = 2;
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_*(<object handle>,<function id>,<arg type>,<arg>,<arg type>,<arg>,...)
Advanced
Wrap third party. Anything with its functions in global space should be able to be wrapped and invoked.
const FUNCTION_JQUERY: i32 = 0;
const FUNCTION_JQUERY_ON: i32 = 1;
const FUNCTION_SAY_LOUD: i32 = 2;
const FUNCTION_SAY_QUEIT: i32 = 3;
Don't like Rust?
The script js_ffi.js
has nothing Rust specific. Everything is only done through methods
jsfficall0(f32,i32)
jsfficall1(f32,i32,i32,f32)
jsfficall2(f32,i32,i32,f32,i32,f32)
- ...
jsfficall10(f32,i32,i32,f32,i32,f32,i32,f32,i32,f32,i32,f32,i32,f32,i32,f32,i32,f32,i32,f32,i32,f32)
jsffiregister(i32,i32)
And one callback:
jsfficallback(i32)
And an entry point function:
main()
As long as your module adheres to this you can use js_ffi. Strings are simply cstrings that end in a 0
character.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in woke by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.