2020-03-24 02:49:08 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
|
2016-03-03 17:08:34 +01:00
|
|
|
|
2016-03-16 23:38:28 +01:00
|
|
|
#[derive(Clone)]
|
2016-03-03 17:08:34 +01:00
|
|
|
struct TestStruct {
|
2020-03-24 02:49:08 +01:00
|
|
|
x: INT,
|
2016-03-03 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
|
|
|
fn update(&mut self) {
|
|
|
|
self.x += 1000;
|
|
|
|
}
|
|
|
|
|
2020-03-19 06:52:10 +01:00
|
|
|
fn new() -> Self {
|
2016-03-03 17:08:34 +01:00
|
|
|
TestStruct { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 11:15:12 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-03-09 14:09:53 +01:00
|
|
|
fn main() -> Result<(), EvalAltResult> {
|
2016-03-03 17:08:34 +01:00
|
|
|
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
|
|
|
|
2020-03-09 14:09:53 +01:00
|
|
|
let result = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?;
|
|
|
|
|
|
|
|
println!("result: {}", result.x); // prints 1001
|
|
|
|
|
|
|
|
Ok(())
|
2016-03-16 23:38:28 +01:00
|
|
|
}
|
2020-03-29 11:15:12 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "no_object")]
|
|
|
|
fn main() {}
|