From 772e44aa3df24e1178f71acefc3d43a1f626c2ed Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 18 Aug 2020 09:25:43 +0800 Subject: [PATCH 1/3] Test getter. --- tests/plugins.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/plugins.rs b/tests/plugins.rs index 3379b411..5331287b 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -7,6 +7,10 @@ use rhai::{Engine, EvalAltResult, INT}; mod special_array_package { use rhai::{Array, INT}; + #[rhai_fn(get = "foo", return_raw)] + pub fn foo(array: &mut Array) -> Result> { + Ok(array[0].clone()) + } #[rhai_fn(name = "test")] pub fn len(array: &mut Array, mul: INT) -> INT { (array.len() as INT) * mul @@ -53,6 +57,7 @@ fn test_plugins_package() -> Result<(), Box> { reg_functions!(engine += greet::single(INT, bool, char)); + assert_eq!(engine.eval::("let a = [1, 2, 3]; a.foo")?, 1); assert_eq!(engine.eval::("let a = [1, 2, 3]; test(a, 2)")?, 6); assert_eq!(engine.eval::("2 + 2")?, 5); assert_eq!( From c55b0d788337de8f07cb14a2167f53c6440193e8 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 19 Aug 2020 12:50:23 +0800 Subject: [PATCH 2/3] Fix bug with plugin method call detection. --- src/fn_native.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/fn_native.rs b/src/fn_native.rs index 4d2cf19b..820e5dc7 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -261,7 +261,9 @@ impl CallableFunction { pub fn is_pure(&self) -> bool { match self { Self::Pure(_) => true, - Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => false, + Self::Method(_) | Self::Iterator(_) => false, + + Self::Plugin(p) => !p.is_method_call(), #[cfg(not(feature = "no_function"))] Self::Script(_) => false, @@ -271,7 +273,9 @@ impl CallableFunction { pub fn is_method(&self) -> bool { match self { Self::Method(_) => true, - Self::Pure(_) | Self::Iterator(_) | Self::Plugin(_) => false, + Self::Pure(_) | Self::Iterator(_) => false, + + Self::Plugin(p) => p.is_method_call(), #[cfg(not(feature = "no_function"))] Self::Script(_) => false, From 980aba77a9f037b68531fb9bc4c8ec9d6eb7a080 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 19 Aug 2020 12:53:33 +0800 Subject: [PATCH 3/3] Use &mut. --- src/packages/string_more.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 087e8e90..99af280d 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -22,12 +22,12 @@ macro_rules! gen_concat_functions { use super::super::*; #[export_fn] - pub fn append_func(x: ImmutableString, y: $arg_type) -> String { + pub fn append_func(x: &mut ImmutableString, y: $arg_type) -> String { super::super::add_append(x, y) } #[export_fn] - pub fn prepend_func(x: $arg_type, y: ImmutableString) -> String { + pub fn prepend_func(x: &mut $arg_type, y: ImmutableString) -> String { super::super::add_prepend(x, y) } } @@ -120,10 +120,10 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str ); }); -fn add_prepend(x: T, y: ImmutableString) -> String { +fn add_prepend(x: &mut T, y: ImmutableString) -> String { format!("{}{}", x, y) } -fn add_append(x: ImmutableString, y: T) -> String { +fn add_append(x: &mut ImmutableString, y: T) -> String { format!("{}{}", x, y) }