Add Dynamic parameters to qualified function calls.

This commit is contained in:
Stephen Chung
2022-04-08 11:27:52 +08:00
parent 6481e19673
commit 42f977862f
3 changed files with 74 additions and 3 deletions

View File

@@ -562,3 +562,29 @@ fn test_module_environ() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_module_dynamic() -> Result<(), Box<EvalAltResult>> {
fn test_fn(input: Dynamic, x: INT) -> Result<INT, Box<EvalAltResult>> {
let s = input.into_string().unwrap();
Ok(s.len() as INT + x)
}
let mut engine = rhai::Engine::new();
let mut module = Module::new();
module.set_native_fn("test", test_fn);
let mut static_modules = rhai::module_resolvers::StaticModuleResolver::new();
static_modules.insert("test", module);
engine.set_module_resolver(static_modules);
engine.register_result_fn("test2", test_fn);
assert_eq!(engine.eval::<INT>(r#"test2("test", 38);"#)?, 42);
assert_eq!(
engine.eval::<INT>(r#"import "test" as test; test::test("test", 38);"#)?,
42
);
Ok(())
}