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

View File

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

View File

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

View File

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

View File

@ -786,7 +786,7 @@ impl Engine {
} }
#[cfg(not(feature = "no_module"))] #[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}"))); 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>, pub(crate) global_modules: StaticVec<SharedModule>,
/// A collection of all sub-modules directly loaded into the Engine. /// A collection of all sub-modules directly loaded into the Engine.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub(crate) global_sub_modules: pub(crate) global_sub_modules: Option<std::collections::BTreeMap<Identifier, SharedModule>>,
Option<Box<std::collections::BTreeMap<Identifier, SharedModule>>>,
/// A module resolution service. /// A module resolution service.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
@ -107,15 +106,14 @@ pub struct Engine {
pub(crate) interned_strings: Option<Box<Locked<StringsInterner>>>, pub(crate) interned_strings: Option<Box<Locked<StringsInterner>>>,
/// A set of symbols to disable. /// 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. /// A map containing custom keywords and precedence to recognize.
#[cfg(not(feature = "no_custom_syntax"))] #[cfg(not(feature = "no_custom_syntax"))]
pub(crate) custom_keywords: pub(crate) custom_keywords: Option<std::collections::BTreeMap<Identifier, Option<Precedence>>>,
Option<Box<std::collections::BTreeMap<Identifier, Option<Precedence>>>>,
/// Custom syntax. /// Custom syntax.
#[cfg(not(feature = "no_custom_syntax"))] #[cfg(not(feature = "no_custom_syntax"))]
pub(crate) custom_syntax: Option< 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. /// Callback closure for filtering variable definition.
pub(crate) def_var_filter: Option<Box<OnDefVarCallback>>, 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(|| global.get_iter(iter_type))
.or_else(|| { .or_else(|| {
self.global_sub_modules self.global_sub_modules
.as_deref() .as_ref()
.into_iter() .into_iter()
.flatten() .flatten()
.find_map(|(_, m)| m.get_qualified_iter(iter_type)) .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(|| _global.get_qualified_fn(hash, true))
.or_else(|| { .or_else(|| {
self.global_sub_modules self.global_sub_modules
.as_deref() .as_ref()
.into_iter() .into_iter()
.flatten() .flatten()
.filter(|(_, m)| m.contains_indexed_global_functions()) .filter(|(_, m)| m.contains_indexed_global_functions())
@ -248,7 +248,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let is_dynamic = is_dynamic let is_dynamic = is_dynamic
|| _global.may_contain_dynamic_fn(hash_base) || _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)) m.values().any(|m| m.may_contain_dynamic_fn(hash_base))
}); });

View File

@ -219,7 +219,7 @@ impl Engine {
// Then check imported modules // Then check imported modules
global.contains_qualified_fn(hash_script) global.contains_qualified_fn(hash_script)
// Then check sub-modules // 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)) m.values().any(|m| m.contains_qualified_fn(hash_script))
}); });

View File

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

View File

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

View File

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

View File

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

View File

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