Use global module constants in optimization.

This commit is contained in:
Stephen Chung
2022-03-28 12:53:52 +08:00
parent 42b6796200
commit 95753bb9c3
2 changed files with 28 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
#![cfg(not(feature = "no_optimize"))]
use rhai::{Engine, EvalAltResult, OptimizationLevel, Scope, INT};
use rhai::{Engine, EvalAltResult, Module, OptimizationLevel, Scope, INT};
#[test]
fn test_optimizer() -> Result<(), Box<EvalAltResult>> {
@@ -74,6 +74,7 @@ fn test_optimizer_run() -> Result<(), Box<EvalAltResult>> {
#[test]
fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::Simple);
let ast = engine.compile("{ const DECISION = false; if DECISION { 42 } else { 123 } }")?;
@@ -97,6 +98,22 @@ fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
assert_eq!(format!("{:?}", ast), "AST { body: [Expr(42 @ 1:1)] }");
let ast = engine.compile("NUMBER")?;
assert_eq!(
format!("{:?}", ast),
"AST { body: [Expr(Variable(NUMBER) @ 1:1)] }"
);
let mut module = Module::new();
module.set_var("NUMBER", 42 as INT);
engine.register_global_module(module.into());
let ast = engine.compile("NUMBER")?;
assert_eq!(format!("{:?}", ast), "AST { body: [Expr(42 @ 1:1)] }");
Ok(())
}