diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index d384c210..c1efde59 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rhai_codegen" -version = "1.4.2" +version = "1.4.3" edition = "2018" resolver = "2" authors = ["jhwgh1968", "Stephen Chung"] @@ -22,5 +22,5 @@ syn = { version = "1.0", features = ["full", "parsing", "printing", "proc-macro" quote = "1" [dev-dependencies] -rhai = { path = "..", version = "1.6", features = ["metadata"] } +rhai = { path = "..", version = "1.11", features = ["metadata"] } trybuild = "1" diff --git a/src/eval/target.rs b/src/eval/target.rs index a12aa96c..c48aa62a 100644 --- a/src/eval/target.rs +++ b/src/eval/target.rs @@ -239,6 +239,17 @@ impl<'a> Target<'a> { _ => None, } } + /// Convert a shared or reference [`Target`] into a target with an owned value. + #[inline(always)] + #[must_use] + pub fn into_owned(self) -> Self { + match self { + Self::RefMut(r) => Self::TempValue(r.clone()), + #[cfg(not(feature = "no_closure"))] + Self::SharedValue { value, .. } => Self::TempValue(value), + _ => self, + } + } /// Get the source [`Dynamic`] of the [`Target`]. #[allow(dead_code)] #[inline] diff --git a/src/func/call.rs b/src/func/call.rs index 758767fb..ae4298fe 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -1234,9 +1234,13 @@ impl Engine { .map(|(value, ..)| arg_values.push(value.flatten())) })?; - let (target, _pos) = + let (mut target, _pos) = self.search_namespace(scope, global, lib, this_ptr, first_expr, level)?; + if target.is_read_only() { + target = target.into_owned(); + } + self.track_operation(global, _pos)?; #[cfg(not(feature = "no_closure"))] diff --git a/tests/constants.rs b/tests/constants.rs index b7df1b3a..bc49e8ee 100644 --- a/tests/constants.rs +++ b/tests/constants.rs @@ -87,7 +87,7 @@ fn test_constant_mut() -> Result<(), Box> { " ) .expect_err("should error"), - EvalAltResult::ErrorAssignmentToConstant(..) + EvalAltResult::ErrorNonPureMethodCallOnConstant(..) )); let mut scope = Scope::new(); @@ -120,7 +120,7 @@ fn test_constant_mut() -> Result<(), Box> { *engine .run_with_scope(&mut scope, "MY_NUMBER.value = 42;") .expect_err("should error"), - EvalAltResult::ErrorAssignmentToConstant(..) + EvalAltResult::ErrorNonPureMethodCallOnConstant(..) )); Ok(()) diff --git a/tests/plugins.rs b/tests/plugins.rs index f5c8d966..ab752bc3 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -119,7 +119,7 @@ fn test_plugins_package() -> Result<(), Box> { assert!( matches!(*engine.run("const A = [1, 2, 3]; A.test(42);").expect_err("should error"), - EvalAltResult::ErrorAssignmentToConstant(x, ..) if x == "array") + EvalAltResult::ErrorNonPureMethodCallOnConstant(x, ..) if x == "test") ) }