Fix bug with eager optimization of method calls.
This commit is contained in:
@@ -2,23 +2,23 @@
|
||||
|
||||
use rhai::{Engine, EvalAltResult, INT};
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
struct TestStruct {
|
||||
x: INT,
|
||||
}
|
||||
|
||||
impl TestStruct {
|
||||
fn update(&mut self, n: INT) {
|
||||
self.x += n;
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
Self { x: 1 }
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_method_call() -> Result<(), Box<EvalAltResult>> {
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
struct TestStruct {
|
||||
x: INT,
|
||||
}
|
||||
|
||||
impl TestStruct {
|
||||
fn update(&mut self, n: INT) {
|
||||
self.x += n;
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
Self { x: 1 }
|
||||
}
|
||||
}
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
||||
engine
|
||||
@@ -47,3 +47,31 @@ fn test_method_call_style() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_optimize"))]
|
||||
#[test]
|
||||
fn test_method_call_with_full_optimization() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
engine.set_optimization_level(rhai::OptimizationLevel::Full);
|
||||
|
||||
engine
|
||||
.register_fn("new_ts", || TestStruct::new())
|
||||
.register_fn("ymd", |_: INT, _: INT, _: INT| 42 as INT)
|
||||
.register_fn("range", |_: &mut TestStruct, _: INT, _: INT| {
|
||||
TestStruct::new()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<TestStruct>(
|
||||
"
|
||||
let xs = new_ts();
|
||||
let ys = xs.range(ymd(2022, 2, 1), ymd(2022, 2, 2));
|
||||
ys
|
||||
"
|
||||
)?,
|
||||
TestStruct::new()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user