2020-03-29 11:15:12 +02:00
|
|
|
#![cfg(not(feature = "no_object"))]
|
|
|
|
|
2021-09-27 04:34:24 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, Scope, INT};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_get_set() -> Result<(), Box<EvalAltResult>> {
|
2017-11-03 17:58:51 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestStruct {
|
2020-03-10 16:06:20 +01:00
|
|
|
x: INT,
|
2020-05-04 12:06:09 +02:00
|
|
|
y: INT,
|
2020-05-05 14:38:48 +02:00
|
|
|
array: Vec<INT>,
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
2020-03-10 16:06:20 +01:00
|
|
|
fn get_x(&mut self) -> INT {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
fn set_x(&mut self, new_x: INT) {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x = new_x;
|
|
|
|
}
|
|
|
|
|
2020-05-04 12:06:09 +02:00
|
|
|
fn get_y(&mut self) -> INT {
|
|
|
|
self.y
|
|
|
|
}
|
|
|
|
|
2020-03-19 06:52:10 +01:00
|
|
|
fn new() -> Self {
|
2020-10-19 08:26:15 +02:00
|
|
|
Self {
|
2020-05-05 14:38:48 +02:00
|
|
|
x: 1,
|
|
|
|
y: 0,
|
|
|
|
array: vec![1, 2, 3, 4, 5],
|
|
|
|
}
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_type::<TestStruct>();
|
|
|
|
|
|
|
|
engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x);
|
2020-05-04 12:06:09 +02:00
|
|
|
engine.register_get("y", TestStruct::get_y);
|
|
|
|
engine.register_fn("add", |value: &mut INT| *value += 41);
|
2017-11-03 17:58:51 +01:00
|
|
|
engine.register_fn("new_ts", TestStruct::new);
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = new_ts(); a.x = 500; a.x")?, 500);
|
2020-05-04 12:06:09 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = new_ts(); a.x.add(); a.x")?, 42);
|
|
|
|
assert_eq!(engine.eval::<INT>("let a = new_ts(); a.y.add(); a.y")?, 0);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
2021-05-18 15:38:09 +02:00
|
|
|
engine.register_indexer_get_set(
|
|
|
|
|value: &mut TestStruct, index: &str| value.array[index.len()],
|
|
|
|
|value: &mut TestStruct, index: &str, new_val: INT| value.array[index.len()] = new_val,
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
assert_eq!(engine.eval::<INT>(r#"let a = new_ts(); a["abc"]"#)?, 4);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(r#"let a = new_ts(); a["abc"] = 42; a["abc"]"#)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>(r"let a = new_ts(); a.abc")?, 4);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(r"let a = new_ts(); a.abc = 42; a.abc")?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-27 04:34:24 +02:00
|
|
|
fn test_get_set_chain_with_write_back() -> Result<(), Box<EvalAltResult>> {
|
2017-11-03 17:58:51 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestChild {
|
2020-03-10 16:06:20 +01:00
|
|
|
x: INT,
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestChild {
|
2020-03-10 16:06:20 +01:00
|
|
|
fn get_x(&mut self) -> INT {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
fn set_x(&mut self, new_x: INT) {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x = new_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new() -> TestChild {
|
|
|
|
TestChild { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestParent {
|
|
|
|
child: TestChild,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestParent {
|
|
|
|
fn get_child(&mut self) -> TestChild {
|
|
|
|
self.child.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_child(&mut self, new_child: TestChild) {
|
|
|
|
self.child = new_child;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new() -> TestParent {
|
2019-09-18 12:21:07 +02:00
|
|
|
TestParent {
|
|
|
|
child: TestChild::new(),
|
|
|
|
}
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_type::<TestChild>();
|
2020-03-04 15:00:01 +01:00
|
|
|
engine.register_type_with_name::<TestParent>("TestParent");
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
engine.register_get_set("x", TestChild::get_x, TestChild::set_x);
|
|
|
|
engine.register_get_set("child", TestParent::get_child, TestParent::set_child);
|
|
|
|
|
|
|
|
engine.register_fn("new_tp", TestParent::new);
|
|
|
|
|
2021-09-27 04:34:24 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = new_tp(); a.child.x")?, 1);
|
2019-09-18 12:21:07 +02:00
|
|
|
assert_eq!(
|
2021-09-27 04:34:24 +02:00
|
|
|
engine.eval::<INT>("let a = new_tp(); a.child.x = 42; a.child.x")?,
|
|
|
|
42
|
2019-09-18 12:21:07 +02:00
|
|
|
);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
assert_eq!(
|
2020-03-20 12:27:02 +01:00
|
|
|
engine.eval::<String>("let a = new_tp(); type_of(a)")?,
|
2020-03-04 15:00:01 +01:00
|
|
|
"TestParent"
|
|
|
|
);
|
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
2021-04-03 05:12:35 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_set_op_assignment() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
struct Num(INT);
|
|
|
|
|
|
|
|
impl Num {
|
|
|
|
fn get(&mut self) -> INT {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
fn set(&mut self, x: INT) {
|
|
|
|
self.0 = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine
|
|
|
|
.register_type::<Num>()
|
|
|
|
.register_fn("new_ts", || Num(40))
|
|
|
|
.register_get_set("v", Num::get, Num::set);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<Num>("let a = new_ts(); a.v = a.v + 2; a")?,
|
|
|
|
Num(42)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<Num>("let a = new_ts(); a.v += 2; a")?,
|
|
|
|
Num(42)
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-27 04:34:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_set_chain_without_write_back() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Outer {
|
|
|
|
pub inner: Inner,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Inner {
|
|
|
|
pub value: INT,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
|
|
|
scope.push(
|
|
|
|
"outer",
|
|
|
|
Outer {
|
|
|
|
inner: Inner { value: 42 },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
engine
|
|
|
|
.register_type::<Inner>()
|
|
|
|
.register_get_set(
|
|
|
|
"value",
|
|
|
|
|t: &mut Inner| t.value,
|
|
|
|
|_: &mut Inner, new: INT| panic!("Inner::value setter called with {}", new),
|
|
|
|
)
|
|
|
|
.register_type::<Outer>()
|
|
|
|
.register_get_set(
|
|
|
|
"inner",
|
|
|
|
|t: &mut Outer| t.inner.clone(),
|
|
|
|
|_: &mut Outer, new: Inner| panic!("Outer::inner setter called with {:?}", new),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_with_scope::<INT>(&mut scope, "outer.inner.value")?,
|
|
|
|
42
|
|
|
|
);
|
2021-09-27 04:59:38 +02:00
|
|
|
engine.run_with_scope(&mut scope, "print(outer.inner.value)")?;
|
2021-09-27 04:34:24 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|