Fix AST combine.

This commit is contained in:
Stephen Chung 2022-05-21 11:31:15 +08:00
parent 46c1d86221
commit 9c1a49da0b
2 changed files with 34 additions and 0 deletions

View File

@ -618,6 +618,27 @@ impl AST {
crate::func::native::shared_make_mut(&mut self.lib) crate::func::native::shared_make_mut(&mut self.lib)
.merge_filtered(&other.lib, &_filter); .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 self
} }
/// Filter out the functions, retaining only some based on a filter predicate. /// Filter out the functions, retaining only some based on a filter predicate.

View File

@ -282,6 +282,19 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
let result: INT = engine.call_fn(&mut Scope::new(), &ast, "foo", (2 as INT,))?; let result: INT = engine.call_fn(&mut Scope::new(), &ast, "foo", (2 as INT,))?;
assert_eq!(result, 84); 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(()) Ok(())