From 798e1df29870cd65996f3b7cdfaeb55eba1eb165 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 4 May 2020 18:06:09 +0800 Subject: [PATCH] Do not error when a property is read-only. --- src/engine.rs | 6 +++++- tests/get_set.rs | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index e4cdfa7c..7e849c7a 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -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)) + })?; } } 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(()) }