Move mutable runtime global state to Imports.

This commit is contained in:
Stephen Chung
2021-11-08 09:27:08 +08:00
parent 71ad158b6a
commit 09e6b21729
5 changed files with 222 additions and 174 deletions

View File

@@ -96,21 +96,6 @@ fn test_functions_global_module() -> Result<(), Box<EvalAltResult>> {
if matches!(&*err, EvalAltResult::ErrorVariableNotFound(v, _) if v == "global::ANSWER")
));
let mut module = Module::new();
module.set_var("ANSWER", 123 as INT);
engine.register_static_module("global", module.into());
assert_eq!(
engine.eval::<INT>(
"
const ANSWER = 42;
fn foo() { global::ANSWER }
foo()
"
)?,
123
);
engine.register_result_fn(
"do_stuff",
|context: NativeCallContext, callback: rhai::FnPtr| {
@@ -129,5 +114,32 @@ fn test_functions_global_module() -> Result<(), Box<EvalAltResult>> {
if matches!(&*err, EvalAltResult::ErrorVariableNotFound(v, _) if v == "global::LOCAL_VALUE")
));
#[cfg(not(feature = "no_closure"))]
assert_eq!(
engine.eval::<INT>(
"
const GLOBAL_VALUE = 42;
do_stuff(|| global::GLOBAL_VALUE);
"
)?,
42
);
// Override global
let mut module = Module::new();
module.set_var("ANSWER", 123 as INT);
engine.register_static_module("global", module.into());
assert_eq!(
engine.eval::<INT>(
"
const ANSWER = 42;
fn foo() { global::ANSWER }
foo()
"
)?,
123
);
Ok(())
}