Skip to content

Commit

Permalink
Merge pull request #885 from schungx/master
Browse files Browse the repository at this point in the history
Use source of AST for call_fn.
  • Loading branch information
schungx authored Jun 3, 2024
2 parents 308d07a + 307afe2 commit 88f15a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
39 changes: 20 additions & 19 deletions src/api/call_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ impl Engine {
args.parse(&mut arg_values);

self._call_fn(
options,
scope,
&mut self.new_global_runtime_state(),
&mut Caches::new(),
ast,
name.as_ref(),
arg_values.as_mut(),
options,
&mut self.new_global_runtime_state(),
&mut Caches::new(),
)
.and_then(|result| {
result.try_cast_result().map_err(|r| {
Expand All @@ -206,29 +206,41 @@ impl Engine {
#[inline(always)]
pub(crate) fn _call_fn(
&self,
options: CallFnOptions,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
ast: &AST,
name: &str,
arg_values: &mut [Dynamic],
options: CallFnOptions,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
) -> RhaiResult {
let statements = ast.statements();

let orig_source = mem::replace(&mut global.source, ast.source_raw().cloned());

let orig_lib_len = global.lib.len();
global.lib.push(ast.shared_lib().clone());

let orig_tag = options.tag.map(|v| mem::replace(&mut global.tag, v));
let mut this_ptr = options.this_ptr;

global.lib.push(ast.shared_lib().clone());
let mut this_ptr = options.this_ptr;

#[cfg(not(feature = "no_module"))]
let orig_embedded_module_resolver =
std::mem::replace(&mut global.embedded_module_resolver, ast.resolver.clone());

let rewind_scope = options.rewind_scope;

defer! { global => move |g| {
#[cfg(not(feature = "no_module"))]
{
g.embedded_module_resolver = orig_embedded_module_resolver;
}
if let Some(orig_tag) = orig_tag { g.tag = orig_tag; }
g.lib.truncate(orig_lib_len);
g.source = orig_source;
}}

let global_result = if options.eval_ast && !statements.is_empty() {
defer! {
scope if rewind_scope => rewind;
Expand Down Expand Up @@ -278,17 +290,6 @@ impl Engine {
self.dbg(global, caches, scope, this_ptr, node)?;
}

#[cfg(not(feature = "no_module"))]
{
global.embedded_module_resolver = orig_embedded_module_resolver;
}

if let Some(value) = orig_tag {
global.tag = value;
}

global.lib.truncate(orig_lib_len);

result.map_err(|err| match *err {
ERR::ErrorInFunctionCall(fn_name, _, inner_err, _) if fn_name == name => inner_err,
_ => err,
Expand Down
6 changes: 3 additions & 3 deletions src/api/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ impl Engine {
};

self._call_fn(
options,
scope,
&mut self.new_global_runtime_state(),
&mut crate::eval::Caches::new(),
ast,
name.as_ref(),
arg_values.as_mut(),
options,
&mut self.new_global_runtime_state(),
&mut crate::eval::Caches::new(),
)
}
/// Register a custom fallible function with the [`Engine`].
Expand Down
5 changes: 3 additions & 2 deletions tests/print.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rhai::{Engine, Scope, INT};
use rhai::{Dynamic, Engine, Scope, INT};

Check warning on line 1 in tests/print.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --tests --features testing-environ,only_i32,serde,metadata,internals,debugg...

unused import: `Scope`
use std::sync::{Arc, RwLock};

#[cfg(not(feature = "only_i32"))]
Expand Down Expand Up @@ -36,9 +36,10 @@ fn test_print_debug() {
engine.run("print(40 + 2)").unwrap();
let mut ast = engine.compile(r#"let x = "hello!"; debug(x)"#).unwrap();
ast.set_source("world");
engine.run_ast(&ast).unwrap();
let r = engine.eval_ast::<Dynamic>(&ast).unwrap();

// 'logbook' captures all the 'print' and 'debug' output
assert!(r.is_unit());
assert_eq!(logbook.read().unwrap().len(), 2);
assert_eq!(logbook.read().unwrap()[0], "entry: 42");
assert_eq!(logbook.read().unwrap()[1], if cfg!(not(feature = "no_position")) { r#"DEBUG of world at 1:19: "hello!""# } else { r#"DEBUG of world at none: "hello!""# });
Expand Down

0 comments on commit 88f15a5

Please sign in to comment.