Type-safe function binding
Closed this issue · 1 comments
Bromeon commented
The statement let result: Result<R, _> = script.call("function", &args);
is good enough for simple scripts or plugins with one entry point, but doesn't scale well for plugin APIs (i.e. a set of functions).
Instead of letting the users write type-safe wrappers around call()
statements, allow to simply declare the functions and generate the binding from it. Something along this might be nice (still need to check feasibility):
js_api! {
trait MyApi {
fn triple(num: i32) -> i32;
// more functions...
}
}
fn main() {
let script: Script = ...;
// Link the trait to the script, possibly verify functions exist
let api: Box<dyn MyApi> = script.bind_functions::<dyn MyApi>()?;
// Call directly on the polymorphic trait object
let x = api.triple(4)?;
}
Some points would need to be clarified:
- generating binding in an easy way
- is there a way to verify a binding before calling functions? (JS doesn't have typed signatures)
- how to handle failing calls (exception, mismatched type)? always return
Result
?
Bromeon commented
Implemented in latest master
and release 0.2.0-rc.0
.