Fix encapsulated environment in module functions.

This commit is contained in:
Stephen Chung
2022-01-30 17:27:13 +08:00
parent 8fc80ecd10
commit 7b92a80c32
13 changed files with 150 additions and 63 deletions

View File

@@ -514,3 +514,47 @@ fn test_module_file() -> Result<(), Box<EvalAltResult>> {
Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
Ok(())
}
#[cfg(not(feature = "no_function"))]
#[test]
fn test_module_environ() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let ast = engine.compile(
r#"
const SECRET = 42;
fn foo(x) {
print(global::SECRET);
global::SECRET + x
}
"#,
)?;
let mut m = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
m.set_id("test");
m.build_index();
engine.register_static_module("test", m.into());
assert_eq!(
engine.eval::<String>(
r#"
const SECRET = "hello";
fn foo(x) {
print(global::SECRET);
global::SECRET + x
}
let t = test::foo(0);
foo(t)
"#
)?,
"hello42"
);
Ok(())
}