Fix build and all tests

This commit is contained in:
torkleyy 2017-12-20 21:09:53 +01:00
parent 3672414212
commit 348a680980
11 changed files with 68 additions and 21 deletions

View File

@ -1,5 +1,5 @@
extern crate rhai; extern crate rhai;
use rhai::{Engine, FnRegister}; use rhai::{Engine, RegisterFn};
#[derive(Clone)] #[derive(Clone)]
struct TestStruct { struct TestStruct {

View File

@ -3,7 +3,7 @@ extern crate rhai;
use std::fmt::Display; use std::fmt::Display;
use std::process::exit; use std::process::exit;
use std::io::{stdin, stdout, Write}; use std::io::{stdin, stdout, Write};
use rhai::{Engine, FnRegister, Scope}; use rhai::{Engine, RegisterFn, Scope};
fn showit<T: Display>(x: &mut T) -> () { fn showit<T: Display>(x: &mut T) -> () {
println!("{}", x) println!("{}", x)

View File

@ -2,7 +2,7 @@ use std::env;
use std::fmt::Display; use std::fmt::Display;
extern crate rhai; extern crate rhai;
use rhai::{Engine, FnRegister}; use rhai::{Engine, RegisterFn};
fn showit<T: Display>(x: &mut T) -> () { fn showit<T: Display>(x: &mut T) -> () {
println!("{}", x) println!("{}", x)

View File

@ -1,5 +1,5 @@
extern crate rhai; extern crate rhai;
use rhai::{Any, Engine, RegisterFn}; use rhai::{Engine, RegisterFn};
fn add(x: i64, y: i64) -> i64 { fn add(x: i64, y: i64) -> i64 {
x + y x + y

View File

@ -27,6 +27,28 @@ pub enum EvalAltResult {
Return(Box<Any>), Return(Box<Any>),
} }
impl PartialEq for EvalAltResult {
fn eq(&self, other: &Self) -> bool {
use EvalAltResult::*;
match (self, other) {
(&ErrorFunctionNotFound, &ErrorFunctionNotFound) => true,
(&ErrorFunctionArgMismatch, &ErrorFunctionArgMismatch) => true,
(&ErrorFunctionCallNotSupported, &ErrorFunctionCallNotSupported) => true,
(&ErrorIndexMismatch, &ErrorIndexMismatch) => true,
(&ErrorIfGuardMismatch, &ErrorIfGuardMismatch) => true,
(&ErrorVariableNotFound(ref a), &ErrorVariableNotFound(ref b)) => a == b,
(&ErrorFunctionArityNotSupported, &ErrorFunctionArityNotSupported) => true,
(&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true,
(&ErrorMismatchOutputType, &ErrorMismatchOutputType) => true,
(&ErrorCantOpenScriptFile, &ErrorCantOpenScriptFile) => true,
(&InternalErrorMalformedDotExpression, &InternalErrorMalformedDotExpression) => true,
(&LoopBreak, &LoopBreak) => true,
_ => false,
}
}
}
impl Error for EvalAltResult { impl Error for EvalAltResult {
fn description(&self) -> &str { fn description(&self) -> &str {
match *self { match *self {
@ -121,6 +143,12 @@ impl Engine {
ident: String, ident: String,
args: Vec<&mut Box<Any>>, args: Vec<&mut Box<Any>>,
) -> Result<Box<Any>, EvalAltResult> { ) -> Result<Box<Any>, EvalAltResult> {
println!(
"Trying to call function {:?} with args {:?}",
ident,
args.iter().map(|x| (***x).type_id()).collect::<Vec<_>>()
);
let spec = FnSpec { let spec = FnSpec {
ident: ident.clone(), ident: ident.clone(),
args: Some( args: Some(
@ -137,11 +165,26 @@ impl Engine {
.ok_or(EvalAltResult::ErrorFunctionNotFound) .ok_or(EvalAltResult::ErrorFunctionNotFound)
.and_then(move |f| match *f { .and_then(move |f| match *f {
FnIntExt::Ext(ref f) => f(args), FnIntExt::Ext(ref f) => f(args),
FnIntExt::Int(_) => unreachable!(), FnIntExt::Int(ref f) => {
let mut scope = Scope::new();
scope.extend(
f.params
.iter()
.cloned()
.zip(args.iter().map(|x| (&**x).clone())),
);
match self.eval_stmt(&mut scope, &*f.body) {
Err(EvalAltResult::Return(x)) => Ok(x),
other => other,
}
}
}) })
} }
pub fn register_fn_raw(&mut self, ident: String, args: Option<Vec<TypeId>>, f: Box<FnAny>) { pub fn register_fn_raw(&mut self, ident: String, args: Option<Vec<TypeId>>, f: Box<FnAny>) {
println!("Register; {:?} with args {:?}", ident, args,);
let spec = FnSpec { ident, args }; let spec = FnSpec { ident, args };
self.fns.insert(spec, FnIntExt::Ext(f)); self.fns.insert(spec, FnIntExt::Ext(f));
@ -247,7 +290,12 @@ impl Engine {
.and_then(move |(idx, &mut (_, ref mut val))| map(val).map(|val| (idx, val))) .and_then(move |(idx, &mut (_, ref mut val))| map(val).map(|val| (idx, val)))
} }
fn array_value(&self, scope: &mut Scope, id: &str, idx: &Expr) -> Result<(usize, usize, Box<Any>), EvalAltResult> { fn array_value(
&self,
scope: &mut Scope,
id: &str,
idx: &Expr,
) -> Result<(usize, usize, Box<Any>), EvalAltResult> {
let idx_boxed = self.eval_expr(scope, idx)? let idx_boxed = self.eval_expr(scope, idx)?
.downcast::<i64>() .downcast::<i64>()
.map_err(|_| EvalAltResult::ErrorIndexMismatch)?; .map_err(|_| EvalAltResult::ErrorIndexMismatch)?;
@ -307,7 +355,10 @@ impl Engine {
Expr::Identifier(ref id) => { Expr::Identifier(ref id) => {
let get_fn_name = "get$".to_string() + id; let get_fn_name = "get$".to_string() + id;
self.call_fn_raw(get_fn_name, vec![this_ptr]) self.call_fn_raw(get_fn_name, vec![this_ptr])
.and_then(|mut v| self.set_dot_val_helper(&mut v, inner_rhs, source_val)) .and_then(|mut v| {
self.set_dot_val_helper(&mut v, inner_rhs, source_val)
.map(|_| v) // Discard Ok return value
})
.and_then(|mut v| { .and_then(|mut v| {
let set_fn_name = "set$".to_string() + id; let set_fn_name = "set$".to_string() + id;

View File

@ -17,7 +17,7 @@
//! And the Rust part: //! And the Rust part:
//! //!
//! ```rust //! ```rust
//! use rhai::{FnRegister, Engine}; //! use rhai::{Engine, RegisterFn};
//! //!
//! fn compute_something(x: i64) -> bool { //! fn compute_something(x: i64) -> bool {
//! (x % 40) == 0 //! (x % 40) == 0

View File

@ -1,5 +1,5 @@
use engine::Engine; use engine::Engine;
use fn_register::FnRegister; use fn_register::RegisterFn;
#[test] #[test]
fn test_arrays() { fn test_arrays() {

View File

@ -1,5 +1,5 @@
use engine::Engine; use engine::Engine;
use fn_register::FnRegister; use fn_register::RegisterFn;
#[test] #[test]
fn test_float() { fn test_float() {

View File

@ -1,5 +1,5 @@
use engine::Engine; use engine::Engine;
use fn_register::FnRegister; use fn_register::RegisterFn;
#[test] #[test]
fn test_get_set() { fn test_get_set() {
@ -86,9 +86,5 @@ fn test_big_get_set() {
engine.register_fn("new_tp", TestParent::new); engine.register_fn("new_tp", TestParent::new);
if let Ok(result) = engine.eval::<i64>("let a = new_tp(); a.child.x = 500; a.child.x") { assert_eq!(engine.eval::<i64>("let a = new_tp(); a.child.x = 500; a.child.x"), Ok(500));
assert_eq!(result, 500);
} else {
assert!(false);
}
} }

View File

@ -1,5 +1,5 @@
use engine::Engine; use engine::Engine;
use fn_register::FnRegister; use fn_register::RegisterFn;
#[test] #[test]
fn test_method_call() { fn test_method_call() {

View File

@ -4,8 +4,8 @@ use engine::{Engine, EvalAltResult};
fn test_mismatched_op() { fn test_mismatched_op() {
let mut engine = Engine::new(); let mut engine = Engine::new();
match engine.eval::<i64>("60 + \"hello\"") { assert_eq!(
Err(EvalAltResult::ErrorFunctionArgMismatch) => (), engine.eval::<i64>("60 + \"hello\""),
_ => assert!(false), Err(EvalAltResult::ErrorFunctionNotFound)
} );
} }