From b23d64bec02edf70b7dc1414b708e04b1c23582c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 5 May 2022 21:34:15 +0800 Subject: [PATCH] Fix bug with using self-contained AST with call_fn. --- CHANGELOG.md | 9 +++++++++ src/api/call_fn.rs | 11 +++++++++++ tests/modules.rs | 10 +++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1cf25db..8f5a5706 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ Rhai Release Notes ================== +Version 1.8.0 +============= + +Bug fixes +--------- + +* Self-contained `AST` now works properly with `Engine::call_fn`. + + Version 1.7.0 ============= diff --git a/src/api/call_fn.rs b/src/api/call_fn.rs index 41d1e414..d6290163 100644 --- a/src/api/call_fn.rs +++ b/src/api/call_fn.rs @@ -244,6 +244,12 @@ impl Engine { let orig_scope_len = scope.len(); + #[cfg(not(feature = "no_module"))] + let orig_embedded_module_resolver = std::mem::replace( + &mut global.embedded_module_resolver, + ast.resolver().cloned(), + ); + if eval_ast && !statements.is_empty() { self.eval_global_statements(scope, global, caches, statements, &[ast.as_ref()], 0)?; @@ -278,6 +284,11 @@ impl Engine { 0, )?; + #[cfg(not(feature = "no_module"))] + { + global.embedded_module_resolver = orig_embedded_module_resolver; + } + #[cfg(feature = "debugging")] if self.debugger.is_some() { global.debugger.status = crate::eval::DebuggerStatus::Terminate; diff --git a/tests/modules.rs b/tests/modules.rs index 4394d893..69e2ef82 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -263,11 +263,11 @@ fn test_module_resolver() -> Result<(), Box> { #[cfg(not(feature = "no_function"))] { let script = r#" - fn foo() { + fn foo(x) { import "hello" as h; - h::answer + h::answer * x } - foo() + { import "hello" as h; h::answer } + foo(1) + { import "hello" as h; h::answer } "#; let mut scope = Scope::new(); @@ -278,6 +278,10 @@ fn test_module_resolver() -> Result<(), Box> { assert_eq!(engine.eval_ast::(&ast)?, 84); assert!(engine.eval::(script).is_err()); + + let result: INT = engine.call_fn(&mut Scope::new(), &ast, "foo", (2 as INT,))?; + + assert_eq!(result, 84); } Ok(())