Use global module constants in optimization.
This commit is contained in:
parent
42b6796200
commit
95753bb9c3
@ -1236,6 +1236,16 @@ fn optimize_top_level(
|
|||||||
optimization_level,
|
optimization_level,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Add constants from global modules
|
||||||
|
for (name, value) in engine
|
||||||
|
.global_modules
|
||||||
|
.iter()
|
||||||
|
.rev()
|
||||||
|
.flat_map(|m| m.iter_var())
|
||||||
|
{
|
||||||
|
state.push_var(name, AccessMode::ReadOnly, Some(value.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
// Add constants and variables from the scope
|
// Add constants and variables from the scope
|
||||||
for (name, constant, value) in scope.iter() {
|
for (name, constant, value) in scope.iter() {
|
||||||
if !constant {
|
if !constant {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#![cfg(not(feature = "no_optimize"))]
|
#![cfg(not(feature = "no_optimize"))]
|
||||||
|
|
||||||
use rhai::{Engine, EvalAltResult, OptimizationLevel, Scope, INT};
|
use rhai::{Engine, EvalAltResult, Module, OptimizationLevel, Scope, INT};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_optimizer() -> Result<(), Box<EvalAltResult>> {
|
fn test_optimizer() -> Result<(), Box<EvalAltResult>> {
|
||||||
@ -74,6 +74,7 @@ fn test_optimizer_run() -> Result<(), Box<EvalAltResult>> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
|
fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
engine.set_optimization_level(OptimizationLevel::Simple);
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
||||||
|
|
||||||
let ast = engine.compile("{ const DECISION = false; if DECISION { 42 } else { 123 } }")?;
|
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)] }");
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user