35 lines
653 B
Rust
35 lines
653 B
Rust
use rhai::{Engine, RegisterFn, INT};
|
|
|
|
#[derive(Clone, Debug)]
|
|
struct TestStruct {
|
|
x: INT,
|
|
}
|
|
|
|
impl TestStruct {
|
|
fn update(&mut self) {
|
|
self.x += 1000;
|
|
}
|
|
|
|
fn new() -> Self {
|
|
TestStruct { x: 1 }
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let mut engine = Engine::new();
|
|
|
|
engine.register_type::<TestStruct>();
|
|
|
|
engine.register_fn("update", TestStruct::update);
|
|
engine.register_fn("new_ts", TestStruct::new);
|
|
|
|
println!(
|
|
"{:?}",
|
|
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")
|
|
);
|
|
println!(
|
|
"{:?}",
|
|
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")
|
|
);
|
|
}
|