js_ffi 0.0.9

A FFI library for calling javascript
Documentation

js_ffi

[dependencies]
js_ffi = "0.0.9"

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 futures
  • works with web assembly languages other than Rust
  • readable library that can be understood quickly and learned from

Simple Example

use js_ffi::*;
use executor::Executor;

const FUNCTION_LOG: i32 = 0;
const FUNCTION_SET_TIMEOUT: i32 = 1;
const FUNCTION_ALERT: i32 = 2;

#[no_mangle]
pub fn main() -> () {
    register(FUNCTION_LOG,"console.log");
    register(FUNCTION_SET_TIMEOUT,"window.setTimeout");
    register(FUNCTION_ALERT,"window.alert");
    Executor::spawn(async {
        console_log("hey");
        window_set_timeout(1000).await;
        window_alert("you");
    });
}

pub fn console_log(msg: &str) {
    call_1(UNDEFINED, FUNCTION_LOG, TYPE_STRING, to_js_string(msg));
}

pub fn window_alert(msg: &str) {
    call_1(UNDEFINED, FUNCTION_ALERT, TYPE_STRING, to_js_string(msg));
}

pub fn window_set_timeout(millis: i32) -> CallbackFuture {
    let (future, id) = CallbackFuture::create();
    js_call_2(
        UNDEFINED,
        FUNCTION_SET_TIMEOUT,
        TYPE_FUNCTION,
        id,
        TYPE_NUM,
        millis as f32,
    );
    future
}
<script src="https://cdn.jsdelivr.net/gh/richardanaya/js_ffi/js_ffi.js"></script>
<script>js_ffi.run("my_app.wasm");</script>

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;

#[no_mangle]
fn main() {
    // register functions of things in global scope
    register(FUNCTION_JQUERY,"$");
    // someimes functions are hidden on prototypes of things in global scope
    register(FUNCTION_JQUERY_ON,"jQuery.prototype.on");
    // reference your own functions created in global scope
    register(FUNCTION_SAY_LOUD,"say_loud");
    // create evaluated functions on the fly
    register(FUNCTION_SAY_QUEIT,"(x) => console.log(x)");
    
    call_1(UNDEFINED,FUNCTION_SAY_QUEIT,TYPE_STRING,to_js_string("adding event handler"));
    let obj = call_1(UNDEFINED,FUNCTION_JQUERY,TYPE_STRING,to_js_string("body"));
    call_2(obj,
           FUNCTION_JQUERY_ON,
           TYPE_STRING,
           to_js_string("click"),
           TYPE_FUNCTION,
           create_callback1(Box::new(|_|{
                call_1(UNDEFINED,
                       FUNCTION_SAY_LOUD,
                       TYPE_STRING,
                       to_js_string("I was clicked!"));
           })));
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/richardanaya/js_ffi/js_ffi.js"></script>
<script>
    function say_loud(msg){
        window.alert(msg);
    }
</script>
<script>js_ffi.run("my_app.wasm");</script>

Don't like Rust?

The script js_ffi.js has nothing Rust specific. Everything is only done through a very basic interface

  • jsffiregister(i32,i32)
  • 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)

And an entry point function:

  • main()

As long as your module adheres to this you can use js_ffi. Strings are simply c-strings in memorythat end in a 0 character.

License

This project is licensed under either of

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.