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;
use rhai::{Engine, FnRegister};
use rhai::{Engine, RegisterFn};
#[derive(Clone)]
struct TestStruct {

View File

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

View File

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

View File

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

View File

@ -27,6 +27,28 @@ pub enum EvalAltResult {
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 {
fn description(&self) -> &str {
match *self {
@ -121,6 +143,12 @@ impl Engine {
ident: String,
args: Vec<&mut Box<Any>>,
) -> Result<Box<Any>, EvalAltResult> {
println!(
"Trying to call function {:?} with args {:?}",
ident,
args.iter().map(|x| (***x).type_id()).collect::<Vec<_>>()
);
let spec = FnSpec {
ident: ident.clone(),
args: Some(
@ -137,11 +165,26 @@ impl Engine {
.ok_or(EvalAltResult::ErrorFunctionNotFound)
.and_then(move |f| match *f {
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>) {
println!("Register; {:?} with args {:?}", ident, args,);
let spec = FnSpec { ident, args };
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)))
}
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)?
.downcast::<i64>()
.map_err(|_| EvalAltResult::ErrorIndexMismatch)?;
@ -307,7 +355,10 @@ impl Engine {
Expr::Identifier(ref id) => {
let get_fn_name = "get$".to_string() + id;
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| {
let set_fn_name = "set$".to_string() + id;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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