Fix export shadowing bug.

This commit is contained in:
Stephen Chung
2023-04-20 22:31:49 +08:00
parent 60ba27e2d6
commit 243e04f5ab
5 changed files with 121 additions and 28 deletions

View File

@@ -1,4 +1,4 @@
use rhai::{Dynamic, Engine, EvalAltResult, ParseErrorType, Position, Scope, INT};
use rhai::{Dynamic, Engine, EvalAltResult, Module, ParseErrorType, Position, Scope, INT};
#[test]
fn test_var_scope() -> Result<(), Box<EvalAltResult>> {
@@ -93,6 +93,41 @@ fn test_var_scope() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[cfg(not(feature = "no_module"))]
#[test]
fn test_var_scope_alias() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
scope.push("x", 42 as INT);
scope.set_alias("x", "a");
scope.set_alias("x", "b");
scope.set_alias("x", "y");
scope.push("x", 123 as INT);
scope.set_alias("x", "b");
scope.set_alias("x", "c");
let ast = engine.compile(
"
let x = 999;
export x as a;
export x as c;
let x = 0;
export x as z;
",
)?;
let m = Module::eval_ast_as_new(scope, &ast, &engine)?;
assert_eq!(m.get_var_value::<INT>("a").unwrap(), 999);
assert_eq!(m.get_var_value::<INT>("b").unwrap(), 123);
assert_eq!(m.get_var_value::<INT>("c").unwrap(), 999);
assert_eq!(m.get_var_value::<INT>("y").unwrap(), 42);
assert_eq!(m.get_var_value::<INT>("z").unwrap(), 0);
Ok(())
}
#[test]
fn test_var_is_def() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();