From 44f8d9e429096f2a1fe9e071d3dd28afa5cf42c8 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 5 Oct 2020 21:52:39 +0800 Subject: [PATCH] Refine Module::iter_script_fn_info. --- RELEASES.md | 2 +- src/fn_call.rs | 2 +- src/module/mod.rs | 58 +++++++++++++++++++++++++++++++++++++---------- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index a980377e..95ee14e7 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -8,8 +8,8 @@ Breaking changes ---------------- * `AST::iter_functions` now returns an iterator instead of taking a closure. -* `Module::iter_script_fn_info` is removed and merged into `Module::iter_script_fn`. * `Module::get_script_function_by_signature` renamed to `Module::get_script_fn` and returns `&>`. +* `Module::num_fn`, `Module::num_var` and `Module::num_iter` are removed and merged into `Module::count`. * The `merge_namespaces` parameter to `Module::eval_ast_as_new` is removed and now defaults to `true`. * `GlobalFileModuleResolver` is removed because its performance gain over the `FileModuleResolver` is no longer very significant. * The following `EvalAltResult` variants are removed and merged into `EvalAltResult::ErrorMismatchDataType`: `ErrorCharMismatch`, `ErrorNumericIndexExpr`, `ErrorStringIndexExpr`, `ErrorImportExpr`, `ErrorLogicGuard`, `ErrorBooleanArgMismatch` diff --git a/src/fn_call.rs b/src/fn_call.rs index 557a3fd2..c9439637 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -663,7 +663,7 @@ impl Engine { )?; // If new functions are defined within the eval string, it is an error - if ast.lib().num_fn() != 0 { + if ast.lib().count().0 != 0 { return Err(ParseErrorType::WrongFnDefinition.into()); } diff --git a/src/module/mod.rs b/src/module/mod.rs index 47bfc398..6d9681e6 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1228,17 +1228,13 @@ impl Module { self } - /// Get the number of variables in the module. - pub fn num_var(&self) -> usize { - self.variables.len() - } - /// Get the number of functions in the module. - pub fn num_fn(&self) -> usize { - self.variables.len() - } - /// Get the number of type iterators in the module. - pub fn num_iter(&self) -> usize { - self.variables.len() + /// Get the number of variables, functions and type iterators in the module. + pub fn count(&self) -> (usize, usize, usize) { + ( + self.variables.len(), + self.variables.len(), + self.variables.len(), + ) } /// Get an iterator to the variables in the module. @@ -1252,8 +1248,14 @@ impl Module { } /// Get an iterator over all script-defined functions in the module. + /// + /// Function metadata includes: + /// 1) Access mode (`FnAccess::Public` or `FnAccess::Private`). + /// 2) Function name (as string slice). + /// 3) Number of parameters. + /// 4) Shared reference to function definition `ScriptFnDef`. #[cfg(not(feature = "no_function"))] - pub fn iter_script_fn<'a>( + pub(crate) fn iter_script_fn<'a>( &'a self, ) -> impl Iterator)> + 'a { self.functions @@ -1267,6 +1269,38 @@ impl Module { }) } + /// Get an iterator over all script-defined functions in the module. + /// + /// Function metadata includes: + /// 1) Access mode (`FnAccess::Public` or `FnAccess::Private`). + /// 2) Function name (as string slice). + /// 3) Number of parameters. + #[cfg(not(feature = "no_function"))] + #[cfg(not(feature = "internals"))] + pub fn iter_script_fn_info(&self) -> impl Iterator { + self.functions + .values() + .filter(|(_, _, _, _, f)| f.is_script()) + .map(|(name, access, num_params, _, _)| (*access, name.as_str(), *num_params)) + } + + /// Get an iterator over all script-defined functions in the module. + /// + /// Function metadata includes: + /// 1) Access mode (`FnAccess::Public` or `FnAccess::Private`). + /// 2) Function name (as string slice). + /// 3) Number of parameters. + /// 4) _[INTERNALS]_ Shared reference to function definition `ScriptFnDef`. + /// Exported under the internals feature only. + #[cfg(not(feature = "no_function"))] + #[cfg(feature = "internals")] + #[inline(always)] + pub fn iter_script_fn_info( + &self, + ) -> impl Iterator)> { + self.iter_script_fn() + } + /// Create a new `Module` by evaluating an `AST`. /// /// The entire `AST` is encapsulated into each function, allowing functions