From 3b42cc5bb2306c2d24f78c522a63bc7501c92be7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 23 Jan 2021 09:37:27 +0800 Subject: [PATCH] Fix bug where plugin module parameters are consumed. --- RELEASES.md | 1 + src/fn_call.rs | 35 +++++++++++++---------------------- tests/plugins.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 2fec4b9e..904627bc 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -17,6 +17,7 @@ Breaking changes Bug fixes --------- +* Parameters passed to plugin module functions were sometimes erroneously consumed. This is now fixed. * Fixes compilation errors in `metadata` feature build. New features diff --git a/src/fn_call.rs b/src/fn_call.rs index 929eb825..7e277d77 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -1210,16 +1210,18 @@ impl Engine { r => r, }; + // Clone first argument if the function is not a method after-all + if let Some(first) = first_arg_value { + if !func.map(|f| f.is_method()).unwrap_or(true) { + let first_val = args[0].clone(); + args[0] = first; + *args[0] = first_val; + } + } + match func { #[cfg(not(feature = "no_function"))] Some(f) if f.is_script() => { - // Clone first argument - if let Some(first) = first_arg_value { - let first_val = args[0].clone(); - args[0] = first; - *args[0] = first_val; - } - let args = args.as_mut(); let new_scope = &mut Default::default(); let fn_def = f.get_fn_def().clone(); @@ -1241,21 +1243,10 @@ impl Engine { (self, fn_name, module.id(), &*mods, lib).into(), args.as_mut(), ), - Some(f) if f.is_native() => { - if !f.is_method() { - // Clone first argument - if let Some(first) = first_arg_value { - let first_val = args[0].clone(); - args[0] = first; - *args[0] = first_val; - } - } - - f.get_native_fn()( - (self, fn_name, module.id(), &*mods, lib).into(), - args.as_mut(), - ) - } + Some(f) if f.is_native() => f.get_native_fn()( + (self, fn_name, module.id(), &*mods, lib).into(), + args.as_mut(), + ), Some(f) => unreachable!("unknown function type: {:?}", f), None if def_val.is_some() => Ok(def_val.unwrap().clone()), None => EvalAltResult::ErrorFunctionNotFound( diff --git a/tests/plugins.rs b/tests/plugins.rs index 4ba79f6f..e42c4c73 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -101,3 +101,32 @@ fn test_plugins_package() -> Result<(), Box> { Ok(()) } + +#[test] +fn test_plugins_parameters() -> Result<(), Box> { + #[export_module] + mod rhai_std { + use rhai::*; + + pub fn noop(_: &str) {} + } + + let mut engine = Engine::new(); + + let std = exported_module!(rhai_std); + + engine.register_static_module("std", std.into()); + + assert_eq!( + engine.eval::( + r#" + let s = "hello"; + std::noop(s); + s + "# + )?, + "hello" + ); + + Ok(()) +}