From b8aeaa84de89fa558acfb4473064cd81ce590aad Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 24 Sep 2020 16:10:25 +0800 Subject: [PATCH] Add functions to iterate script function definitions. --- RELEASES.md | 1 + src/module.rs | 35 +++++++++++++++++++++-------------- src/parser.rs | 6 ++++++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 3ecbd4ab..56326471 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -25,6 +25,7 @@ New features * Plugins support via procedural macros. * Scripted functions are allowed in packages. * `parse_int` and `parse_float` functions. +* `AST::iter_functions` and `Module::iter_script_fn_info` to iterate functions. Version 0.18.3 diff --git a/src/module.rs b/src/module.rs index e03c11fa..dd089c91 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1124,6 +1124,14 @@ impl Module { .map(|f| f.get_shared_fn_def()) } + #[cfg(not(feature = "no_function"))] + pub fn iter_script_fn_info(&self, action: impl Fn(FnAccess, &str, usize)) { + self.functions.iter().for_each(|(_, (_, _, _, v))| match v { + Func::Script(ref f) => action(f.access, f.name.as_str(), f.params.len()), + _ => (), + }); + } + /// Create a new `Module` by evaluating an `AST`. /// /// # Examples @@ -1493,24 +1501,23 @@ mod file { #[cfg(feature = "sync")] let c = self.cache.read().unwrap(); - match c.get(&file_path) { - Some(ast) => ( + if let Some(ast) = c.get(&file_path) { + ( Module::eval_ast_as_new(scope, ast, engine) .map_err(|err| err.new_position(pos))?, None, - ), - None => { - // Load the file and compile it if not found - let ast = engine - .compile_file(file_path.clone()) - .map_err(|err| err.new_position(pos))?; + ) + } else { + // Load the file and compile it if not found + let ast = engine + .compile_file(file_path.clone()) + .map_err(|err| err.new_position(pos))?; - ( - Module::eval_ast_as_new(scope, &ast, engine) - .map_err(|err| err.new_position(pos))?, - Some(ast), - ) - } + ( + Module::eval_ast_as_new(scope, &ast, engine) + .map_err(|err| err.new_position(pos))?, + Some(ast), + ) } }; diff --git a/src/parser.rs b/src/parser.rs index 2c0076df..d06daf1b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -299,6 +299,12 @@ impl AST { self.1.retain_functions(filter); } + /// Iterate through all functions + #[cfg(not(feature = "no_function"))] + pub fn iter_functions(&self, action: impl Fn(FnAccess, &str, usize)) { + self.1.iter_script_fn_info(action); + } + /// Clear all function definitions in the `AST`. #[cfg(not(feature = "no_function"))] pub fn clear_functions(&mut self) {