From 1087c338bda1f9440b22ecacda3823cdac963f2d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 12 Dec 2020 16:31:13 +0800 Subject: [PATCH] Change output of AST::iter_functions. --- RELEASES.md | 1 + examples/repl.rs | 13 ++++++++++++- src/ast.rs | 16 +++++++++++++--- src/fn_call.rs | 2 +- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 7988a792..7a5b65db 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -15,6 +15,7 @@ Breaking changes * `Engine::on_progress` now takes `u64` instead of `&u64`. * The closure for `Engine::on_debug` now takes an additional `Position` parameter. +* `AST::iter_functions` returns a slice of parameter names instead of the internal `ScriptFnDef`. Enhancements ------------ diff --git a/examples/repl.rs b/examples/repl.rs index fe7c4bc5..a2e48709 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -144,7 +144,18 @@ fn main() { #[cfg(not(feature = "no_function"))] main_ast .iter_functions() - .for_each(|(_, _, _, _, f)| println!("{}", f)); + .for_each(|(_, access, name, _, params)| { + println!( + "{}{}({}) -> Dynamic", + if access.is_private() { "private " } else { "" }, + name, + params + .iter() + .map(|s| s.as_str()) + .collect::>() + .join(", ") + ) + }); println!(); continue; diff --git a/src/ast.rs b/src/ast.rs index 2867b48f..be07e0f7 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -82,7 +82,7 @@ pub struct ScriptFnDef { pub params: StaticVec, /// Access to external variables. #[cfg(not(feature = "no_closure"))] - pub externals: crate::stdlib::collections::HashSet, + pub externals: Vec, } impl fmt::Display for ScriptFnDef { @@ -501,8 +501,18 @@ impl AST { #[inline(always)] pub fn iter_functions<'a>( &'a self, - ) -> impl Iterator + 'a { - self.functions.iter_script_fn() + ) -> impl Iterator + 'a { + self.functions + .iter_script_fn() + .map(|(namespace, access, name, num_params, fn_def)| { + ( + namespace, + access, + name, + num_params, + fn_def.params.as_slice(), + ) + }) } /// Clear all function definitions in the [`AST`]. #[cfg(not(feature = "no_function"))] diff --git a/src/fn_call.rs b/src/fn_call.rs index 85330a81..82673b20 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -539,7 +539,7 @@ impl Engine { if !func.externals.is_empty() { captured .into_iter() - .filter(|(name, _, _)| func.externals.contains(name.as_ref())) + .filter(|(name, _, _)| func.externals.iter().any(|ex| ex == name)) .for_each(|(name, value, _)| { // Consume the scope values. scope.push_dynamic(name, value);