-
Notifications
You must be signed in to change notification settings - Fork 228
Open
Labels
Description
use rhai::{CallFnOptions, Dynamic, Engine, EvalAltResult, FnPtr, Scope};
const SCRIPT: &str = r#"
fn init() {
let my_handler = #{
count: 0,
on_player_joined: |event| {
print(`got ${event}`);
print(`now ${this.count}`);
},
};
true
}
"#;
fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.set_max_call_levels(64);
engine.set_max_expr_depths(64, 64);
let ast = engine.compile(SCRIPT)?;
let mut scope = Scope::new();
let options = CallFnOptions::new().eval_ast(false).rewind_scope(false);
let _: bool = engine.call_fn_with_options(options, &mut scope, &ast, "init", ())?;
let my_handler: Dynamic = scope.get_value("my_handler").unwrap();
let binding = my_handler.as_map_ref().unwrap();
let opj: FnPtr = binding.get("on_player_joined").unwrap().clone_cast();
let _: bool = opj.call(&engine, &ast, (1_i64,))?; // <-- failed with Unboundthis error!
Ok(())
}I hope to inject an event from rust to rhai by above code which is, I think, better than current official solution. But I got an error ErrorUnboundThis when call FnPtr::call, so I hope a new API FnPtr::call(my_handler, ...) to achieve it. Thanks!
Reactions are currently unavailable