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

@@ -3,25 +3,23 @@
use super::{EvalContext, EvalState, GlobalRuntimeState, Target};
use crate::ast::{Expr, FnCallExpr, OpAssignment};
use crate::engine::{KEYWORD_THIS, OP_CONCAT};
use crate::module::Namespace;
use crate::types::dynamic::AccessMode;
use crate::{
Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared, StaticVec, ERR,
};
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, ERR};
use std::num::NonZeroUsize;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
impl Engine {
/// Search for a module within an imports stack.
#[cfg(not(feature = "no_module"))]
#[inline]
#[must_use]
pub(crate) fn search_imports(
&self,
global: &GlobalRuntimeState,
state: &mut EvalState,
namespace: &Namespace,
) -> Option<Shared<Module>> {
namespace: &crate::module::Namespace,
) -> Option<crate::Shared<Module>> {
let root = &namespace[0].name;
// Qualified - check if the root module is directly indexed
@@ -59,7 +57,10 @@ impl Engine {
}
Expr::Variable(None, _var_pos, v) => match v.as_ref() {
// Normal variable access
#[cfg(not(feature = "no_module"))]
(_, None, _) => self.search_scope_only(scope, global, state, lib, this_ptr, expr),
#[cfg(feature = "no_module")]
(_, (), _) => self.search_scope_only(scope, global, state, lib, this_ptr, expr),
// Qualified variable access
#[cfg(not(feature = "no_module"))]
@@ -117,9 +118,6 @@ impl Engine {
Err(ERR::ErrorModuleNotFound(namespace.to_string(), namespace[0].pos).into())
}
#[cfg(feature = "no_module")]
(_, Some((_, _)), _) => unreachable!("qualified access under no_module"),
},
_ => unreachable!("Expr::Variable expected but gets {:?}", expr),
}
@@ -211,6 +209,7 @@ impl Engine {
) -> RhaiResult {
let FnCallExpr {
name,
#[cfg(not(feature = "no_module"))]
namespace,
capture_parent_scope: capture,
hashes,
@@ -219,26 +218,27 @@ impl Engine {
..
} = expr;
#[cfg(not(feature = "no_module"))]
if let Some(namespace) = namespace.as_ref() {
// Qualified function call
let hash = hashes.native;
self.make_qualified_function_call(
return self.make_qualified_function_call(
scope, global, state, lib, this_ptr, namespace, name, args, constants, hash, pos,
level,
)
} else {
// Normal function call
let (first_arg, args) = args.split_first().map_or_else(
|| (None, args.as_ref()),
|(first, rest)| (Some(first), rest),
);
self.make_function_call(
scope, global, state, lib, this_ptr, name, first_arg, args, constants, *hashes,
pos, *capture, level,
)
}
// Normal function call
let (first_arg, args) = args.split_first().map_or_else(
|| (None, args.as_ref()),
|(first, rest)| (Some(first), rest),
);
self.make_function_call(
scope, global, state, lib, this_ptr, name, first_arg, args, constants, *hashes, pos,
*capture, level,
)
}
/// Evaluate an expression.

View File

@@ -1,30 +1,30 @@
//! Global runtime state.
use crate::func::{CallableFunction, IteratorFn};
use crate::{Engine, Identifier, Module, Shared, StaticVec};
use crate::{Engine, Identifier};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
any::TypeId,
fmt,
iter::{Rev, Zip},
marker::PhantomData,
};
use std::{fmt, marker::PhantomData};
/// _(internals)_ A stack of imported [modules][Module] plus mutable global runtime states.
/// _(internals)_ Global runtime states.
/// Exported under the `internals` feature only.
//
// # Implementation Notes
//
// This implementation splits the module names from the shared modules to improve data locality.
// Most usage will be looking up a particular key from the list and then getting the module that
// corresponds to that key.
// This implementation for imported [modules][crate::Module] splits the module names from the shared
// modules to improve data locality. Most usage will be looking up a particular key from the list
// and then getting the module that corresponds to that key.
#[derive(Clone)]
pub struct GlobalRuntimeState<'a> {
/// Stack of module names.
keys: StaticVec<Identifier>,
/// Stack of imported [modules][Module].
modules: StaticVec<Shared<Module>>,
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
keys: crate::StaticVec<Identifier>,
/// Stack of imported [modules][crate::Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec<crate::Shared<crate::Module>>,
/// Source of the current context.
/// No source if the string is empty.
pub source: Identifier,
@@ -35,9 +35,10 @@ pub struct GlobalRuntimeState<'a> {
/// Function call hashes to index getters and setters.
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
fn_hash_indexing: (u64, u64),
/// Embedded [module][Module] resolver.
/// Embedded [crate::Module][crate::Module] resolver.
#[cfg(not(feature = "no_module"))]
pub embedded_module_resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>,
pub embedded_module_resolver:
Option<crate::Shared<crate::module::resolvers::StaticModuleResolver>>,
/// Cache of globally-defined constants.
///
/// Interior mutability is needed because it is shared in order to aid in cloning.
@@ -61,8 +62,10 @@ impl GlobalRuntimeState<'_> {
let _engine = engine;
Self {
keys: StaticVec::new_const(),
modules: StaticVec::new_const(),
#[cfg(not(feature = "no_module"))]
keys: crate::StaticVec::new_const(),
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec::new_const(),
source: Identifier::new_const(),
num_operations: 0,
num_modules_loaded: 0,
@@ -78,26 +81,41 @@ impl GlobalRuntimeState<'_> {
dummy: PhantomData::default(),
}
}
/// Get the length of the stack of globally-imported [modules][Module].
/// Get the length of the stack of globally-imported [modules][crate::Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[must_use]
pub fn num_imports(&self) -> usize {
self.keys.len()
}
/// Get the globally-imported [module][Module] at a particular index.
/// Get the globally-imported [crate::Module][crate::Module] at a particular index.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[must_use]
pub fn get_shared_import(&self, index: usize) -> Option<Shared<Module>> {
pub fn get_shared_import(&self, index: usize) -> Option<crate::Shared<crate::Module>> {
self.modules.get(index).cloned()
}
/// Get a mutable reference to the globally-imported [module][Module] at a particular index.
/// Get a mutable reference to the globally-imported [crate::Module][crate::Module] at a particular index.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline(always)]
#[must_use]
pub(crate) fn get_shared_import_mut(&mut self, index: usize) -> Option<&mut Shared<Module>> {
pub(crate) fn get_shared_import_mut(
&mut self,
index: usize,
) -> Option<&mut crate::Shared<crate::Module>> {
self.modules.get_mut(index)
}
/// Get the index of a globally-imported [module][Module] by name.
/// Get the index of a globally-imported [crate::Module][crate::Module] by name.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline]
#[must_use]
pub fn find_import(&self, name: &str) -> Option<usize> {
@@ -111,69 +129,107 @@ impl GlobalRuntimeState<'_> {
}
})
}
/// Push an imported [module][Module] onto the stack.
/// Push an imported [crate::Module][crate::Module] onto the stack.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn push_import(&mut self, name: impl Into<Identifier>, module: impl Into<Shared<Module>>) {
pub fn push_import(
&mut self,
name: impl Into<Identifier>,
module: impl Into<crate::Shared<crate::Module>>,
) {
self.keys.push(name.into());
self.modules.push(module.into());
}
/// Truncate the stack of globally-imported [modules][Module] to a particular length.
/// Truncate the stack of globally-imported [modules][crate::Module] to a particular length.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn truncate_imports(&mut self, size: usize) {
self.keys.truncate(size);
self.modules.truncate(size);
}
/// Get an iterator to the stack of globally-imported [modules][Module] in reverse order.
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> {
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> {
self.keys
.iter()
.rev()
.zip(self.modules.iter().rev())
.map(|(name, module)| (name.as_str(), module.as_ref()))
}
/// Get an iterator to the stack of globally-imported [modules][Module] in reverse order.
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
pub(crate) fn iter_imports_raw(&self) -> impl Iterator<Item = (&Identifier, &Shared<Module>)> {
pub(crate) fn iter_imports_raw(
&self,
) -> impl Iterator<Item = (&Identifier, &crate::Shared<crate::Module>)> {
self.keys.iter().rev().zip(self.modules.iter().rev())
}
/// Get an iterator to the stack of globally-imported [modules][Module] in forward order.
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
pub fn scan_imports_raw(&self) -> impl Iterator<Item = (&Identifier, &Shared<Module>)> {
pub fn scan_imports_raw(
&self,
) -> impl Iterator<Item = (&Identifier, &crate::Shared<crate::Module>)> {
self.keys.iter().zip(self.modules.iter())
}
/// Does the specified function hash key exist in the stack of globally-imported [modules][Module]?
/// Does the specified function hash key exist in the stack of globally-imported [modules][crate::Module]?
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
#[must_use]
pub fn contains_qualified_fn(&self, hash: u64) -> bool {
self.modules.iter().any(|m| m.contains_qualified_fn(hash))
}
/// Get the specified function via its hash key from the stack of globally-imported [modules][Module].
/// Get the specified function via its hash key from the stack of globally-imported [modules][crate::Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline]
#[must_use]
pub fn get_qualified_fn(&self, hash: u64) -> Option<(&CallableFunction, Option<&str>)> {
pub fn get_qualified_fn(
&self,
hash: u64,
) -> Option<(&crate::func::CallableFunction, Option<&str>)> {
self.modules
.iter()
.rev()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id())))
}
/// Does the specified [`TypeId`][std::any::TypeId] iterator exist in the stack of
/// globally-imported [modules][Module]?
/// globally-imported [modules][crate::Module]?
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
#[must_use]
pub fn contains_iter(&self, id: TypeId) -> bool {
pub fn contains_iter(&self, id: std::any::TypeId) -> bool {
self.modules.iter().any(|m| m.contains_qualified_iter(id))
}
/// Get the specified [`TypeId`][std::any::TypeId] iterator from the stack of globally-imported
/// [modules][Module].
/// [modules][crate::Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline]
#[must_use]
pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> {
pub fn get_iter(&self, id: std::any::TypeId) -> Option<&crate::func::IteratorFn> {
self.modules
.iter()
.rev()
@@ -216,10 +272,13 @@ impl GlobalRuntimeState<'_> {
}
}
#[cfg(not(feature = "no_module"))]
impl IntoIterator for GlobalRuntimeState<'_> {
type Item = (Identifier, Shared<Module>);
type IntoIter =
Zip<Rev<smallvec::IntoIter<[Identifier; 3]>>, Rev<smallvec::IntoIter<[Shared<Module>; 3]>>>;
type Item = (Identifier, crate::Shared<crate::Module>);
type IntoIter = std::iter::Zip<
std::iter::Rev<smallvec::IntoIter<[Identifier; 3]>>,
std::iter::Rev<smallvec::IntoIter<[crate::Shared<crate::Module>; 3]>>,
>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
@@ -230,7 +289,10 @@ impl IntoIterator for GlobalRuntimeState<'_> {
}
}
impl<K: Into<Identifier>, M: Into<Shared<Module>>> Extend<(K, M)> for GlobalRuntimeState<'_> {
#[cfg(not(feature = "no_module"))]
impl<K: Into<Identifier>, M: Into<crate::Shared<crate::Module>>> Extend<(K, M)>
for GlobalRuntimeState<'_>
{
#[inline]
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
for (k, m) in iter {
@@ -245,8 +307,10 @@ impl fmt::Debug for GlobalRuntimeState<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("GlobalRuntimeState");
f.field("imports", &self.keys.iter().zip(self.modules.iter()))
.field("source", &self.source)
#[cfg(not(feature = "no_module"))]
f.field("imports", &self.keys.iter().zip(self.modules.iter()));
f.field("source", &self.source)
.field("num_operations", &self.num_operations)
.field("num_modules_loaded", &self.num_modules_loaded);

View File

@@ -37,6 +37,7 @@ impl Engine {
let orig_always_search_scope = state.always_search_scope;
let orig_scope_len = scope.len();
#[cfg(not(feature = "no_module"))]
let orig_imports_len = global.num_imports();
let orig_fn_resolution_caches_len = state.fn_resolution_caches_len();
@@ -96,6 +97,7 @@ impl Engine {
if restore_orig_state {
scope.rewind(orig_scope_len);
state.scope_level -= 1;
#[cfg(not(feature = "no_module"))]
global.truncate_imports(orig_imports_len);
// The impact of new local variables goes away at the end of a block
@@ -579,11 +581,20 @@ impl Engine {
.global_modules
.iter()
.find_map(|m| m.get_iter(iter_type))
.or_else(|| global.get_iter(iter_type))
.or_else(|| {
self.global_sub_modules
#[cfg(not(feature = "no_module"))]
return global.get_iter(iter_type);
#[cfg(feature = "no_module")]
return None;
})
.or_else(|| {
#[cfg(not(feature = "no_module"))]
return self
.global_sub_modules
.values()
.find_map(|m| m.get_qualified_iter(iter_type))
.find_map(|m| m.get_qualified_iter(iter_type));
#[cfg(feature = "no_module")]
return None;
});
if let Some(func) = func {