Change output of AST::iter_functions.

This commit is contained in:
Stephen Chung 2020-12-12 16:31:13 +08:00
parent dbdb8f43b7
commit 1087c338bd
4 changed files with 27 additions and 5 deletions

View File

@ -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
------------

View File

@ -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::<Vec<_>>()
.join(", ")
)
});
println!();
continue;

View File

@ -82,7 +82,7 @@ pub struct ScriptFnDef {
pub params: StaticVec<ImmutableString>,
/// Access to external variables.
#[cfg(not(feature = "no_closure"))]
pub externals: crate::stdlib::collections::HashSet<ImmutableString>,
pub externals: Vec<ImmutableString>,
}
impl fmt::Display for ScriptFnDef {
@ -501,8 +501,18 @@ impl AST {
#[inline(always)]
pub fn iter_functions<'a>(
&'a self,
) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize, &ScriptFnDef)> + 'a {
self.functions.iter_script_fn()
) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize, &[ImmutableString])> + '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"))]

View File

@ -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);