Propagate constants to functions for Engine::XXX_with_scope calls.

This commit is contained in:
Stephen Chung
2021-11-08 22:16:28 +08:00
parent 31ef7e6c69
commit 5083df3096
4 changed files with 87 additions and 44 deletions

View File

@@ -1,6 +1,6 @@
#![cfg(not(feature = "no_optimize"))]
use rhai::{Engine, EvalAltResult, OptimizationLevel, INT};
use rhai::{Engine, EvalAltResult, OptimizationLevel, Scope, INT};
#[test]
fn test_optimizer() -> Result<(), Box<EvalAltResult>> {
@@ -107,3 +107,24 @@ fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_optimizer_scope() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
scope.push_constant("FOO", 42 as INT);
let ast = engine.compile_with_scope(&scope, "fn foo() { FOO } foo()")?;
scope.push("FOO", 123 as INT);
assert_eq!(engine.eval_ast::<INT>(&ast)?, 42);
assert_eq!(engine.eval_ast_with_scope::<INT>(&mut scope, &ast)?, 42);
let ast = engine.compile_with_scope(&scope, "fn foo() { FOO } foo()")?;
assert!(engine.eval_ast_with_scope::<INT>(&mut scope, &ast).is_err());
Ok(())
}