Engine::gen_fn_signatures enumerates non-standard external packages.

This commit is contained in:
Stephen Chung 2022-01-17 23:15:22 +08:00
parent 86fc2f7bf1
commit d58df1fb34
4 changed files with 31 additions and 33 deletions

View File

@ -15,6 +15,7 @@ Bug fixes
* Missing `to_hex`, `to_octal` and `to_binary` for `i128` and `u128` are added. * Missing `to_hex`, `to_octal` and `to_binary` for `i128` and `u128` are added.
* `remove` for arrays and BLOB's now treat negative index correctly. * `remove` for arrays and BLOB's now treat negative index correctly.
* `parse_int` now works properly for negative numbers. * `parse_int` now works properly for negative numbers.
* `Engine::gen_fn_signatures` now generates signatures for external packages registered via `Engine::register_global_module`.
Enhancements Enhancements
------------ ------------

View File

@ -1027,7 +1027,8 @@ impl Engine {
/// Functions from the following sources are included, in order: /// Functions from the following sources are included, in order:
/// 1) Functions registered into the global namespace /// 1) Functions registered into the global namespace
/// 2) Functions in registered sub-modules /// 2) Functions in registered sub-modules
/// 3) Functions in packages (optional) /// 3) Functions in registered packages
/// 4) Functions in standard packages (optional)
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
#[inline] #[inline]
#[must_use] #[must_use]
@ -1040,14 +1041,13 @@ impl Engine {
signatures.extend(m.gen_fn_signatures().map(|f| format!("{}::{}", name, f))) signatures.extend(m.gen_fn_signatures().map(|f| format!("{}::{}", name, f)))
}); });
if include_packages { signatures.extend(
signatures.extend( self.global_modules
self.global_modules .iter()
.iter() .skip(1)
.skip(1) .filter(|m| !m.internal && (include_packages || !m.standard))
.flat_map(|m| m.gen_fn_signatures()), .flat_map(|m| m.gen_fn_signatures()),
); );
}
signatures signatures
} }

View File

@ -182,11 +182,6 @@ impl FuncInfo {
.collect(); .collect();
sig.push_str(&params.join(", ")); sig.push_str(&params.join(", "));
sig.push(')'); sig.push(')');
if !return_type.is_empty() {
sig.push_str(" -> ");
sig.push_str(&return_type);
}
} else { } else {
for x in 0..self.metadata.params { for x in 0..self.metadata.params {
sig.push('_'); sig.push('_');
@ -194,17 +189,12 @@ impl FuncInfo {
sig.push_str(", "); sig.push_str(", ");
} }
} }
sig.push(')'); sig.push(')');
}
if !self.func.is_script() { if !self.func.is_script() && !return_type.is_empty() {
sig.push(')'); sig.push_str(" -> ");
sig.push_str(&return_type);
if !return_type.is_empty() {
sig.push_str(" -> ");
sig.push_str(&return_type);
}
}
} }
sig sig
@ -596,7 +586,7 @@ impl Module {
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
params_info, params_info,
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
return_type: "Dynamic".into(), return_type: "".into(),
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments: None, comments: None,
}, },

View File

@ -196,11 +196,12 @@ impl Engine {
/// 1) Functions defined in an [`AST`][crate::AST] /// 1) Functions defined in an [`AST`][crate::AST]
/// 2) Functions registered into the global namespace /// 2) Functions registered into the global namespace
/// 3) Functions in static modules /// 3) Functions in static modules
/// 4) Functions in global modules (optional) /// 4) Functions in registered global packages
/// 5) Functions in standard packages (optional)
pub fn gen_fn_metadata_with_ast_to_json( pub fn gen_fn_metadata_with_ast_to_json(
&self, &self,
ast: &AST, ast: &AST,
include_global: bool, include_packages: bool,
) -> serde_json::Result<String> { ) -> serde_json::Result<String> {
let _ast = ast; let _ast = ast;
let mut global = ModuleMetadata::new(); let mut global = ModuleMetadata::new();
@ -211,14 +212,20 @@ impl Engine {
self.global_modules self.global_modules
.iter() .iter()
.take(if include_global { usize::MAX } else { 1 }) .filter(|m| include_packages || !m.standard)
.flat_map(|m| m.iter_fn()) .flat_map(|m| m.iter_fn())
.for_each(|f| global.functions.push(f.into())); .for_each(|f| {
let mut meta: FnMetadata = f.into();
meta.namespace = FnNamespace::Global;
global.functions.push(meta);
});
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
_ast.shared_lib() _ast.shared_lib().iter_fn().for_each(|f| {
.iter_fn() let mut meta: FnMetadata = f.into();
.for_each(|f| global.functions.push(f.into())); meta.namespace = FnNamespace::Global;
global.functions.push(meta);
});
global.functions.sort(); global.functions.sort();
@ -233,7 +240,7 @@ impl Engine {
/// 2) Functions in static modules /// 2) Functions in static modules
/// 3) Functions in global modules (optional) /// 3) Functions in global modules (optional)
#[inline(always)] #[inline(always)]
pub fn gen_fn_metadata_to_json(&self, include_global: bool) -> serde_json::Result<String> { pub fn gen_fn_metadata_to_json(&self, include_packages: bool) -> serde_json::Result<String> {
self.gen_fn_metadata_with_ast_to_json(&AST::empty(), include_global) self.gen_fn_metadata_with_ast_to_json(&AST::empty(), include_packages)
} }
} }