Separate return type name and use references for serialization.
This commit is contained in:
parent
d7960dfe80
commit
f74486f904
@ -47,9 +47,12 @@ pub struct FuncInfo {
|
|||||||
pub params: usize,
|
pub params: usize,
|
||||||
/// Parameter types (if applicable).
|
/// Parameter types (if applicable).
|
||||||
pub param_types: StaticVec<TypeId>,
|
pub param_types: StaticVec<TypeId>,
|
||||||
/// Parameter names (if available).
|
/// Parameter names and types (if available).
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
pub param_names: StaticVec<Identifier>,
|
pub param_names_and_types: StaticVec<Identifier>,
|
||||||
|
/// Return type name.
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
pub return_type_name: Identifier,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FuncInfo {
|
impl FuncInfo {
|
||||||
@ -60,16 +63,21 @@ impl FuncInfo {
|
|||||||
pub fn gen_signature(&self) -> String {
|
pub fn gen_signature(&self) -> String {
|
||||||
let mut sig = format!("{}(", self.name);
|
let mut sig = format!("{}(", self.name);
|
||||||
|
|
||||||
if !self.param_names.is_empty() {
|
if !self.param_names_and_types.is_empty() {
|
||||||
let mut params: StaticVec<Box<str>> =
|
let params: StaticVec<_> = self
|
||||||
self.param_names.iter().map(|s| s.as_str().into()).collect();
|
.param_names_and_types
|
||||||
let return_type = params.pop().unwrap_or_else(|| "()".into());
|
.iter()
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.collect();
|
||||||
sig.push_str(¶ms.join(", "));
|
sig.push_str(¶ms.join(", "));
|
||||||
if &*return_type != "()" {
|
sig.push_str(")");
|
||||||
sig.push_str(") -> ");
|
|
||||||
sig.push_str(&return_type);
|
match self.return_type_name.as_str() {
|
||||||
} else {
|
"" | "()" => (),
|
||||||
sig.push(')');
|
ty => {
|
||||||
|
sig.push_str(" -> ");
|
||||||
|
sig.push_str(ty);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for x in 0..self.params {
|
for x in 0..self.params {
|
||||||
@ -82,7 +90,12 @@ impl FuncInfo {
|
|||||||
if self.func.is_script() {
|
if self.func.is_script() {
|
||||||
sig.push(')');
|
sig.push(')');
|
||||||
} else {
|
} else {
|
||||||
sig.push_str(") -> ?");
|
sig.push_str(")");
|
||||||
|
|
||||||
|
match self.return_type_name.as_str() {
|
||||||
|
"()" => (),
|
||||||
|
_ => sig.push_str(" -> ?"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -457,8 +470,12 @@ impl Module {
|
|||||||
// None + function name + number of arguments.
|
// None + function name + number of arguments.
|
||||||
let num_params = fn_def.params.len();
|
let num_params = fn_def.params.len();
|
||||||
let hash_script = crate::calc_fn_hash(&fn_def.name, num_params);
|
let hash_script = crate::calc_fn_hash(&fn_def.name, num_params);
|
||||||
let mut param_names = fn_def.params.clone();
|
#[cfg(feature = "metadata")]
|
||||||
param_names.push("Dynamic".into());
|
let param_names_and_types = fn_def
|
||||||
|
.params
|
||||||
|
.iter()
|
||||||
|
.map(|v| self.identifiers.get(v.as_str()))
|
||||||
|
.collect();
|
||||||
self.functions.insert(
|
self.functions.insert(
|
||||||
hash_script,
|
hash_script,
|
||||||
FuncInfo {
|
FuncInfo {
|
||||||
@ -468,7 +485,9 @@ impl Module {
|
|||||||
params: num_params,
|
params: num_params,
|
||||||
param_types: StaticVec::new_const(),
|
param_types: StaticVec::new_const(),
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
param_names,
|
param_names_and_types,
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
return_type_name: self.identifiers.get("Dynamic"),
|
||||||
func: Into::<CallableFunction>::into(fn_def).into(),
|
func: Into::<CallableFunction>::into(fn_def).into(),
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
@ -613,13 +632,20 @@ impl Module {
|
|||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn update_fn_metadata(&mut self, hash_fn: u64, arg_names: &[impl AsRef<str>]) -> &mut Self {
|
pub fn update_fn_metadata(&mut self, hash_fn: u64, arg_names: &[impl AsRef<str>]) -> &mut Self {
|
||||||
let param_names = arg_names
|
let mut param_names: StaticVec<_> = arg_names
|
||||||
.iter()
|
.iter()
|
||||||
.map(|name| self.identifiers.get(name.as_ref()))
|
.map(|name| self.identifiers.get(name.as_ref()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if let Some(f) = self.functions.get_mut(&hash_fn) {
|
if let Some(f) = self.functions.get_mut(&hash_fn) {
|
||||||
f.param_names = param_names;
|
let (param_names, return_type_name) = if param_names.len() > f.params {
|
||||||
|
let return_type = param_names.pop().expect("exists");
|
||||||
|
(param_names, return_type)
|
||||||
|
} else {
|
||||||
|
(param_names, Default::default())
|
||||||
|
};
|
||||||
|
f.param_names_and_types = param_names;
|
||||||
|
f.return_type_name = return_type_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -664,6 +690,15 @@ impl Module {
|
|||||||
/// # WARNING - Low Level API
|
/// # WARNING - Low Level API
|
||||||
///
|
///
|
||||||
/// This function is very low level.
|
/// This function is very low level.
|
||||||
|
///
|
||||||
|
/// ## Parameter Names and Types
|
||||||
|
///
|
||||||
|
/// Each parameter name/type pair should be a single string of the format: `var_name: type`.
|
||||||
|
///
|
||||||
|
/// ## Return 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.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_fn(
|
pub fn set_fn(
|
||||||
&mut self,
|
&mut self,
|
||||||
@ -686,13 +721,20 @@ impl Module {
|
|||||||
param_types.shrink_to_fit();
|
param_types.shrink_to_fit();
|
||||||
|
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
let mut param_names: StaticVec<_> = _arg_names
|
let (param_names, return_type_name) = {
|
||||||
.iter()
|
let mut names = _arg_names
|
||||||
.flat_map(|&p| p.iter())
|
.iter()
|
||||||
.map(|&arg| self.identifiers.get(arg))
|
.flat_map(|&p| p.iter())
|
||||||
.collect();
|
.map(|&arg| self.identifiers.get(arg))
|
||||||
#[cfg(feature = "metadata")]
|
.collect::<StaticVec<_>>();
|
||||||
param_names.shrink_to_fit();
|
let return_type = if names.len() > arg_types.len() {
|
||||||
|
names.pop().expect("exists")
|
||||||
|
} else {
|
||||||
|
Default::default()
|
||||||
|
};
|
||||||
|
names.shrink_to_fit();
|
||||||
|
(names, return_type)
|
||||||
|
};
|
||||||
|
|
||||||
let hash_fn = calc_native_fn_hash(empty::<&str>(), name.as_ref(), ¶m_types);
|
let hash_fn = calc_native_fn_hash(empty::<&str>(), name.as_ref(), ¶m_types);
|
||||||
|
|
||||||
@ -705,7 +747,9 @@ impl Module {
|
|||||||
params: param_types.len(),
|
params: param_types.len(),
|
||||||
param_types,
|
param_types,
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
param_names,
|
param_names_and_types: param_names,
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
return_type_name,
|
||||||
func: func.into(),
|
func: func.into(),
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
@ -1284,8 +1328,8 @@ impl Module {
|
|||||||
|
|
||||||
/// Get an iterator to the sub-modules in the [`Module`].
|
/// Get an iterator to the sub-modules in the [`Module`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn iter_sub_modules(&self) -> impl Iterator<Item = (&str, Shared<Module>)> {
|
pub fn iter_sub_modules(&self) -> impl Iterator<Item = (&str, &Shared<Module>)> {
|
||||||
self.modules.iter().map(|(k, m)| (k.as_str(), m.clone()))
|
self.modules.iter().map(|(k, m)| (k.as_str(), m))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get an iterator to the variables in the [`Module`].
|
/// Get an iterator to the variables in the [`Module`].
|
||||||
|
@ -48,33 +48,24 @@ impl From<crate::FnAccess> for FnAccess {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct FnParam {
|
struct FnParam<'a> {
|
||||||
pub name: Box<str>,
|
|
||||||
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
|
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
|
||||||
pub typ: Option<Box<str>>,
|
pub name: Option<&'a str>,
|
||||||
|
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
|
||||||
|
pub typ: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for FnParam {
|
impl PartialOrd for FnParam<'_> {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
Some(match self.name.partial_cmp(&other.name).expect("succeed") {
|
Some(match self.name.partial_cmp(&other.name).expect("succeed") {
|
||||||
Ordering::Less => Ordering::Less,
|
Ordering::Less => Ordering::Less,
|
||||||
Ordering::Greater => Ordering::Greater,
|
Ordering::Greater => Ordering::Greater,
|
||||||
Ordering::Equal => match (self.typ.is_none(), other.typ.is_none()) {
|
Ordering::Equal => self.typ.partial_cmp(other.typ).expect("succeed"),
|
||||||
(true, true) => Ordering::Equal,
|
|
||||||
(true, false) => Ordering::Greater,
|
|
||||||
(false, true) => Ordering::Less,
|
|
||||||
(false, false) => self
|
|
||||||
.typ
|
|
||||||
.as_ref()
|
|
||||||
.expect("`Some`")
|
|
||||||
.partial_cmp(other.typ.as_ref().expect("`Some`"))
|
|
||||||
.expect("succeed"),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ord for FnParam {
|
impl Ord for FnParam<'_> {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
match self.name.cmp(&other.name) {
|
match self.name.cmp(&other.name) {
|
||||||
Ordering::Equal => self.typ.cmp(&other.typ),
|
Ordering::Equal => self.typ.cmp(&other.typ),
|
||||||
@ -85,7 +76,7 @@ impl Ord for FnParam {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct FnMetadata {
|
struct FnMetadata<'a> {
|
||||||
pub base_hash: u64,
|
pub base_hash: u64,
|
||||||
pub full_hash: u64,
|
pub full_hash: u64,
|
||||||
pub namespace: FnNamespace,
|
pub namespace: FnNamespace,
|
||||||
@ -95,21 +86,21 @@ struct FnMetadata {
|
|||||||
pub typ: FnType,
|
pub typ: FnType,
|
||||||
pub num_params: usize,
|
pub num_params: usize,
|
||||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||||
pub params: Vec<FnParam>,
|
pub params: Vec<FnParam<'a>>,
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub return_type: Option<Box<str>>,
|
pub return_type_name: Option<&'a str>,
|
||||||
pub signature: String,
|
pub signature: String,
|
||||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||||
pub doc_comments: Vec<Box<str>>,
|
pub doc_comments: Vec<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for FnMetadata {
|
impl PartialOrd for FnMetadata<'_> {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ord for FnMetadata {
|
impl Ord for FnMetadata<'_> {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
match self.name.cmp(&other.name) {
|
match self.name.cmp(&other.name) {
|
||||||
Ordering::Equal => match self.num_params.cmp(&other.num_params) {
|
Ordering::Equal => match self.num_params.cmp(&other.num_params) {
|
||||||
@ -121,8 +112,8 @@ impl Ord for FnMetadata {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&crate::module::FuncInfo> for FnMetadata {
|
impl<'a> From<&'a crate::module::FuncInfo> for FnMetadata<'a> {
|
||||||
fn from(info: &crate::module::FuncInfo) -> Self {
|
fn from(info: &'a crate::module::FuncInfo) -> Self {
|
||||||
let base_hash = calc_fn_hash(&info.name, info.params);
|
let base_hash = calc_fn_hash(&info.name, info.params);
|
||||||
let (typ, full_hash) = if info.func.is_script() {
|
let (typ, full_hash) = if info.func.is_script() {
|
||||||
(FnType::Script, base_hash)
|
(FnType::Script, base_hash)
|
||||||
@ -142,24 +133,22 @@ impl From<&crate::module::FuncInfo> for FnMetadata {
|
|||||||
typ,
|
typ,
|
||||||
num_params: info.params,
|
num_params: info.params,
|
||||||
params: info
|
params: info
|
||||||
.param_names
|
.param_names_and_types
|
||||||
.iter()
|
.iter()
|
||||||
.take(info.params)
|
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
let mut seg = s.splitn(2, ':');
|
let mut seg = s.splitn(2, ':');
|
||||||
let name = seg
|
let name = match seg.next().map(&str::trim).unwrap_or("_") {
|
||||||
.next()
|
"_" => None,
|
||||||
.map(|s| s.trim().into())
|
s => Some(s),
|
||||||
.unwrap_or_else(|| "_".into());
|
};
|
||||||
let typ = seg.next().map(|s| s.trim().into());
|
let typ = seg.next().map(&str::trim);
|
||||||
FnParam { name, typ }
|
FnParam { name, typ }
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
return_type: info
|
return_type_name: match info.return_type_name.as_str() {
|
||||||
.param_names
|
"" | "()" => None,
|
||||||
.last()
|
ty => Some(ty),
|
||||||
.map(|s| s.as_str().into())
|
},
|
||||||
.or_else(|| Some("()".into())),
|
|
||||||
signature: info.gen_signature(),
|
signature: info.gen_signature(),
|
||||||
doc_comments: if info.func.is_script() {
|
doc_comments: if info.func.is_script() {
|
||||||
#[cfg(feature = "no_function")]
|
#[cfg(feature = "no_function")]
|
||||||
@ -171,7 +160,7 @@ impl From<&crate::module::FuncInfo> for FnMetadata {
|
|||||||
.expect("script-defined function")
|
.expect("script-defined function")
|
||||||
.comments
|
.comments
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or_else(|| Vec::new(), |v| v.to_vec())
|
.map_or_else(|| Vec::new(), |v| v.iter().map(|s| &**s).collect())
|
||||||
} else {
|
} else {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
},
|
},
|
||||||
@ -181,14 +170,14 @@ impl From<&crate::module::FuncInfo> for FnMetadata {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct ModuleMetadata {
|
struct ModuleMetadata<'a> {
|
||||||
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
|
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
|
||||||
pub modules: BTreeMap<String, Self>,
|
pub modules: BTreeMap<&'a str, Self>,
|
||||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
pub functions: Vec<FnMetadata>,
|
pub functions: Vec<FnMetadata<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleMetadata {
|
impl ModuleMetadata<'_> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -198,15 +187,15 @@ impl ModuleMetadata {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&crate::Module> for ModuleMetadata {
|
impl<'a> From<&'a crate::Module> for ModuleMetadata<'a> {
|
||||||
fn from(module: &crate::Module) -> Self {
|
fn from(module: &'a crate::Module) -> Self {
|
||||||
let mut functions: Vec<_> = module.iter_fn().map(|f| f.into()).collect();
|
let mut functions: Vec<_> = module.iter_fn().map(|f| f.into()).collect();
|
||||||
functions.sort();
|
functions.sort();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
modules: module
|
modules: module
|
||||||
.iter_sub_modules()
|
.iter_sub_modules()
|
||||||
.map(|(name, m)| (name.to_string(), m.as_ref().into()))
|
.map(|(name, m)| (name, m.as_ref().into()))
|
||||||
.collect(),
|
.collect(),
|
||||||
functions,
|
functions,
|
||||||
}
|
}
|
||||||
@ -232,20 +221,14 @@ impl Engine {
|
|||||||
let _ast = ast;
|
let _ast = ast;
|
||||||
let mut global = ModuleMetadata::new();
|
let mut global = ModuleMetadata::new();
|
||||||
|
|
||||||
if include_global {
|
|
||||||
self.global_modules
|
|
||||||
.iter()
|
|
||||||
.take(self.global_modules.len() - 1)
|
|
||||||
.flat_map(|m| m.iter_fn())
|
|
||||||
.for_each(|f| global.functions.push(f.into()));
|
|
||||||
}
|
|
||||||
|
|
||||||
self.global_sub_modules.iter().for_each(|(name, m)| {
|
self.global_sub_modules.iter().for_each(|(name, m)| {
|
||||||
global.modules.insert(name.to_string(), m.as_ref().into());
|
global.modules.insert(name, m.as_ref().into());
|
||||||
});
|
});
|
||||||
|
|
||||||
self.global_namespace()
|
self.global_modules
|
||||||
.iter_fn()
|
.iter()
|
||||||
|
.take(if include_global { usize::MAX } else { 1 })
|
||||||
|
.flat_map(|m| m.iter_fn())
|
||||||
.for_each(|f| global.functions.push(f.into()));
|
.for_each(|f| global.functions.push(f.into()));
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Loading…
Reference in New Issue
Block a user