Add Module::iter_script_fn.
This commit is contained in:
parent
74c82bbd9c
commit
ab347fa14e
@ -135,7 +135,7 @@ pub use token::{get_next_token, parse_string_literal, InputStream, Token, Tokeni
|
|||||||
|
|
||||||
#[cfg(feature = "internals")]
|
#[cfg(feature = "internals")]
|
||||||
#[deprecated(note = "this type is volatile and may change")]
|
#[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")]
|
#[cfg(feature = "internals")]
|
||||||
#[deprecated(note = "this type is volatile and may change")]
|
#[deprecated(note = "this type is volatile and may change")]
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use crate::any::{Dynamic, Variant};
|
use crate::any::{Dynamic, Variant};
|
||||||
use crate::calc_fn_hash;
|
use crate::calc_fn_hash;
|
||||||
use crate::engine::{make_getter, make_setter, Engine, FN_IDX_GET, FN_IDX_SET};
|
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::{
|
use crate::parser::{
|
||||||
FnAccess,
|
FnAccess,
|
||||||
FnAccess::{Private, Public},
|
FnAccess::{Private, Public},
|
||||||
@ -65,9 +65,26 @@ impl fmt::Debug for Module {
|
|||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"<module vars={:?}, functions={}>",
|
"Module(\n vars: {}\n functions: {}\n)",
|
||||||
self.variables,
|
self.variables
|
||||||
self.functions.len(),
|
.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()
|
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`.
|
/// Create a new `Module` by evaluating an `AST`.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -15,7 +15,7 @@ use crate::stdlib::{
|
|||||||
boxed::Box,
|
boxed::Box,
|
||||||
char,
|
char,
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
format,
|
fmt, format,
|
||||||
iter::empty,
|
iter::empty,
|
||||||
mem,
|
mem,
|
||||||
num::NonZeroUsize,
|
num::NonZeroUsize,
|
||||||
@ -202,6 +202,25 @@ pub struct ScriptFnDef {
|
|||||||
pub pos: Position,
|
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.
|
/// `return`/`throw` statement.
|
||||||
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
|
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
|
||||||
pub enum ReturnType {
|
pub enum ReturnType {
|
||||||
|
@ -620,7 +620,7 @@ fn scan_comment(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get the next token.
|
/// Get the next token.
|
||||||
fn get_next_token(
|
pub fn get_next_token(
|
||||||
stream: &mut impl InputStream,
|
stream: &mut impl InputStream,
|
||||||
state: &mut TokenizeState,
|
state: &mut TokenizeState,
|
||||||
pos: &mut Position,
|
pos: &mut Position,
|
||||||
|
Loading…
Reference in New Issue
Block a user