Add functions to iterate script function definitions.

This commit is contained in:
Stephen Chung 2020-09-24 16:10:25 +08:00
parent 12e9a8567d
commit b8aeaa84de
3 changed files with 28 additions and 14 deletions

View File

@ -25,6 +25,7 @@ New features
* Plugins support via procedural macros. * Plugins support via procedural macros.
* Scripted functions are allowed in packages. * Scripted functions are allowed in packages.
* `parse_int` and `parse_float` functions. * `parse_int` and `parse_float` functions.
* `AST::iter_functions` and `Module::iter_script_fn_info` to iterate functions.
Version 0.18.3 Version 0.18.3

View File

@ -1124,6 +1124,14 @@ impl Module {
.map(|f| f.get_shared_fn_def()) .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`. /// Create a new `Module` by evaluating an `AST`.
/// ///
/// # Examples /// # Examples
@ -1493,13 +1501,13 @@ mod file {
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
let c = self.cache.read().unwrap(); let c = self.cache.read().unwrap();
match c.get(&file_path) { if let Some(ast) = c.get(&file_path) {
Some(ast) => ( (
Module::eval_ast_as_new(scope, ast, engine) Module::eval_ast_as_new(scope, ast, engine)
.map_err(|err| err.new_position(pos))?, .map_err(|err| err.new_position(pos))?,
None, None,
), )
None => { } else {
// Load the file and compile it if not found // Load the file and compile it if not found
let ast = engine let ast = engine
.compile_file(file_path.clone()) .compile_file(file_path.clone())
@ -1511,7 +1519,6 @@ mod file {
Some(ast), Some(ast),
) )
} }
}
}; };
if let Some(ast) = ast { if let Some(ast) = ast {

View File

@ -299,6 +299,12 @@ impl AST {
self.1.retain_functions(filter); 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`. /// Clear all function definitions in the `AST`.
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
pub fn clear_functions(&mut self) { pub fn clear_functions(&mut self) {