diff --git a/src/api/call_fn.rs b/src/api/call_fn.rs index 6f72e1b7..6ac100b0 100644 --- a/src/api/call_fn.rs +++ b/src/api/call_fn.rs @@ -159,7 +159,6 @@ impl Engine { let orig_scope_len = scope.len(); if eval_ast && !statements.is_empty() { - // Make sure new variables introduced at global level do not _spill_ into the function call self.eval_global_statements(scope, mods, state, statements, &[ast.as_ref()], 0)?; if rewind_scope { diff --git a/tests/call_fn.rs b/tests/call_fn.rs index aa098fb2..0080dfcd 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -69,6 +69,36 @@ fn test_call_fn() -> Result<(), Box> { Ok(()) } +#[test] +fn test_call_fn_scope() -> Result<(), Box> { + let engine = Engine::new(); + let mut scope = Scope::new(); + + let ast = engine.compile( + " + fn foo(x) { + let hello = 42; + bar + hello + x + } + + let bar = 123; + ", + )?; + + for _ in 0..50 { + assert_eq!( + engine + .call_fn_raw(&mut scope, &ast, true, false, "foo", None, [Dynamic::THREE])? + .as_int()?, + 168 + ); + } + + assert_eq!(scope.len(), 100); + + Ok(()) +} + struct Options { pub foo: bool, pub bar: String,