Do not error when a property is read-only.

This commit is contained in:
Stephen Chung 2020-05-04 18:06:09 +08:00
parent d83b829810
commit 798e1df298
2 changed files with 15 additions and 2 deletions

View File

@ -885,7 +885,11 @@ impl Engine {
if let Expr::Property(id, pos) = dot_lhs.as_ref() {
let fn_name = make_setter(id);
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))
})?;
}
}

View File

@ -7,6 +7,7 @@ fn test_get_set() -> Result<(), Box<EvalAltResult>> {
#[derive(Clone)]
struct TestStruct {
x: INT,
y: INT,
}
impl TestStruct {
@ -18,8 +19,12 @@ fn test_get_set() -> Result<(), Box<EvalAltResult>> {
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<EvalAltResult>> {
engine.register_type::<TestStruct>();
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::<INT>("let a = new_ts(); a.x = 500; a.x")?, 500);
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);
Ok(())
}