Minor tweaks, fix bug

This commit is contained in:
torkleyy
2017-12-20 14:35:44 +01:00
parent f09545921f
commit a0fb5036f6
4 changed files with 35 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
extern crate rhai;
use rhai::{Engine, FnRegister};
use rhai::{Any, Engine, EvalAltResult, RegisterFn};
#[derive(Clone, Debug)]
struct TestStruct {
@@ -16,14 +16,27 @@ impl TestStruct {
}
}
fn print_ty_name(test: Vec<&mut Box<Any>>) -> Result<Box<Any>, EvalAltResult> {
println!("Ty name: {:?}", (*test[0]).as_ref().type_name());
Ok(Box::new(()))
}
fn main() {
let mut engine = Engine::new();
engine.register_type::<TestStruct>();
print_ty_name(vec![&mut (Box::new(TestStruct::new()) as Box<Any>)]).unwrap();
engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new);
engine.register_fn_raw("ty_name".to_owned(), None, Box::new(print_ty_name));
println!(
"{:?}",
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")
);
println!(
"{:?}",
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")

View File

@@ -2,8 +2,6 @@ extern crate rhai;
use rhai::{Any, Engine, RegisterFn};
fn add(x: i64, y: i64) -> i64 {
println!("Adding: {}", x + y);
x + y
}
@@ -11,17 +9,7 @@ fn main() {
let mut engine = Engine::new();
engine.register_fn("add", add);
engine
.call_fn_raw(
"add".to_owned(),
vec![
&mut (Box::new(3i64) as Box<Any>),
&mut (Box::new(5i64) as Box<Any>),
],
)
.expect("FAIL");
//if let Ok(result) = engine.eval::<i64>("add(40, 2)") {
// println!("Answer: {}", result); // prints 42
//}
if let Ok(result) = engine.eval::<i64>("add(40, 2)") {
println!("Answer: {}", result); // prints 42
}
}