2020-03-15 15:39:58 +01:00
|
|
|
#![cfg(not(feature = "no_optimize"))]
|
|
|
|
|
2022-03-28 06:53:52 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, Module, OptimizationLevel, Scope, INT};
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2021-07-01 06:27:29 +02:00
|
|
|
#[test]
|
|
|
|
fn test_optimizer() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
2023-04-24 06:17:23 +02:00
|
|
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
2021-07-01 06:27:29 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
2023-04-24 06:17:23 +02:00
|
|
|
const X = 0;
|
|
|
|
const X = 40 + 2 - 1 + 1;
|
|
|
|
X
|
2021-07-01 06:27:29 +02:00
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
#[test]
|
2020-06-13 15:57:46 +02:00
|
|
|
fn test_optimizer_run() -> Result<(), Box<EvalAltResult>> {
|
2020-04-21 17:25:12 +02:00
|
|
|
fn run_test(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
|
2021-04-20 06:01:35 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("if true { 42 } else { 123 }")?, 42);
|
2020-03-15 15:39:58 +01:00
|
|
|
assert_eq!(
|
2021-04-20 06:01:35 +02:00
|
|
|
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
|
2020-03-15 15:39:58 +01:00
|
|
|
42
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(r#"const abc = "hello"; if abc < "foo" { 42 } else { 123 }"#)?,
|
|
|
|
123
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
run_test(&mut engine)?;
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
|
|
|
run_test(&mut engine)?;
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
run_test(&mut engine)?;
|
|
|
|
|
2020-10-08 04:34:32 +02:00
|
|
|
// Override == operator
|
2022-09-03 09:15:42 +02:00
|
|
|
engine.register_fn("==", |_x: INT, _y: INT| false);
|
2020-10-08 04:34:32 +02:00
|
|
|
|
2022-09-03 09:15:42 +02:00
|
|
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
2020-10-08 04:34:32 +02:00
|
|
|
|
2022-09-03 16:07:36 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
engine.set_fast_operators(false);
|
|
|
|
|
2022-09-03 09:15:42 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
|
|
|
|
123
|
|
|
|
);
|
2020-10-08 04:34:32 +02:00
|
|
|
|
2022-09-03 09:15:42 +02:00
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
2020-10-08 04:34:32 +02:00
|
|
|
|
2022-09-03 09:15:42 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
|
|
|
|
123
|
|
|
|
);
|
2020-10-08 04:34:32 +02:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-13 15:57:46 +02:00
|
|
|
|
2022-07-25 07:40:23 +02:00
|
|
|
#[cfg(feature = "metadata")]
|
2021-04-06 17:28:22 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2021-11-29 02:43:35 +01:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2021-04-22 17:02:25 +02:00
|
|
|
#[cfg(not(feature = "no_position"))]
|
2020-06-13 15:57:46 +02:00
|
|
|
#[test]
|
|
|
|
fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
2022-03-28 06:53:52 +02:00
|
|
|
|
2020-06-13 15:57:46 +02:00
|
|
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
|
|
|
|
2020-12-08 15:20:29 +01:00
|
|
|
let ast = engine.compile("{ const DECISION = false; if DECISION { 42 } else { 123 } }")?;
|
|
|
|
|
2022-07-25 07:40:23 +02:00
|
|
|
assert_eq!(
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("{ast:?}"),
|
2023-02-21 10:52:11 +01:00
|
|
|
r#"AST { source: None, doc: None, resolver: None, body: [Expr(123 @ 1:53)] }"#
|
2022-07-25 07:40:23 +02:00
|
|
|
);
|
2021-03-13 16:43:05 +01:00
|
|
|
|
|
|
|
let ast = engine.compile("const DECISION = false; if DECISION { 42 } else { 123 }")?;
|
|
|
|
|
2021-04-06 17:18:41 +02:00
|
|
|
assert_eq!(
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("{ast:?}"),
|
2023-02-21 10:52:11 +01:00
|
|
|
r#"AST { source: None, doc: None, resolver: None, body: [Var(("DECISION" @ 1:7, false @ 1:18, None), CONSTANT, 1:1), Expr(123 @ 1:51)] }"#
|
2021-04-06 17:18:41 +02:00
|
|
|
);
|
2020-12-08 15:20:29 +01:00
|
|
|
|
|
|
|
let ast = engine.compile("if 1 == 2 { 42 }")?;
|
2020-06-13 15:57:46 +02:00
|
|
|
|
2022-07-25 07:40:23 +02:00
|
|
|
assert_eq!(
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("{ast:?}"),
|
2023-02-21 10:52:11 +01:00
|
|
|
r#"AST { source: None, doc: None, resolver: None, body: [] }"#
|
2022-07-25 07:40:23 +02:00
|
|
|
);
|
2020-06-13 15:57:46 +02:00
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
|
2020-12-08 15:20:29 +01:00
|
|
|
let ast = engine.compile("abs(-42)")?;
|
2020-06-13 15:57:46 +02:00
|
|
|
|
2022-07-25 07:40:23 +02:00
|
|
|
assert_eq!(
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("{ast:?}"),
|
2023-02-21 10:52:11 +01:00
|
|
|
r#"AST { source: None, doc: None, resolver: None, body: [Expr(42 @ 1:1)] }"#
|
2022-07-25 07:40:23 +02:00
|
|
|
);
|
2020-06-13 15:57:46 +02:00
|
|
|
|
2022-03-28 06:53:52 +02:00
|
|
|
let ast = engine.compile("NUMBER")?;
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("{ast:?}"),
|
2023-02-21 10:52:11 +01:00
|
|
|
r#"AST { source: None, doc: None, resolver: None, body: [Expr(Variable(NUMBER) @ 1:1)] }"#
|
2022-03-28 06:53:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut module = Module::new();
|
|
|
|
module.set_var("NUMBER", 42 as INT);
|
|
|
|
|
|
|
|
engine.register_global_module(module.into());
|
|
|
|
|
|
|
|
let ast = engine.compile("NUMBER")?;
|
|
|
|
|
2022-07-25 07:40:23 +02:00
|
|
|
assert_eq!(
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("{ast:?}"),
|
2023-02-21 10:52:11 +01:00
|
|
|
r#"AST { source: None, doc: None, resolver: None, body: [Expr(42 @ 1:1)] }"#
|
2022-07-25 07:40:23 +02:00
|
|
|
);
|
2022-03-28 06:53:52 +02:00
|
|
|
|
2020-06-13 15:57:46 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-08 15:16:28 +01:00
|
|
|
|
2021-11-08 16:24:03 +01:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2021-11-08 15:16:28 +01:00
|
|
|
#[test]
|
|
|
|
fn test_optimizer_scope() -> Result<(), Box<EvalAltResult>> {
|
2021-11-08 16:24:03 +01:00
|
|
|
const SCRIPT: &str = "
|
|
|
|
fn foo() { FOO }
|
|
|
|
foo()
|
|
|
|
";
|
|
|
|
|
2021-11-08 15:16:28 +01:00
|
|
|
let engine = Engine::new();
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
|
|
|
scope.push_constant("FOO", 42 as INT);
|
|
|
|
|
2021-11-08 16:24:03 +01:00
|
|
|
let ast = engine.compile_with_scope(&scope, SCRIPT)?;
|
2021-11-08 15:16:28 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-11-08 16:24:03 +01:00
|
|
|
let ast = engine.compile_with_scope(&scope, SCRIPT)?;
|
2021-11-08 15:16:28 +01:00
|
|
|
|
|
|
|
assert!(engine.eval_ast_with_scope::<INT>(&mut scope, &ast).is_err());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-04-10 07:11:24 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
#[test]
|
|
|
|
fn test_optimizer_reoptimize() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
const SCRIPT: &str = "
|
|
|
|
const FOO = 42;
|
|
|
|
fn foo() {
|
|
|
|
let f = || FOO * 2;
|
2023-04-10 07:17:48 +02:00
|
|
|
call(f)
|
2023-04-10 07:11:24 +02:00
|
|
|
}
|
|
|
|
foo()
|
|
|
|
";
|
|
|
|
|
|
|
|
let engine = Engine::new();
|
|
|
|
let ast = engine.compile(SCRIPT)?;
|
|
|
|
let scope: Scope = ast.iter_literal_variables(true, false).collect();
|
|
|
|
let ast = engine.optimize_ast(&scope, ast, OptimizationLevel::Simple);
|
|
|
|
|
|
|
|
assert_eq!(engine.eval_ast::<INT>(&ast)?, 84);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-04-10 16:29:44 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optimizer_full() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct TestStruct(INT);
|
|
|
|
|
|
|
|
const SCRIPT: &str = "
|
|
|
|
const FOO = ts(40) + ts(2);
|
|
|
|
value(FOO)
|
|
|
|
";
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
|
2023-04-24 06:17:23 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
fn foo(x) { print(x); return; }
|
|
|
|
fn foo2(x) { if x > 0 {} return; }
|
|
|
|
42
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2023-04-10 16:29:44 +02:00
|
|
|
engine
|
|
|
|
.register_type_with_name::<TestStruct>("TestStruct")
|
|
|
|
.register_fn("ts", |n: INT| TestStruct(n))
|
|
|
|
.register_fn("value", |ts: &mut TestStruct| ts.0)
|
|
|
|
.register_fn("+", |ts1: &mut TestStruct, ts2: TestStruct| {
|
|
|
|
TestStruct(ts1.0 + ts2.0)
|
|
|
|
});
|
|
|
|
|
|
|
|
let ast = engine.compile(SCRIPT)?;
|
|
|
|
|
2023-04-11 04:35:24 +02:00
|
|
|
#[cfg(feature = "internals")]
|
2023-04-10 16:29:44 +02:00
|
|
|
assert_eq!(ast.statements().len(), 2);
|
|
|
|
|
|
|
|
assert_eq!(engine.eval_ast_with_scope::<INT>(&mut scope, &ast)?, 42);
|
|
|
|
|
|
|
|
assert_eq!(scope.len(), 1);
|
|
|
|
|
|
|
|
assert_eq!(scope.get_value::<TestStruct>("FOO").unwrap().0, 42);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|