From 9c1a49da0b044ff3bb392ef72da1e687a3129ce4 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 21 May 2022 11:31:15 +0800 Subject: [PATCH] Fix AST combine. --- src/ast/ast.rs | 21 +++++++++++++++++++++ tests/modules.rs | 13 +++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/ast/ast.rs b/src/ast/ast.rs index 08099edf..0b9ec418 100644 --- a/src/ast/ast.rs +++ b/src/ast/ast.rs @@ -618,6 +618,27 @@ impl AST { crate::func::native::shared_make_mut(&mut self.lib) .merge_filtered(&other.lib, &_filter); } + + #[cfg(not(feature = "no_module"))] + match ( + self.resolver.as_ref().map_or(0, |r| r.len()), + other.resolver.as_ref().map_or(0, |r| r.len()), + ) { + (_, 0) => (), + (0, _) => { + self.set_resolver(other.resolver.unwrap()); + } + (_, _) => { + let resolver = + crate::func::native::shared_make_mut(self.resolver.as_mut().unwrap()); + let other_resolver = + crate::func::native::shared_take_or_clone(other.resolver.unwrap()); + for (k, v) in other_resolver { + resolver.insert(k, crate::func::shared_take_or_clone(v)); + } + } + } + self } /// Filter out the functions, retaining only some based on a filter predicate. diff --git a/tests/modules.rs b/tests/modules.rs index 69e2ef82..d6bacf3f 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -282,6 +282,19 @@ fn test_module_resolver() -> Result<(), Box> { let result: INT = engine.call_fn(&mut Scope::new(), &ast, "foo", (2 as INT,))?; assert_eq!(result, 84); + + let mut ast2 = engine.compile("fn foo(x) { 42 }")?; + + let len = ast.resolver().unwrap().len(); + + ast2 += ast; + + assert!(ast2.resolver().is_some()); + assert_eq!(ast2.resolver().unwrap().len(), len); + + let result: INT = engine.call_fn(&mut Scope::new(), &ast2, "foo", (2 as INT,))?; + + assert_eq!(result, 84); } Ok(())