Add return type to function metadata.
This commit is contained in:
@@ -89,7 +89,7 @@ impl fmt::Display for ScriptFnDef {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}{}({})",
|
||||
"{}{}({}) -> Dynamic",
|
||||
if self.access.is_private() {
|
||||
"private "
|
||||
} else {
|
||||
@@ -151,6 +151,7 @@ impl AST {
|
||||
&mut self.0
|
||||
}
|
||||
/// Get the internal shared [`Module`] containing all script-defined functions.
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[inline(always)]
|
||||
pub(crate) fn shared_lib(&self) -> Shared<Module> {
|
||||
self.1.clone()
|
||||
|
@@ -9,7 +9,7 @@ use crate::stdlib::{
|
||||
boxed::Box,
|
||||
collections::HashMap,
|
||||
fmt, format,
|
||||
iter::empty,
|
||||
iter::{empty, once},
|
||||
num::NonZeroUsize,
|
||||
ops::{Add, AddAssign, Deref, DerefMut},
|
||||
string::{String, ToString},
|
||||
@@ -90,8 +90,15 @@ impl FuncInfo {
|
||||
let mut sig = format!("{}(", self.name);
|
||||
|
||||
if let Some(ref names) = self.param_names {
|
||||
let params: Vec<_> = names.iter().map(ImmutableString::to_string).collect();
|
||||
let mut params: Vec<_> = names.iter().map(ImmutableString::to_string).collect();
|
||||
let return_type = params.pop().unwrap_or_else(|| "()".to_string());
|
||||
sig.push_str(¶ms.join(", "));
|
||||
if return_type != "()" {
|
||||
sig.push_str(") -> ");
|
||||
sig.push_str(&return_type);
|
||||
} else {
|
||||
sig.push_str(")");
|
||||
}
|
||||
} else {
|
||||
for x in 0..self.params {
|
||||
sig.push_str("_");
|
||||
@@ -99,9 +106,9 @@ impl FuncInfo {
|
||||
sig.push_str(", ");
|
||||
}
|
||||
}
|
||||
sig.push_str(") -> Dynamic");
|
||||
}
|
||||
|
||||
sig.push_str(")");
|
||||
sig
|
||||
}
|
||||
}
|
||||
@@ -362,7 +369,14 @@ impl Module {
|
||||
access: fn_def.access,
|
||||
params: num_params,
|
||||
param_types: None,
|
||||
param_names: Some(fn_def.params.clone()),
|
||||
param_names: Some(
|
||||
fn_def
|
||||
.params
|
||||
.iter()
|
||||
.cloned()
|
||||
.chain(once("Dynamic".into()))
|
||||
.collect(),
|
||||
),
|
||||
func: fn_def.into(),
|
||||
},
|
||||
);
|
||||
@@ -483,13 +497,22 @@ impl Module {
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the parameter names and types in a registered function.
|
||||
/// Update the metadata (parameter names/types and return type) of 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 {
|
||||
///
|
||||
/// Each parameter name/type pair should be a single string of the format: `var_name: type`.
|
||||
///
|
||||
/// The last entry in the list should be the return type of the function.
|
||||
/// In other words, the number of entries should be one larger than the number of parameters.
|
||||
pub fn update_fn_metadata<'a>(
|
||||
&mut self,
|
||||
hash_fn: u64,
|
||||
arg_names: impl AsRef<[&'a 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());
|
||||
f.param_names = Some(arg_names.as_ref().iter().map(|&n| n.into()).collect());
|
||||
}
|
||||
self
|
||||
}
|
||||
@@ -573,6 +596,10 @@ impl Module {
|
||||
///
|
||||
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -632,6 +659,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -663,6 +694,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -696,6 +731,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -733,6 +772,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust getter function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -760,6 +803,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -799,6 +846,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -843,6 +894,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing setter Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -880,6 +935,10 @@ impl Module {
|
||||
/// Panics if the type is [`Array`] or [`Map`].
|
||||
/// Indexers for arrays, object maps and strings cannot be registered.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -918,6 +977,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -963,6 +1026,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -1018,6 +1085,10 @@ impl Module {
|
||||
/// Panics if the type is [`Array`] or [`Map`].
|
||||
/// Indexers for arrays, object maps and strings cannot be registered.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -1079,6 +1150,10 @@ impl Module {
|
||||
/// Panics if the type is [`Array`] or [`Map`].
|
||||
/// Indexers for arrays, object maps and strings cannot be registered.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -1114,6 +1189,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -1166,6 +1245,10 @@ impl Module {
|
||||
///
|
||||
/// If there is a similar existing Rust function, it is replaced.
|
||||
///
|
||||
/// # Function Metadata
|
||||
///
|
||||
/// No metadata for the function is registered. Use `update_fn_metadata` to add metadata.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
|
@@ -49,7 +49,11 @@ macro_rules! reg_range {
|
||||
$(
|
||||
$lib.set_iterator::<Range<$y>>();
|
||||
let hash = $lib.set_fn_2($x, get_range::<$y>);
|
||||
$lib.update_fn_param_names(hash, &[concat!("from: ", stringify!($y)), concat!("to: ", stringify!($y))]);
|
||||
$lib.update_fn_metadata(hash, [
|
||||
concat!("from: ", stringify!($y)),
|
||||
concat!("to: ", stringify!($y)),
|
||||
concat!("Iterator<Item=", stringify!($y), ">")
|
||||
]);
|
||||
)*
|
||||
)
|
||||
}
|
||||
@@ -61,7 +65,11 @@ macro_rules! reg_step {
|
||||
$(
|
||||
$lib.set_iterator::<StepRange<$y>>();
|
||||
let hash = $lib.set_fn_3($x, get_step_range::<$y>);
|
||||
$lib.update_fn_param_names(hash, &[concat!("from: ", stringify!($y)), concat!("to: ", stringify!($y)), concat!("step: ", stringify!($y))]);
|
||||
$lib.update_fn_metadata(hash, [
|
||||
concat!("from: ", stringify!($y)),
|
||||
concat!("to: ", stringify!($y)),
|
||||
concat!("step: ", stringify!($y)), concat!("Iterator<Item=", stringify!($y), ">")
|
||||
]);
|
||||
)*
|
||||
)
|
||||
}
|
||||
@@ -69,7 +77,7 @@ macro_rules! reg_step {
|
||||
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
||||
lib.set_iterator::<Range<INT>>();
|
||||
let hash = lib.set_fn_2("range", get_range::<INT>);
|
||||
lib.update_fn_param_names(hash, &["from: INT", "to: INT"]);
|
||||
lib.update_fn_metadata(hash, ["from: INT", "to: INT", "Iterator<Item=INT>"]);
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
@@ -83,7 +91,7 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
||||
|
||||
lib.set_iterator::<StepRange<INT>>();
|
||||
let hash = lib.set_fn_3("range", get_step_range::<INT>);
|
||||
lib.update_fn_param_names(hash, &["from: INT", "to: INT", "step: INT"]);
|
||||
lib.update_fn_metadata(hash, ["from: INT", "to: INT", "step: INT", "Iterator<Item=INT>"]);
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
|
@@ -38,4 +38,7 @@ pub trait PluginFunction {
|
||||
|
||||
/// Return a boxed slice of type ID's of the function's parameters.
|
||||
fn input_types(&self) -> Box<[TypeId]>;
|
||||
|
||||
/// Return a string slice of the function's return type.
|
||||
fn return_type(&self) -> &'static str;
|
||||
}
|
||||
|
Reference in New Issue
Block a user