Reduce call to Module::is_empty.

This commit is contained in:
Stephen Chung 2022-11-10 23:57:46 +08:00
parent 013ee223ee
commit fca71b5ed2
8 changed files with 15 additions and 43 deletions

View File

@ -253,9 +253,7 @@ impl Engine {
let orig_lib_len = global.lib.len();
#[cfg(not(feature = "no_function"))]
if ast.has_functions() {
global.lib.push(ast.functions().clone());
}
global.lib.push(ast.shared_lib().clone());
let mut no_this_ptr = Dynamic::NULL;
let this_ptr = this_ptr.unwrap_or(&mut no_this_ptr);

View File

@ -212,9 +212,7 @@ impl Engine {
let orig_lib_len = global.lib.len();
#[cfg(not(feature = "no_function"))]
if ast.has_functions() {
global.lib.push(ast.functions().clone());
}
global.lib.push(ast.shared_lib().clone());
#[cfg(not(feature = "no_module"))]
let orig_embedded_module_resolver = mem::replace(

View File

@ -116,9 +116,8 @@ impl Engine {
global.source = ast.source_raw().cloned();
#[cfg(not(feature = "no_function"))]
if ast.has_functions() {
global.lib.push(ast.functions().clone());
}
global.lib.push(ast.shared_lib().clone());
#[cfg(not(feature = "no_module"))]
{
global.embedded_module_resolver = ast.resolver().cloned();

View File

@ -57,12 +57,11 @@ impl fmt::Debug for AST {
fp.field("body", &self.body.as_slice());
#[cfg(not(feature = "no_function"))]
if !self.lib.is_empty() {
for (.., fn_def) in self.lib.iter_script_fn() {
let sig = fn_def.to_string();
fp.field(&sig, &fn_def.body.as_slice());
}
for (.., fn_def) in self.lib.iter_script_fn() {
let sig = fn_def.to_string();
fp.field(&sig, &fn_def.body.as_slice());
}
fp.finish()
}
}
@ -231,23 +230,6 @@ impl AST {
pub(crate) fn set_doc(&mut self, doc: impl Into<crate::SmartString>) {
self.doc = doc.into();
}
/// Get the shared [module][crate::Module] containing script-defined functions.
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "internals"))]
#[inline(always)]
#[must_use]
pub(crate) fn functions(&self) -> &crate::SharedModule {
&self.lib
}
/// _(internals)_ Get the shared [module][crate::Module] containing script-defined functions.
/// Exported under the `internals` feature only.
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "internals")]
#[inline(always)]
#[must_use]
pub fn functions(&self) -> &crate::SharedModule {
&self.lib
}
/// Get the statements.
#[cfg(not(feature = "internals"))]
#[inline(always)]
@ -756,7 +738,7 @@ impl AST {
&mut self,
filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool,
) -> &mut Self {
if !self.lib.is_empty() {
if self.has_functions() {
crate::func::shared_make_mut(&mut self.lib).retain_script_functions(filter);
}
self

View File

@ -1445,7 +1445,7 @@ impl Engine {
// If new functions are defined within the eval string, it is an error
#[cfg(not(feature = "no_function"))]
if !ast.shared_lib().is_empty() {
if ast.has_functions() {
return Err(crate::PERR::WrongFnDefinition.into());
}

View File

@ -118,9 +118,7 @@ impl Engine {
.cloned()
.for_each(|(n, m)| global.push_import(n, m));
if !lib.is_empty() {
global.lib.push(lib.clone());
}
global.lib.push(lib.clone());
Some(mem::replace(&mut global.constants, constants.clone()))
} else {

View File

@ -543,14 +543,13 @@ impl Module {
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.indexed
&& !self.contains_indexed_global_functions
!self.contains_indexed_global_functions
&& self.functions.is_empty()
&& self.all_functions.as_ref().map_or(true, |m| m.is_empty())
&& self.variables.as_ref().map_or(true, |m| m.is_empty())
&& self.all_variables.as_ref().map_or(true, |m| m.is_empty())
&& self.modules.as_ref().map_or(true, |m| m.is_empty())
&& self.type_iterators.as_ref().map_or(true, |t| t.is_empty())
&& self.all_functions.as_ref().map_or(true, |m| m.is_empty())
&& self.all_variables.as_ref().map_or(true, |m| m.is_empty())
&& self
.all_type_iterators
.as_ref()

View File

@ -154,9 +154,7 @@ impl FnPtr {
let global = &mut GlobalRuntimeState::new(engine);
#[cfg(not(feature = "no_function"))]
if _ast.has_functions() {
global.lib.push(_ast.functions().clone());
}
global.lib.push(_ast.shared_lib().clone());
let ctx = (engine, self.fn_name(), None, &*global, Position::NONE).into();