Merge branch 'master' into plugins
This commit is contained in:
@@ -120,6 +120,20 @@ fn test_fn_ptr_raw() -> Result<(), Box<EvalAltResult>> {
|
||||
42
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
r#"
|
||||
fn foo(x, y) { this += x + y; }
|
||||
|
||||
let x = 40;
|
||||
let v = 1;
|
||||
x.bar(Fn("foo").curry(v), 1);
|
||||
x
|
||||
"#
|
||||
)?,
|
||||
42
|
||||
);
|
||||
|
||||
assert!(matches!(
|
||||
*engine.eval::<INT>(
|
||||
r#"
|
||||
|
@@ -1,7 +1,12 @@
|
||||
#![cfg(not(feature = "no_function"))]
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, RegisterFn, INT};
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, ParseErrorType, RegisterFn, Scope, INT};
|
||||
use std::any::TypeId;
|
||||
use std::cell::RefCell;
|
||||
use std::mem::take;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
use rhai::Map;
|
||||
|
||||
#[test]
|
||||
fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
|
||||
@@ -39,6 +44,14 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
|
||||
fn test_closures() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
assert!(matches!(
|
||||
*engine
|
||||
.compile_expression("let f = |x| {};")
|
||||
.expect_err("should error")
|
||||
.0,
|
||||
ParseErrorType::BadInput(_)
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
r#"
|
||||
@@ -181,3 +194,59 @@ fn test_closures_data_race() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
type MyType = Rc<RefCell<INT>>;
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
#[cfg(not(feature = "sync"))]
|
||||
fn test_closure_shared_obj() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Register API on MyType
|
||||
engine
|
||||
.register_type_with_name::<MyType>("MyType")
|
||||
.register_get_set(
|
||||
"data",
|
||||
|p: &mut MyType| *p.borrow(),
|
||||
|p: &mut MyType, value: INT| *p.borrow_mut() = value,
|
||||
)
|
||||
.register_fn("+=", |p1: &mut MyType, p2: MyType| {
|
||||
*p1.borrow_mut() += *p2.borrow()
|
||||
})
|
||||
.register_fn("-=", |p1: &mut MyType, p2: MyType| {
|
||||
*p1.borrow_mut() -= *p2.borrow()
|
||||
});
|
||||
|
||||
let engine = engine; // Make engine immutable
|
||||
|
||||
let code = r#"
|
||||
#{
|
||||
name: "A",
|
||||
description: "B",
|
||||
cost: 1,
|
||||
health_added: 0,
|
||||
action: |p1, p2| { p1 += p2 }
|
||||
}
|
||||
"#;
|
||||
|
||||
let ast = engine.compile(code)?;
|
||||
let res = engine.eval_ast::<Map>(&ast)?;
|
||||
|
||||
// Make closure
|
||||
let f = move |p1: MyType, p2: MyType| -> Result<(), Box<EvalAltResult>> {
|
||||
let action_ptr = res["action"].clone().cast::<FnPtr>();
|
||||
let name = action_ptr.fn_name();
|
||||
engine.call_fn::<_, ()>(&mut Scope::new(), &ast, name, (p1, p2))
|
||||
};
|
||||
|
||||
// Test closure
|
||||
let p1 = Rc::new(RefCell::new(41));
|
||||
let p2 = Rc::new(RefCell::new(1));
|
||||
|
||||
f(p1.clone(), p2.clone())?;
|
||||
|
||||
assert_eq!(*p1.borrow(), 42);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#![cfg(not(feature = "no_object"))]
|
||||
|
||||
use rhai::{Engine, EvalAltResult, Map, Scope, INT};
|
||||
use rhai::{Engine, EvalAltResult, Map, ParseErrorType, Scope, INT};
|
||||
|
||||
#[test]
|
||||
fn test_map_indexing() -> Result<(), Box<EvalAltResult>> {
|
||||
@@ -182,6 +182,14 @@ fn test_map_json() -> Result<(), Box<EvalAltResult>> {
|
||||
);
|
||||
}
|
||||
|
||||
engine.parse_json(&format!("#{}", json), true)?;
|
||||
|
||||
assert!(matches!(
|
||||
*engine.parse_json(" 123", true).expect_err("should error"),
|
||||
EvalAltResult::ErrorParsing(ParseErrorType::MissingToken(token, _), pos)
|
||||
if token == "{" && pos.position() == Some(4)
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@@ -94,6 +94,31 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
|
||||
42
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
r#"
|
||||
import "hello" as h1;
|
||||
import "hello" as h2;
|
||||
let x = 42;
|
||||
h1::sum(x, -10, 3, 7)
|
||||
"#
|
||||
)?,
|
||||
42
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
r#"
|
||||
import "hello" as h1;
|
||||
import "hello" as h2;
|
||||
let x = 42;
|
||||
h1::sum(x, 0, 0, 0);
|
||||
x
|
||||
"#
|
||||
)?,
|
||||
42
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
r#"
|
||||
|
Reference in New Issue
Block a user