2022-01-11 15:12:46 +01:00
|
|
|
use rhai::{Engine, EvalAltResult};
|
2016-04-14 03:40:06 +02:00
|
|
|
|
2022-01-16 15:50:39 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-12-26 08:41:41 +01:00
|
|
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
2022-02-10 05:54:30 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct TestStruct {
|
|
|
|
x: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { x: 1 }
|
|
|
|
}
|
|
|
|
pub fn update(&mut self) {
|
|
|
|
self.x += 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 03:40:06 +02:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-10-07 09:55:45 +02:00
|
|
|
engine
|
2022-02-10 05:54:30 +01:00
|
|
|
.register_type_with_name::<TestStruct>("TestStruct")
|
2020-12-26 08:41:41 +01:00
|
|
|
.register_fn("new_ts", TestStruct::new)
|
|
|
|
.register_fn("update", TestStruct::update);
|
2016-04-14 03:40:06 +02:00
|
|
|
|
2022-02-10 05:54:30 +01:00
|
|
|
#[cfg(feature = "metadata")]
|
|
|
|
{
|
|
|
|
println!("Functions registered:");
|
|
|
|
|
|
|
|
engine
|
|
|
|
.gen_fn_signatures(false)
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|func| println!("{}", func));
|
|
|
|
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
2021-02-20 16:46:25 +01:00
|
|
|
let result = engine.eval::<TestStruct>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2021-02-20 16:46:25 +01:00
|
|
|
let x = new_ts();
|
|
|
|
x.update();
|
|
|
|
x
|
|
|
|
",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
println!("{:?}", result);
|
|
|
|
|
|
|
|
let result = engine.eval::<TestStruct>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2021-02-20 16:46:25 +01:00
|
|
|
let x = [ new_ts() ];
|
|
|
|
x[0].update();
|
|
|
|
x[0]
|
|
|
|
",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
println!("{:?}", result);
|
2020-12-26 08:41:41 +01:00
|
|
|
|
|
|
|
Ok(())
|
2016-04-14 03:40:06 +02:00
|
|
|
}
|
2022-01-16 15:50:39 +01:00
|
|
|
|
|
|
|
#[cfg(any(feature = "no_index", feature = "no_object"))]
|
|
|
|
fn main() {
|
|
|
|
panic!("This example does not run under 'no_index' or 'no_object'.")
|
|
|
|
}
|