Add external closure test.

This commit is contained in:
Stephen Chung 2020-08-23 16:29:15 +08:00
parent d134b72473
commit f4c74cc03b

View File

@ -200,7 +200,7 @@ type MyType = Rc<RefCell<INT>>;
#[test] #[test]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
fn test_closure_shared_obj() -> Result<(), Box<EvalAltResult>> { fn test_closures_shared_obj() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
// Register API on MyType // Register API on MyType
@ -250,3 +250,30 @@ fn test_closure_shared_obj() -> Result<(), Box<EvalAltResult>> {
Ok(()) Ok(())
} }
#[test]
#[cfg(not(feature = "no_closure"))]
fn test_closures_external() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut ast = engine.compile(
r#"
let test = "hello";
|x| test + x
"#,
)?;
// Save the function pointer together with captured variables
let fn_ptr = engine.eval_ast::<FnPtr>(&ast)?;
// Get rid of the script, retaining only functions
ast.retain_functions(|_, _, _| true);
// Closure 'f' captures: the engine, the AST, and the curried function pointer
let f = move |x: INT| fn_ptr.call_dynamic(&engine, ast, None, [x.into()]);
assert_eq!(f(42)?.as_str(), Ok("hello42"));
Ok(())
}