Add gen_fn_siguatures API.

This commit is contained in:
Stephen Chung
2020-11-22 17:21:34 +08:00
parent 739dce72e3
commit 07fe132e1a
17 changed files with 400 additions and 83 deletions

View File

@@ -79,7 +79,31 @@ pub struct FuncInfo {
/// Number of parameters.
pub params: usize,
/// Parameter types (if applicable).
pub types: Option<StaticVec<TypeId>>,
pub param_types: Option<StaticVec<TypeId>>,
/// Parameter names (if available).
pub param_names: Option<StaticVec<ImmutableString>>,
}
impl FuncInfo {
/// Generate a signature of the function.
pub fn gen_signature(&self) -> String {
let mut sig = format!("{}(", self.name);
if let Some(ref names) = self.param_names {
let params: Vec<_> = names.iter().map(ImmutableString::to_string).collect();
sig.push_str(&params.join(", "));
} else {
for x in 0..self.params {
sig.push_str("_");
if x < self.params - 1 {
sig.push_str(", ");
}
}
}
sig.push_str(")");
sig
}
}
/// A module which may contain variables, sub-modules, external Rust functions,
@@ -230,6 +254,14 @@ impl Module {
self.indexed
}
/// Generate signatures for all the functions in the module.
pub fn gen_fn_signatures<'a>(&'a self) -> impl Iterator<Item = String> + 'a {
self.functions
.values()
.filter(|FuncInfo { access, .. }| !access.is_private())
.map(FuncInfo::gen_signature)
}
/// Does a variable exist in the module?
///
/// # Example
@@ -329,7 +361,8 @@ impl Module {
namespace: FnNamespace::Internal,
access: fn_def.access,
params: num_params,
types: None,
param_types: None,
param_names: Some(fn_def.params.clone()),
func: fn_def.into(),
},
);
@@ -450,6 +483,17 @@ impl Module {
}
}
/// Update the parameter names and types in a registered function.
///
/// The [`u64`] hash is calculated either by the function [`crate::calc_native_fn_hash`] or
/// the function [`crate::calc_script_fn_hash`].
pub fn update_fn_param_names(&mut self, hash_fn: u64, arg_names: &[&str]) -> &mut Self {
if let Some(f) = self.functions.get_mut(&hash_fn) {
f.param_names = Some(arg_names.iter().map(|&n| n.into()).collect());
}
self
}
/// Set a Rust function into the module, returning a hash key.
///
/// If there is an existing Rust function of the same hash, it is replaced.
@@ -462,6 +506,7 @@ impl Module {
name: impl Into<String>,
namespace: FnNamespace,
access: FnAccess,
arg_names: Option<&[&str]>,
arg_types: &[TypeId],
func: CallableFunction,
) -> u64 {
@@ -488,7 +533,8 @@ impl Module {
namespace,
access,
params: params.len(),
types: Some(params),
param_types: Some(params),
param_names: arg_names.map(|p| p.iter().map(|&v| v.into()).collect()),
func: func.into(),
},
);
@@ -576,6 +622,7 @@ impl Module {
name,
namespace,
access,
None,
arg_types,
CallableFunction::from_method(Box::new(f)),
)
@@ -606,6 +653,7 @@ impl Module {
name,
FnNamespace::Internal,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_pure(Box::new(f)),
)
@@ -638,6 +686,7 @@ impl Module {
name,
FnNamespace::Internal,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_pure(Box::new(f)),
)
@@ -673,6 +722,7 @@ impl Module {
name,
namespace,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_method(Box::new(f)),
)
@@ -738,6 +788,7 @@ impl Module {
name,
FnNamespace::Internal,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_pure(Box::new(f)),
)
@@ -780,6 +831,7 @@ impl Module {
name,
namespace,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_method(Box::new(f)),
)
@@ -900,6 +952,7 @@ impl Module {
name,
FnNamespace::Internal,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_pure(Box::new(f)),
)
@@ -948,6 +1001,7 @@ impl Module {
name,
namespace,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_method(Box::new(f)),
)
@@ -1008,6 +1062,7 @@ impl Module {
crate::engine::FN_IDX_SET,
FnNamespace::Internal,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_method(Box::new(f)),
)
@@ -1100,6 +1155,7 @@ impl Module {
name,
FnNamespace::Internal,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_pure(Box::new(f)),
)
@@ -1155,6 +1211,7 @@ impl Module {
name,
namespace,
FnAccess::Public,
None,
&arg_types,
CallableFunction::from_method(Box::new(f)),
)
@@ -1569,7 +1626,7 @@ impl Module {
name,
namespace,
params,
types,
param_types: types,
func,
..
},