From c8addb45ac778624cdb921e5dd208013dfdaeffb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 27 Feb 2021 15:06:57 +0800 Subject: [PATCH] Set position for errors in native function call. --- CHANGELOG.md | 9 +++++++++ src/fn_call.rs | 29 +++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c2553d6..2f0bbd46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ Rhai Release Notes ================== +Version 0.19.14 +=============== + +Bug fixes +--------- + +* Errors in native Rust functions now contain the correct function call positions. + + Version 0.19.13 =============== diff --git a/src/fn_call.rs b/src/fn_call.rs index 26956964..83925c1e 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -254,7 +254,7 @@ impl Engine { // Restore the original reference backup.restore_first_arg(args); - let result = result?; + let result = result.map_err(|err| err.fill_position(pos))?; // See if the function match print/debug (which requires special processing) return Ok(match fn_name { @@ -292,12 +292,16 @@ impl Engine { } let (first, second) = args.split_first_mut().unwrap(); - match run_builtin_op_assignment(fn_name, first, second[0])? { + match run_builtin_op_assignment(fn_name, first, second[0]) + .map_err(|err| err.fill_position(pos))? + { Some(_) => return Ok((Dynamic::UNIT, false)), None => (), } } else { - match run_builtin_binary_op(fn_name, args[0], args[1])? { + match run_builtin_binary_op(fn_name, args[0], args[1]) + .map_err(|err| err.fill_position(pos))? + { Some(v) => return Ok((v, false)), None => (), } @@ -1334,15 +1338,24 @@ impl Engine { result } - Some(f) if f.is_plugin_fn() => f.get_plugin_fn().clone().call( - (self, fn_name, module.id(), &*mods, lib).into(), - args.as_mut(), - ), + + Some(f) if f.is_plugin_fn() => f + .get_plugin_fn() + .clone() + .call( + (self, fn_name, module.id(), &*mods, lib).into(), + args.as_mut(), + ) + .map_err(|err| err.fill_position(pos)), + Some(f) if f.is_native() => f.get_native_fn()( (self, fn_name, module.id(), &*mods, lib).into(), args.as_mut(), - ), + ) + .map_err(|err| err.fill_position(pos)), + Some(f) => unreachable!("unknown function type: {:?}", f), + None => EvalAltResult::ErrorFunctionNotFound( format!( "{}{} ({})",