diff --git a/src/engine.rs b/src/engine.rs index a0c4be00..6af55b69 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -936,7 +936,11 @@ impl Engine { let fn_name = make_setter(id); // Reuse args because the first &mut parameter will not be consumed args[1] = indexed_val; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0)?; + self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0).or_else(|err| match *err { + // If there is no setter, no need to feed it back because the property is read-only + EvalAltResult::ErrorDotExpr(_,_) => Ok(Default::default()), + err => Err(Box::new(err)) + })?; } } diff --git a/tests/get_set.rs b/tests/get_set.rs index 5859ea2e..633192ad 100644 --- a/tests/get_set.rs +++ b/tests/get_set.rs @@ -7,6 +7,7 @@ fn test_get_set() -> Result<(), Box> { #[derive(Clone)] struct TestStruct { x: INT, + y: INT, } impl TestStruct { @@ -18,8 +19,12 @@ fn test_get_set() -> Result<(), Box> { self.x = new_x; } + fn get_y(&mut self) -> INT { + self.y + } + fn new() -> Self { - TestStruct { x: 1 } + TestStruct { x: 1, y: 0 } } } @@ -28,9 +33,13 @@ fn test_get_set() -> Result<(), Box> { engine.register_type::(); engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); + engine.register_get("y", TestStruct::get_y); + engine.register_fn("add", |value: &mut INT| *value += 41); engine.register_fn("new_ts", TestStruct::new); assert_eq!(engine.eval::("let a = new_ts(); a.x = 500; a.x")?, 500); + assert_eq!(engine.eval::("let a = new_ts(); a.x.add(); a.x")?, 42); + assert_eq!(engine.eval::("let a = new_ts(); a.y.add(); a.y")?, 0); Ok(()) }