diff --git a/tests/closures.rs b/tests/closures.rs index 348aa162..6f5de9e7 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -200,7 +200,7 @@ type MyType = Rc>; #[test] #[cfg(not(feature = "no_object"))] #[cfg(not(feature = "sync"))] -fn test_closure_shared_obj() -> Result<(), Box> { +fn test_closures_shared_obj() -> Result<(), Box> { let mut engine = Engine::new(); // Register API on MyType @@ -250,3 +250,30 @@ fn test_closure_shared_obj() -> Result<(), Box> { Ok(()) } + +#[test] +#[cfg(not(feature = "no_closure"))] +fn test_closures_external() -> Result<(), Box> { + 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::(&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(()) +}