Add return type to function metadata.

This commit is contained in:
Stephen Chung
2020-11-22 22:15:17 +08:00
parent 6222f14fcb
commit 8d410dfc71
11 changed files with 373 additions and 50 deletions

View File

@@ -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(&params.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
///
/// ```