Fix bug with capturing undefined variable.

This commit is contained in:
Stephen Chung
2022-07-29 09:43:40 +08:00
parent ea38185cac
commit fc976172e7
3 changed files with 22 additions and 7 deletions

View File

@@ -42,21 +42,35 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_object"))]
fn test_closures() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let mut scope = Scope::new();
scope.push("x", 42 as INT);
assert!(matches!(
*engine
.compile_expression("let f = |x| {};")
engine
.compile_expression("|x| {}")
.expect_err("should error")
.0,
.err_type(),
ParseErrorType::BadInput(..)
));
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
"
let f = || { x };
f.call()
",
)?,
42
);
assert_eq!(
engine.eval::<INT>(
"
let foo = #{ x: 42 };
let f = || { this.x };
foo.call(f)
foo.call(f)
",
)?,
42