Gate dead code for no_module.

This commit is contained in:
Stephen Chung
2022-01-29 11:09:43 +08:00
parent 225d9a6546
commit 6b02dde848
19 changed files with 556 additions and 282 deletions

View File

@@ -10,8 +10,6 @@ use crate::engine::{
KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_TYPE_OF,
};
use crate::eval::{EvalState, GlobalRuntimeState};
use crate::module::Namespace;
use crate::tokenizer::Token;
use crate::{
calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, FnArgsVec, FnPtr,
Identifier, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR,
@@ -150,18 +148,26 @@ impl Engine {
#[must_use]
fn gen_call_signature(
&self,
namespace: Option<&Namespace>,
#[cfg(not(feature = "no_module"))] namespace: Option<&crate::module::Namespace>,
fn_name: &str,
args: &[&mut Dynamic],
) -> String {
format!(
"{}{}{} ({})",
#[cfg(not(feature = "no_module"))]
let (ns, sep) = (
namespace.map_or_else(|| String::new(), |ns| ns.to_string()),
if namespace.is_some() {
Token::DoubleColon.literal_syntax()
crate::tokenizer::Token::DoubleColon.literal_syntax()
} else {
""
},
);
#[cfg(feature = "no_module")]
let (ns, sep) = ("", "");
format!(
"{}{}{} ({})",
ns,
sep,
fn_name,
args.iter()
.map(|a| if a.is::<ImmutableString>() {
@@ -194,6 +200,8 @@ impl Engine {
allow_dynamic: bool,
is_op_assignment: bool,
) -> Option<&'s FnResolutionCacheEntry> {
let _global = global;
if hash_script == 0 {
return None;
}
@@ -235,23 +243,29 @@ impl Engine {
})
})
.or_else(|| {
global.get_qualified_fn(hash).map(|(func, source)| {
#[cfg(not(feature = "no_module"))]
return _global.get_qualified_fn(hash).map(|(func, source)| {
FnResolutionCacheEntry {
func: func.clone(),
source: source
.map_or_else(|| Identifier::new_const(), Into::into),
}
})
});
#[cfg(feature = "no_module")]
return None;
})
.or_else(|| {
self.global_sub_modules.values().find_map(|m| {
#[cfg(not(feature = "no_module"))]
return self.global_sub_modules.values().find_map(|m| {
m.get_qualified_fn(hash).cloned().map(|func| {
FnResolutionCacheEntry {
func,
source: m.id_raw().clone(),
}
})
})
});
#[cfg(feature = "no_module")]
return None;
});
match func {
@@ -506,9 +520,16 @@ impl Engine {
}
// Raise error
_ => Err(
ERR::ErrorFunctionNotFound(self.gen_call_signature(None, name, args), pos).into(),
),
_ => Err(ERR::ErrorFunctionNotFound(
self.gen_call_signature(
#[cfg(not(feature = "no_module"))]
None,
name,
args,
),
pos,
)
.into()),
}
}
@@ -1196,6 +1217,7 @@ impl Engine {
}
/// Call a namespace-qualified function in normal function-call style.
#[cfg(not(feature = "no_module"))]
pub(crate) fn make_qualified_function_call(
&self,
scope: &mut Scope,
@@ -1203,7 +1225,7 @@ impl Engine {
state: &mut EvalState,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
namespace: &Namespace,
namespace: &crate::module::Namespace,
fn_name: &str,
args_expr: &[Expr],
constants: &[Dynamic],

View File

@@ -215,7 +215,6 @@ impl<'a> NativeCallContext<'a> {
///
/// Not available under `no_module`.
#[cfg(feature = "internals")]
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[must_use]
pub const fn global_runtime_state(&self) -> Option<&GlobalRuntimeState> {

View File

@@ -70,6 +70,7 @@ impl Engine {
}
let orig_scope_len = scope.len();
#[cfg(not(feature = "no_module"))]
let orig_imports_len = global.num_imports();
#[cfg(feature = "debugging")]
@@ -161,6 +162,7 @@ impl Engine {
// Remove arguments only, leaving new variables in the scope
scope.remove_range(orig_scope_len, args.len())
}
#[cfg(not(feature = "no_module"))]
global.truncate_imports(orig_imports_len);
// Restore state
@@ -183,6 +185,8 @@ impl Engine {
lib: &[&Module],
hash_script: u64,
) -> bool {
let _global = global;
let cache = state.fn_resolution_cache_mut();
if let Some(result) = cache.get(&hash_script).map(|v| v.is_some()) {
@@ -192,9 +196,12 @@ impl Engine {
// First check script-defined functions
let result = lib.iter().any(|&m| m.contains_fn(hash_script))
// Then check the global namespace and packages
|| self.global_modules.iter().any(|m| m.contains_fn(hash_script))
|| self.global_modules.iter().any(|m| m.contains_fn(hash_script));
#[cfg(not(feature = "no_module"))]
let result = result ||
// Then check imported modules
|| global.map_or(false, |m| m.contains_qualified_fn(hash_script))
_global.map_or(false, |m| m.contains_qualified_fn(hash_script))
// Then check sub-modules
|| self.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash_script));