Function wilton_rusty::register_wiltocall[][src]

pub fn register_wiltocall<I: DeserializeOwned, O: Serialize, F: 'static + Fn(I) -> O>(
    name: &str,
    callback: F
) -> Result<(), String>

Registers a closure, that can be called from JavaScript

This function takes a closure and registers it with Wilton, so it can be called from JavaScript using wiltoncall API.

Closure must take a single argument - a struct that implements serde::Deserialize and must return a struct that implements serde::Serialize. Closure input argument is converted from JavaScript object to Rust struct object. Closure output is returned to JavaScript as a JSON (that can be immediately converted to JavaScript object).

If closure panics, its panic message is converted into JavaScript Error message (that can be caugth and handled on JavaScript side).

Arguments

  • name - name this call, that should be used from JavaScript to invoke the closure
  • callback - closure, that will be called from JavaScript

Example

// declare input/output structs
#[derive(Deserialize)]
struct MyIn { }
#[derive(Serialize)]
struct MyOut {  }

// write a function that does some work
fn hello(obj: MyIn) -> MyOut { }

// register that function inside the `wilton_module_init` function,
// that will be called by Wilton during the Rust module load
#[no_mangle]
pub extern "C" fn wilton_module_init() -> *mut std::os::raw::c_char {
   // register a call, error checking omitted
   wilton_rusty::register_wiltocall("hello", |obj: MyIn| { hello(obj) });
   // return success status to Wilton
   wilton_rusty::create_wilton_error(None)
}