2016-03-03 17:08:34 +01:00
|
|
|
extern crate rhai;
|
|
|
|
use rhai::{Engine, FnRegister};
|
|
|
|
|
2016-03-16 23:38:28 +01:00
|
|
|
#[derive(Clone)]
|
2016-03-03 17:08:34 +01:00
|
|
|
struct TestStruct {
|
2016-04-14 03:40:06 +02:00
|
|
|
x: i64
|
2016-03-03 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
|
|
|
fn update(&mut self) {
|
|
|
|
self.x += 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new() -> TestStruct {
|
|
|
|
TestStruct { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_type::<TestStruct>();
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
engine.register_fn("update", TestStruct::update);
|
|
|
|
engine.register_fn("new_ts", TestStruct::new);
|
2016-03-03 17:08:34 +01:00
|
|
|
|
2017-10-02 09:51:45 +02:00
|
|
|
if let Ok(result) = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x") {
|
2016-03-03 17:08:34 +01:00
|
|
|
println!("result: {}", result.x); // prints 1001
|
|
|
|
}
|
2016-03-16 23:38:28 +01:00
|
|
|
}
|