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.
* 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

View File

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

View File

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