Remove indirection.

This commit is contained in:
Stephen Chung 2023-03-23 13:37:10 +08:00
parent 3d06ddc6e2
commit 2e724b804e
15 changed files with 56 additions and 58 deletions

View File

@ -261,12 +261,12 @@ impl Engine {
// Make it a custom keyword/symbol if it is disabled or reserved
if (self
.disabled_symbols
.as_deref()
.as_ref()
.map_or(false, |m| m.contains(s))
|| token.as_ref().map_or(false, Token::is_reserved))
&& !self
.custom_keywords
.as_deref()
.as_ref()
.map_or(false, |m| m.contains_key(s))
{
self.custom_keywords
@ -281,7 +281,7 @@ impl Engine {
&& token.as_ref().map_or(false, Token::is_standard_keyword)
&& !self
.disabled_symbols
.as_deref()
.as_ref()
.map_or(false, |m| m.contains(s)) =>
{
return Err(LexError::ImproperSymbol(
@ -301,12 +301,12 @@ impl Engine {
// Make it a custom keyword/symbol if it is disabled or reserved
if self
.disabled_symbols
.as_deref()
.as_ref()
.map_or(false, |m| m.contains(s))
|| (token.as_ref().map_or(false, Token::is_reserved)
&& !self
.custom_keywords
.as_deref()
.as_ref()
.map_or(false, |m| m.contains_key(s)))
{
self.custom_keywords

View File

@ -372,7 +372,7 @@ impl Definitions<'_> {
let mut m = self
.engine
.global_sub_modules
.as_deref()
.as_ref()
.into_iter()
.flatten()
.map(move |(name, module)| {
@ -461,7 +461,7 @@ impl Module {
|| def
.engine
.custom_keywords
.as_deref()
.as_ref()
.map_or(false, |m| m.contains_key(f.metadata.name.as_str()));
f.write_definition(writer, def, operator)?;

View File

@ -208,7 +208,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
return self
.global_sub_modules
.as_deref()
.as_ref()
.into_iter()
.flatten()
.find_map(|(_, m)| m.get_custom_type(name));
@ -243,7 +243,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
return self
.global_sub_modules
.as_deref()
.as_ref()
.into_iter()
.flatten()
.find_map(|(_, m)| m.get_custom_type(name));

View File

@ -171,7 +171,7 @@ impl Engine {
Some(token) if token.is_standard_keyword() => {
if !self
.disabled_symbols
.as_deref()
.as_ref()
.map_or(false, |m| m.contains(token.literal_syntax()))
{
return Err(format!("'{keyword}' is a reserved keyword"));
@ -181,7 +181,7 @@ impl Engine {
Some(token) if token.is_standard_symbol() => {
if !self
.disabled_symbols
.as_deref()
.as_ref()
.map_or(false, |m| m.contains(token.literal_syntax()))
{
return Err(format!("'{keyword}' is a reserved operator"));
@ -191,7 +191,7 @@ impl Engine {
Some(token)
if !self
.disabled_symbols
.as_deref()
.as_ref()
.map_or(false, |m| m.contains(token.literal_syntax())) =>
{
return Err(format!("'{keyword}' is a reserved symbol"))

View File

@ -786,7 +786,7 @@ impl Engine {
}
#[cfg(not(feature = "no_module"))]
for (name, m) in self.global_sub_modules.as_deref().into_iter().flatten() {
for (name, m) in self.global_sub_modules.as_ref().into_iter().flatten() {
signatures.extend(m.gen_fn_signatures().map(|f| format!("{name}::{f}")));
}

View File

@ -96,8 +96,7 @@ pub struct Engine {
pub(crate) global_modules: StaticVec<SharedModule>,
/// A collection of all sub-modules directly loaded into the Engine.
#[cfg(not(feature = "no_module"))]
pub(crate) global_sub_modules:
Option<Box<std::collections::BTreeMap<Identifier, SharedModule>>>,
pub(crate) global_sub_modules: Option<std::collections::BTreeMap<Identifier, SharedModule>>,
/// A module resolution service.
#[cfg(not(feature = "no_module"))]
@ -107,15 +106,14 @@ pub struct Engine {
pub(crate) interned_strings: Option<Box<Locked<StringsInterner>>>,
/// A set of symbols to disable.
pub(crate) disabled_symbols: Option<Box<BTreeSet<Identifier>>>,
pub(crate) disabled_symbols: Option<BTreeSet<Identifier>>,
/// A map containing custom keywords and precedence to recognize.
#[cfg(not(feature = "no_custom_syntax"))]
pub(crate) custom_keywords:
Option<Box<std::collections::BTreeMap<Identifier, Option<Precedence>>>>,
pub(crate) custom_keywords: Option<std::collections::BTreeMap<Identifier, Option<Precedence>>>,
/// Custom syntax.
#[cfg(not(feature = "no_custom_syntax"))]
pub(crate) custom_syntax: Option<
Box<std::collections::BTreeMap<Identifier, Box<crate::api::custom_syntax::CustomSyntax>>>,
std::collections::BTreeMap<Identifier, Box<crate::api::custom_syntax::CustomSyntax>>,
>,
/// Callback closure for filtering variable definition.
pub(crate) def_var_filter: Option<Box<OnDefVarCallback>>,

View File

@ -664,7 +664,7 @@ impl Engine {
.or_else(|| global.get_iter(iter_type))
.or_else(|| {
self.global_sub_modules
.as_deref()
.as_ref()
.into_iter()
.flatten()
.find_map(|(_, m)| m.get_qualified_iter(iter_type))

View File

@ -206,7 +206,7 @@ impl Engine {
.or_else(|| _global.get_qualified_fn(hash, true))
.or_else(|| {
self.global_sub_modules
.as_deref()
.as_ref()
.into_iter()
.flatten()
.filter(|(_, m)| m.contains_indexed_global_functions())
@ -248,7 +248,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
let is_dynamic = is_dynamic
|| _global.may_contain_dynamic_fn(hash_base)
|| self.global_sub_modules.as_deref().map_or(false, |m| {
|| self.global_sub_modules.as_ref().map_or(false, |m| {
m.values().any(|m| m.may_contain_dynamic_fn(hash_base))
});

View File

@ -219,7 +219,7 @@ impl Engine {
// Then check imported modules
global.contains_qualified_fn(hash_script)
// Then check sub-modules
|| self.global_sub_modules.as_deref().map_or(false, |m| {
|| self.global_sub_modules.as_ref().map_or(false, |m| {
m.values().any(|m| m.contains_qualified_fn(hash_script))
});

View File

@ -195,22 +195,22 @@ pub struct Module {
/// Custom types.
custom_types: Option<Box<CustomTypesCollection>>,
/// Sub-modules.
modules: Option<Box<BTreeMap<Identifier, SharedModule>>>,
modules: Option<BTreeMap<Identifier, SharedModule>>,
/// [`Module`] variables.
variables: Option<Box<BTreeMap<Identifier, Dynamic>>>,
variables: Option<BTreeMap<Identifier, Dynamic>>,
/// Flattened collection of all [`Module`] variables, including those in sub-modules.
all_variables: Option<Box<StraightHashMap<Dynamic>>>,
all_variables: Option<StraightHashMap<Dynamic>>,
/// Functions (both native Rust and scripted).
functions: Option<StraightHashMap<FuncInfo>>,
/// Flattened collection of all functions, native Rust and scripted.
/// including those in sub-modules.
all_functions: Option<Box<StraightHashMap<CallableFunction>>>,
all_functions: Option<StraightHashMap<CallableFunction>>,
/// Bloom filter on native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters.
dynamic_functions_filter: Option<Box<BloomFilterU64>>,
/// Iterator functions, keyed by the type producing the iterator.
type_iterators: Option<Box<BTreeMap<TypeId, Shared<IteratorFn>>>>,
type_iterators: Option<BTreeMap<TypeId, Shared<IteratorFn>>>,
/// Flattened collection of iterator functions, including those in sub-modules.
all_type_iterators: Option<Box<BTreeMap<TypeId, Shared<IteratorFn>>>>,
all_type_iterators: Option<BTreeMap<TypeId, Shared<IteratorFn>>>,
/// Flags.
pub(crate) flags: ModuleFlags,
}
@ -234,7 +234,7 @@ impl fmt::Debug for Module {
"modules",
&self
.modules
.as_deref()
.as_ref()
.into_iter()
.flat_map(BTreeMap::keys)
.map(SmartString::as_str)
@ -561,23 +561,23 @@ impl Module {
.functions
.as_ref()
.map_or(true, StraightHashMap::is_empty)
&& self.variables.as_deref().map_or(true, BTreeMap::is_empty)
&& self.modules.as_deref().map_or(true, BTreeMap::is_empty)
&& self.variables.as_ref().map_or(true, BTreeMap::is_empty)
&& self.modules.as_ref().map_or(true, BTreeMap::is_empty)
&& self
.type_iterators
.as_deref()
.as_ref()
.map_or(true, BTreeMap::is_empty)
&& self
.all_functions
.as_deref()
.as_ref()
.map_or(true, StraightHashMap::is_empty)
&& self
.all_variables
.as_deref()
.as_ref()
.map_or(true, StraightHashMap::is_empty)
&& self
.all_type_iterators
.as_deref()
.as_ref()
.map_or(true, BTreeMap::is_empty)
}
@ -1979,9 +1979,9 @@ impl Module {
#[must_use]
pub fn count(&self) -> (usize, usize, usize) {
(
self.variables.as_deref().map_or(0, BTreeMap::len),
self.variables.as_ref().map_or(0, BTreeMap::len),
self.functions.as_ref().map_or(0, StraightHashMap::len),
self.type_iterators.as_deref().map_or(0, BTreeMap::len),
self.type_iterators.as_ref().map_or(0, BTreeMap::len),
)
}
@ -1989,7 +1989,7 @@ impl Module {
#[inline]
pub fn iter_sub_modules(&self) -> impl Iterator<Item = (&str, &SharedModule)> {
self.modules
.as_deref()
.as_ref()
.into_iter()
.flatten()
.map(|(k, m)| (k.as_str(), m))
@ -1999,7 +1999,7 @@ impl Module {
#[inline]
pub fn iter_var(&self) -> impl Iterator<Item = (&str, &Dynamic)> {
self.variables
.as_deref()
.as_ref()
.into_iter()
.flatten()
.map(|(k, v)| (k.as_str(), v))
@ -2392,7 +2392,7 @@ impl Module {
if !self.is_indexed() {
let mut path = Vec::with_capacity(4);
let mut variables = new_hash_map(self.variables.as_deref().map_or(0, BTreeMap::len));
let mut variables = new_hash_map(self.variables.as_ref().map_or(0, BTreeMap::len));
let mut functions =
new_hash_map(self.functions.as_ref().map_or(0, StraightHashMap::len));
let mut type_iterators = BTreeMap::new();

View File

@ -1262,7 +1262,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
if self
.global_sub_modules
.as_deref()
.as_ref()
.into_iter()
.flatten()
.any(|(_, m)| m.contains_qualified_fn(hash))

View File

@ -273,7 +273,7 @@ fn collect_fn_metadata(
#[cfg(not(feature = "no_module"))]
ctx.engine()
.global_sub_modules
.as_deref()
.as_ref()
.into_iter()
.flatten()
.flat_map(|(_, m)| m.iter_script_fn())

View File

@ -601,7 +601,7 @@ impl Engine {
.any(|m| m.as_str() == root)
&& !self
.global_sub_modules
.as_deref()
.as_ref()
.map_or(false, |m| m.contains_key(root))
{
return Err(
@ -676,7 +676,7 @@ impl Engine {
.any(|m| m.as_str() == root)
&& !self
.global_sub_modules
.as_deref()
.as_ref()
.map_or(false, |m| m.contains_key(root))
{
return Err(
@ -1577,12 +1577,12 @@ impl Engine {
Token::Custom(key) | Token::Reserved(key) | Token::Identifier(key)
if self
.custom_syntax
.as_deref()
.as_ref()
.map_or(false, |m| m.contains_key(&**key)) =>
{
let (key, syntax) = self
.custom_syntax
.as_deref()
.as_ref()
.and_then(|m| m.get_key_value(&**key))
.unwrap();
let (.., pos) = input.next().expect(NEVER_ENDS);
@ -1888,7 +1888,7 @@ impl Engine {
.any(|m| m.as_str() == root)
&& !self
.global_sub_modules
.as_deref()
.as_ref()
.map_or(false, |m| m.contains_key(root))
{
return Err(
@ -2303,7 +2303,7 @@ impl Engine {
#[cfg(not(feature = "no_custom_syntax"))]
Token::Custom(c) => self
.custom_keywords
.as_deref()
.as_ref()
.and_then(|m| m.get(&**c))
.copied()
.ok_or_else(|| PERR::Reserved(c.to_string()).into_err(*current_pos))?,
@ -2329,7 +2329,7 @@ impl Engine {
#[cfg(not(feature = "no_custom_syntax"))]
Token::Custom(c) => self
.custom_keywords
.as_deref()
.as_ref()
.and_then(|m| m.get(&**c))
.copied()
.ok_or_else(|| PERR::Reserved(c.to_string()).into_err(*next_pos))?,
@ -2434,7 +2434,7 @@ impl Engine {
Token::Custom(s)
if self
.custom_keywords
.as_deref()
.as_ref()
.and_then(|m| m.get(s.as_str()))
.map_or(false, Option::is_some) =>
{

View File

@ -182,7 +182,7 @@ pub fn gen_metadata_to_json(
let mut global = ModuleMetadata::new();
#[cfg(not(feature = "no_module"))]
for (name, m) in engine.global_sub_modules.as_deref().into_iter().flatten() {
for (name, m) in engine.global_sub_modules.as_ref().into_iter().flatten() {
global.modules.insert(name, m.as_ref().into());
}

View File

@ -2450,7 +2450,7 @@ impl<'a> Iterator for TokenIterator<'a> {
Some((Token::Reserved(s), pos)) => (match
(s.as_str(),
#[cfg(not(feature = "no_custom_syntax"))]
self.engine.custom_keywords.as_deref().map_or(false, |m| m.contains_key(&*s)),
self.engine.custom_keywords.as_ref().map_or(false, |m| m.contains_key(&*s)),
#[cfg(feature = "no_custom_syntax")]
false
)
@ -2487,7 +2487,7 @@ impl<'a> Iterator for TokenIterator<'a> {
#[cfg(feature = "no_custom_syntax")]
(.., true) => unreachable!("no custom operators"),
// Reserved keyword that is not custom and disabled.
(token, false) if self.engine.disabled_symbols.as_deref().map_or(false,|m| m.contains(token)) => {
(token, false) if self.engine.disabled_symbols.as_ref().map_or(false,|m| m.contains(token)) => {
let msg = format!("reserved {} '{token}' is disabled", if is_valid_identifier(token) { "keyword"} else {"symbol"});
Token::LexError(LERR::ImproperSymbol(s.to_string(), msg).into())
},
@ -2496,13 +2496,13 @@ impl<'a> Iterator for TokenIterator<'a> {
}, pos),
// Custom keyword
#[cfg(not(feature = "no_custom_syntax"))]
Some((Token::Identifier(s), pos)) if self.engine.custom_keywords.as_deref().map_or(false,|m| m.contains_key(&*s)) => {
Some((Token::Identifier(s), pos)) if self.engine.custom_keywords.as_ref().map_or(false,|m| m.contains_key(&*s)) => {
(Token::Custom(s), pos)
}
// Custom keyword/symbol - must be disabled
#[cfg(not(feature = "no_custom_syntax"))]
Some((token, pos)) if token.is_literal() && self.engine.custom_keywords.as_deref().map_or(false,|m| m.contains_key(token.literal_syntax())) => {
if self.engine.disabled_symbols.as_deref().map_or(false,|m| m.contains(token.literal_syntax())) {
Some((token, pos)) if token.is_literal() && self.engine.custom_keywords.as_ref().map_or(false,|m| m.contains_key(token.literal_syntax())) => {
if self.engine.disabled_symbols.as_ref().map_or(false,|m| m.contains(token.literal_syntax())) {
// Disabled standard keyword/symbol
(Token::Custom(Box::new(token.literal_syntax().into())), pos)
} else {
@ -2511,7 +2511,7 @@ impl<'a> Iterator for TokenIterator<'a> {
}
}
// Disabled symbol
Some((token, pos)) if token.is_literal() && self.engine.disabled_symbols.as_deref().map_or(false,|m| m.contains(token.literal_syntax())) => {
Some((token, pos)) if token.is_literal() && self.engine.disabled_symbols.as_ref().map_or(false,|m| m.contains(token.literal_syntax())) => {
(Token::Reserved(Box::new(token.literal_syntax().into())), pos)
}
// Normal symbol