Add Module::iter_script_fn.

This commit is contained in:
Stephen Chung 2020-06-26 22:33:27 +08:00
parent 74c82bbd9c
commit ab347fa14e
4 changed files with 52 additions and 7 deletions

View File

@ -135,7 +135,7 @@ pub use token::{get_next_token, parse_string_literal, InputStream, Token, Tokeni
#[cfg(feature = "internals")]
#[deprecated(note = "this type is volatile and may change")]
pub use parser::{Expr, ReturnType, Stmt};
pub use parser::{Expr, ReturnType, ScriptFnDef, Stmt};
#[cfg(feature = "internals")]
#[deprecated(note = "this type is volatile and may change")]

View File

@ -3,7 +3,7 @@
use crate::any::{Dynamic, Variant};
use crate::calc_fn_hash;
use crate::engine::{make_getter, make_setter, Engine, FN_IDX_GET, FN_IDX_SET};
use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn, SendSync};
use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn, SendSync, Shared};
use crate::parser::{
FnAccess,
FnAccess::{Private, Public},
@ -65,9 +65,26 @@ impl fmt::Debug for Module {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"<module vars={:?}, functions={}>",
self.variables,
self.functions.len(),
"Module(\n vars: {}\n functions: {}\n)",
self.variables
.keys()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join(", "),
self.iter_fn()
.map(|(name, access, _, f)| match f {
CallableFunction::Script(s) => s.to_string(),
_ => format!(
"{}{}()",
match access {
FnAccess::Public => "",
FnAccess::Private => "private ",
},
name
),
})
.collect::<Vec<_>>()
.join(", "),
)
}
}
@ -837,6 +854,15 @@ impl Module {
self.functions.values()
}
/// Get an iterator over all script-defined functions in the module.
pub fn iter_script_fn<'a>(&'a self) -> impl Iterator<Item = Shared<ScriptFnDef>> + 'a {
self.functions
.values()
.map(|(_, _, _, f)| f)
.filter(|f| f.is_script())
.map(|f| f.get_shared_fn_def())
}
/// Create a new `Module` by evaluating an `AST`.
///
/// # Examples

View File

@ -15,7 +15,7 @@ use crate::stdlib::{
boxed::Box,
char,
collections::HashMap,
format,
fmt, format,
iter::empty,
mem,
num::NonZeroUsize,
@ -202,6 +202,25 @@ pub struct ScriptFnDef {
pub pos: Position,
}
impl fmt::Display for ScriptFnDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}{}({})",
match self.access {
FnAccess::Public => "",
FnAccess::Private => "private ",
},
self.name,
self.params
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join(",")
)
}
}
/// `return`/`throw` statement.
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
pub enum ReturnType {

View File

@ -620,7 +620,7 @@ fn scan_comment(
}
/// Get the next token.
fn get_next_token(
pub fn get_next_token(
stream: &mut impl InputStream,
state: &mut TokenizeState,
pos: &mut Position,