Rewrite Rhai function dispatching
This commit is contained in:
@@ -3,7 +3,7 @@ use rhai::{Engine, FnRegister};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct TestStruct {
|
||||
x: i64
|
||||
x: i64,
|
||||
}
|
||||
|
||||
impl TestStruct {
|
||||
@@ -24,5 +24,8 @@ fn main() {
|
||||
engine.register_fn("update", TestStruct::update);
|
||||
engine.register_fn("new_ts", TestStruct::new);
|
||||
|
||||
println!("{:?}", engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]"));
|
||||
println!(
|
||||
"{:?}",
|
||||
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")
|
||||
);
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ use rhai::{Engine, FnRegister};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct TestStruct {
|
||||
x: i64
|
||||
x: i64,
|
||||
}
|
||||
|
||||
impl TestStruct {
|
||||
|
@@ -5,6 +5,6 @@ fn main() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
if let Ok(result) = engine.eval::<i64>("40 + 2") {
|
||||
println!("Answer: {}", result); // prints 42
|
||||
println!("Answer: {}", result); // prints 42
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -3,38 +3,40 @@ extern crate rhai;
|
||||
use std::fmt::Display;
|
||||
use std::process::exit;
|
||||
use std::io::{stdin, stdout, Write};
|
||||
use rhai::{Engine, Scope, FnRegister};
|
||||
use rhai::{Engine, FnRegister, Scope};
|
||||
|
||||
fn showit<T: Display>(x: &mut T) -> () {
|
||||
println!("{}", x)
|
||||
}
|
||||
|
||||
fn quit() { exit(0); }
|
||||
fn quit() {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let mut engine = Engine::new();
|
||||
let mut scope = Scope::new();
|
||||
let mut engine = Engine::new();
|
||||
let mut scope = Scope::new();
|
||||
|
||||
engine.register_fn("print", showit as fn(x: &mut i32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut i64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut u32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut u64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut f32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut f64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut bool)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut String)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut i32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut i64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut u32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut u64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut f32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut f64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut bool) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut String) -> ());
|
||||
engine.register_fn("exit", quit);
|
||||
|
||||
loop {
|
||||
print!("> ");
|
||||
let mut input = String::new();
|
||||
stdout().flush().expect("couldn't flush stdout");
|
||||
if let Err(e) = stdin().read_line(&mut input) {
|
||||
println!("input error: {}", e);
|
||||
}
|
||||
loop {
|
||||
print!("> ");
|
||||
let mut input = String::new();
|
||||
stdout().flush().expect("couldn't flush stdout");
|
||||
if let Err(e) = stdin().read_line(&mut input) {
|
||||
println!("input error: {}", e);
|
||||
}
|
||||
|
||||
if let Err(e) = engine.consume_with_scope(&mut scope, &input) {
|
||||
println!("error: {}", e);
|
||||
}
|
||||
}
|
||||
if let Err(e) = engine.consume_with_scope(&mut scope, &input) {
|
||||
println!("error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,10 +5,14 @@ fn main() {
|
||||
let mut engine = Engine::new();
|
||||
let mut scope: Scope = Vec::new();
|
||||
|
||||
if !engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5").is_ok() { assert!(false); }
|
||||
if !engine
|
||||
.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")
|
||||
.is_ok()
|
||||
{
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
if let Ok(result) = engine.eval_with_scope::<i64>(&mut scope, "x") {
|
||||
println!("result: {}", result);
|
||||
println!("result: {}", result);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -12,19 +12,18 @@ fn main() {
|
||||
for fname in env::args().skip(1) {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
engine.register_fn("print", showit as fn(x: &mut i32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut i64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut u32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut u64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut f32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut f64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut bool)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut String)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut i32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut i64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut u32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut u64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut f32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut f64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut bool) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut String) -> ());
|
||||
|
||||
match engine.eval_file::<()>(&fname) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {println!("Error: {}", e)}
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,9 @@
|
||||
extern crate rhai;
|
||||
use rhai::{Engine, FnRegister};
|
||||
use rhai::{Any, Engine, RegisterFn};
|
||||
|
||||
fn add(x: i64, y: i64) -> i64 {
|
||||
println!("Adding: {}", x + y);
|
||||
|
||||
x + y
|
||||
}
|
||||
|
||||
@@ -9,8 +11,17 @@ 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
|
||||
//}
|
||||
}
|
||||
|
Reference in New Issue
Block a user