Enable dot operations on constant variables.

This commit is contained in:
Stephen Chung
2020-03-20 19:27:02 +08:00
parent 16ea8f416e
commit ed996e71d6
7 changed files with 385 additions and 238 deletions

View File

@@ -91,7 +91,7 @@ fn test_big_get_set() -> Result<(), EvalAltResult> {
);
assert_eq!(
engine.eval::<String>("let a = new_tp(); a.type_of()")?,
engine.eval::<String>("let a = new_tp(); type_of(a)")?,
"TestParent"
);

39
tests/side_effects.rs Normal file
View File

@@ -0,0 +1,39 @@
use rhai::{Engine, EvalAltResult, RegisterFn, Scope};
use std::cell::Cell;
use std::rc::Rc;
#[derive(Debug, Clone)]
struct CommandWrapper {
value: Rc<Cell<i64>>,
}
impl CommandWrapper {
pub fn set_value(&mut self, x: i64) {
let val = self.value.get();
self.value.set(val + x);
}
}
#[test]
fn test_side_effects() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
let mut scope = Scope::new();
let payload = Rc::new(Cell::new(12));
assert_eq!(payload.get(), 12);
let command = CommandWrapper {
value: payload.clone(),
};
scope.push_constant("Command", command);
engine.register_type_with_name::<CommandWrapper>("CommandType");
engine.register_fn("action", CommandWrapper::set_value);
engine.eval_with_scope::<()>(&mut scope, "Command.action(30)")?;
assert_eq!(payload.get(), 42);
Ok(())
}

View File

@@ -23,10 +23,10 @@ fn test_type_of() -> Result<(), EvalAltResult> {
assert_eq!(engine.eval::<String>(r#"type_of("hello")"#)?, "string");
#[cfg(not(feature = "only_i32"))]
assert_eq!(engine.eval::<String>("let x = 123; x.type_of()")?, "i64");
assert_eq!(engine.eval::<String>("let x = 123; type_of(x)")?, "i64");
#[cfg(feature = "only_i32")]
assert_eq!(engine.eval::<String>("let x = 123; x.type_of()")?, "i32");
assert_eq!(engine.eval::<String>("let x = 123; type_of(x)")?, "i32");
Ok(())
}