Merge branch 'master' into plugins

This commit is contained in:
Stephen Chung
2020-08-04 16:51:22 +08:00
46 changed files with 2041 additions and 869 deletions

View File

@@ -88,6 +88,7 @@ fn test_call_fn_private() -> Result<(), Box<EvalAltResult>> {
fn test_fn_ptr_raw() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
#[allow(deprecated)]
engine
.register_fn("mul", |x: &mut INT, y: INT| *x *= y)
.register_raw_fn(

View File

@@ -35,7 +35,8 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
}
#[test]
#[cfg(not(feature = "no_capture"))]
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "no_object"))]
fn test_closures() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
@@ -56,5 +57,61 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
42
);
assert_eq!(
engine.eval::<INT>(
r#"
let a = 41;
let foo = |x| { a += x };
foo.call(1);
a
"#
)?,
42
);
assert!(engine.eval::<bool>(
r#"
let a = 41;
let foo = |x| { a += x };
a.is_shared()
"#
)?);
Ok(())
}
#[test]
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "no_object"))]
fn test_closures_data_race() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<INT>(
r#"
let a = 1;
let b = 40;
let foo = |x| { this += a + x };
b.call(foo, 1);
b
"#
)?,
42
);
assert!(matches!(
*engine
.eval::<INT>(
r#"
let a = 20;
let foo = |x| { this += a + x };
a.call(foo, 1);
a
"#
)
.expect_err("should error"),
EvalAltResult::ErrorDataRace(_, _)
));
Ok(())
}

View File

@@ -73,7 +73,7 @@ fn test_fn_ptr() -> Result<(), Box<EvalAltResult>> {
"#
)
.expect_err("should error"),
EvalAltResult::ErrorInFunctionCall(fn_name, err, _) if fn_name == "foo" && matches!(*err, EvalAltResult::ErrorUnboundedThis(_))
EvalAltResult::ErrorInFunctionCall(fn_name, err, _) if fn_name == "foo" && matches!(*err, EvalAltResult::ErrorUnboundThis(_))
));
Ok(())

View File

@@ -1,5 +1,5 @@
#![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, INT};
use rhai::{Engine, EvalAltResult, ParseError, ParseErrorType, INT};
#[test]
fn test_functions() -> Result<(), Box<EvalAltResult>> {
@@ -120,3 +120,53 @@ fn test_function_pointers() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
#[cfg(not(feature = "no_closure"))]
fn test_function_captures() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<INT>(
r#"
fn foo(y) { x += y; x }
let x = 41;
let y = 999;
foo!(1) + x
"#
)?,
83
);
assert!(engine
.eval::<INT>(
r#"
fn foo(y) { x += y; x }
let x = 41;
let y = 999;
foo(1) + x
"#
)
.is_err());
#[cfg(not(feature = "no_object"))]
assert!(matches!(
engine.compile(
r#"
fn foo() { this += x; }
let x = 41;
let y = 999;
y.foo!();
"#
).expect_err("should error"),
ParseError(err, _) if matches!(*err, ParseErrorType::MalformedCapture(_))
));
Ok(())
}