Skip to content

Commit

Permalink
Remove more redirections.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Sep 2, 2023
1 parent 6997364 commit 3276ff5
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 102 deletions.
5 changes: 3 additions & 2 deletions src/api/call_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use crate::eval::{Caches, GlobalRuntimeState};
use crate::types::dynamic::Variant;
use crate::{
Dynamic, Engine, FuncArgs, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, AST, ERR,
Dynamic, Engine, FnArgsVec, FuncArgs, Position, RhaiResult, RhaiResultOf, Scope, StaticVec,
AST, ERR,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
Expand Down Expand Up @@ -238,7 +239,7 @@ impl Engine {
};

let result = global_result.and_then(|_| {
let args = &mut arg_values.iter_mut().collect::<StaticVec<_>>();
let args = &mut arg_values.iter_mut().collect::<FnArgsVec<_>>();

// Check for data race.
#[cfg(not(feature = "no_closure"))]
Expand Down
10 changes: 6 additions & 4 deletions src/api/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,21 @@ impl Engine {
let param_types = F::param_types();

#[cfg(feature = "metadata")]
let mut param_type_names: crate::StaticVec<_> = F::param_names()
let mut param_type_names = F::param_names()
.iter()
.map(|ty| format!("_: {}", self.format_type_name(ty)))
.collect();
.collect::<crate::FnArgsVec<_>>();

#[cfg(feature = "metadata")]
if F::return_type() != TypeId::of::<()>() {
param_type_names.push(self.format_type_name(F::return_type_name()).into());
}

#[cfg(feature = "metadata")]
let param_type_names: crate::StaticVec<_> =
param_type_names.iter().map(String::as_str).collect();
let param_type_names = param_type_names
.iter()
.map(String::as_str)
.collect::<crate::FnArgsVec<_>>();
#[cfg(feature = "metadata")]
let param_type_names = Some(param_type_names.as_ref());

Expand Down
6 changes: 3 additions & 3 deletions src/eval/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub struct CallStackFrame {
/// Function name.
pub fn_name: ImmutableString,
/// Copies of function call arguments, if any.
pub args: crate::StaticVec<Dynamic>,
pub args: Vec<Dynamic>,
/// Source of the function.
pub source: Option<ImmutableString>,
/// [Position][`Position`] of the function call.
Expand Down Expand Up @@ -285,13 +285,13 @@ impl Debugger {
pub(crate) fn push_call_stack_frame(
&mut self,
fn_name: ImmutableString,
args: crate::StaticVec<Dynamic>,
args: impl IntoIterator<Item = Dynamic>,
source: Option<ImmutableString>,
pos: Position,
) {
self.call_stack.push(CallStackFrame {
fn_name,
args,
args: args.into_iter().collect(),
source,
pos,
});
Expand Down
106 changes: 36 additions & 70 deletions src/eval/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub type SharedGlobalConstants =
pub struct GlobalRuntimeState {
/// Names of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))]
imports: Option<Vec<ImmutableString>>,
imports: Vec<ImmutableString>,
/// Stack of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))]
modules: Option<Vec<crate::SharedModule>>,
modules: Vec<crate::SharedModule>,

/// The current stack of loaded [modules][crate::Module] containing script-defined functions.
#[cfg(not(feature = "no_function"))]
Expand Down Expand Up @@ -82,9 +82,9 @@ impl GlobalRuntimeState {
pub fn new(engine: &Engine) -> Self {
Self {
#[cfg(not(feature = "no_module"))]
imports: None,
imports: Vec::new(),
#[cfg(not(feature = "no_module"))]
modules: None,
modules: Vec::new(),
#[cfg(not(feature = "no_function"))]
lib: Vec::new(),
source: None,
Expand Down Expand Up @@ -113,10 +113,10 @@ impl GlobalRuntimeState {
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline]
#[inline(always)]
#[must_use]
pub fn num_imports(&self) -> usize {
self.modules.as_ref().map_or(0, Vec::len)
self.modules.len()
}
/// Get the globally-imported [module][crate::Module] at a particular index.
///
Expand All @@ -125,7 +125,7 @@ impl GlobalRuntimeState {
#[inline]
#[must_use]
pub fn get_shared_import(&self, index: usize) -> Option<crate::SharedModule> {
self.modules.as_ref().and_then(|m| m.get(index).cloned())
self.modules.get(index).cloned()
}
/// Get the index of a globally-imported [module][crate::Module] by name.
///
Expand All @@ -134,13 +134,7 @@ impl GlobalRuntimeState {
#[inline]
#[must_use]
pub fn find_import(&self, name: &str) -> Option<usize> {
self.imports.as_ref().and_then(|imports| {
imports
.iter()
.rev()
.position(|key| key.as_str() == name)
.map(|i| imports.len() - 1 - i)
})
self.imports.iter().rposition(|key| key.as_str() == name)
}
/// Push an imported [module][crate::Module] onto the stack.
///
Expand All @@ -152,27 +146,18 @@ impl GlobalRuntimeState {
name: impl Into<ImmutableString>,
module: impl Into<crate::SharedModule>,
) {
self.imports
.get_or_insert_with(|| Vec::new().into())
.push(name.into());
self.imports.push(name.into());

self.modules
.get_or_insert_with(|| Vec::new().into())
.push(module.into());
self.modules.push(module.into());
}
/// Truncate the stack of globally-imported [modules][crate::Module] to a particular length.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline]
#[inline(always)]
pub fn truncate_imports(&mut self, size: usize) {
if size == 0 {
self.imports = None;
self.modules = None;
} else if self.imports.is_some() {
self.imports.as_mut().unwrap().truncate(size);
self.modules.as_mut().unwrap().truncate(size);
}
self.imports.truncate(size);
self.modules.truncate(size);
}
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
///
Expand All @@ -181,11 +166,9 @@ impl GlobalRuntimeState {
#[inline]
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> {
self.imports
.as_deref()
.into_iter()
.flatten()
.iter()
.rev()
.zip(self.modules.as_deref().into_iter().flatten().rev())
.zip(self.modules.iter().rev())
.map(|(name, module)| (name.as_str(), &**module))
}
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
Expand All @@ -196,12 +179,7 @@ impl GlobalRuntimeState {
pub(crate) fn iter_imports_raw(
&self,
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
self.imports
.as_deref()
.into_iter()
.flatten()
.rev()
.zip(self.modules.as_deref().into_iter().flatten())
self.imports.iter().rev().zip(self.modules.iter().rev())
}
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
///
Expand All @@ -211,11 +189,7 @@ impl GlobalRuntimeState {
pub fn scan_imports_raw(
&self,
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
self.imports
.as_deref()
.into_iter()
.flatten()
.zip(self.modules.as_deref().into_iter().flatten())
self.imports.iter().zip(self.modules.iter())
}
/// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of
/// globally-imported [modules][crate::Module]?
Expand All @@ -224,9 +198,9 @@ impl GlobalRuntimeState {
#[cfg(not(feature = "no_module"))]
#[inline]
pub(crate) fn may_contain_dynamic_fn(&self, hash_script: u64) -> bool {
self.modules.as_deref().map_or(false, |m| {
m.iter().any(|m| m.may_contain_dynamic_fn(hash_script))
})
self.modules
.iter()
.any(|m| m.may_contain_dynamic_fn(hash_script))
}
/// Does the specified function hash key exist in the stack of globally-imported
/// [modules][crate::Module]?
Expand All @@ -237,9 +211,7 @@ impl GlobalRuntimeState {
#[inline]
#[must_use]
pub fn contains_qualified_fn(&self, hash: u64) -> bool {
self.modules
.as_ref()
.map_or(false, |m| m.iter().any(|m| m.contains_qualified_fn(hash)))
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][crate::Module].
Expand All @@ -254,18 +226,16 @@ impl GlobalRuntimeState {
global_namespace_only: bool,
) -> Option<(&crate::func::CallableFunction, Option<&ImmutableString>)> {
if global_namespace_only {
self.modules.as_ref().and_then(|m| {
m.iter()
.rev()
.filter(|m| m.contains_indexed_global_functions())
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id_raw())))
})
self.modules
.iter()
.rev()
.filter(|&m| m.contains_indexed_global_functions())
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id_raw())))
} else {
self.modules.as_ref().and_then(|m| {
m.iter()
.rev()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id_raw())))
})
self.modules
.iter()
.rev()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id_raw())))
}
}
/// Does the specified [`TypeId`][std::any::TypeId] iterator exist in the stack of
Expand All @@ -277,9 +247,7 @@ impl GlobalRuntimeState {
#[inline]
#[must_use]
pub fn contains_iter(&self, id: std::any::TypeId) -> bool {
self.modules
.as_ref()
.map_or(false, |m| m.iter().any(|m| m.contains_qualified_iter(id)))
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][crate::Module].
Expand All @@ -290,8 +258,9 @@ impl GlobalRuntimeState {
#[must_use]
pub fn get_iter(&self, id: std::any::TypeId) -> Option<&crate::func::IteratorFn> {
self.modules
.as_ref()
.and_then(|m| m.iter().rev().find_map(|m| m.get_qualified_iter(id)))
.iter()
.rev()
.find_map(|m| m.get_qualified_iter(id))
}
/// Get the current source.
#[inline(always)]
Expand Down Expand Up @@ -333,12 +302,9 @@ impl GlobalRuntimeState {
impl<K: Into<ImmutableString>, M: Into<crate::SharedModule>> Extend<(K, M)> for GlobalRuntimeState {
#[inline]
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
let imports = self.imports.get_or_insert_with(|| Vec::new().into());
let modules = self.modules.get_or_insert_with(|| Vec::new().into());

for (k, m) in iter {
imports.push(k.into());
modules.push(m.into());
self.imports.push(k.into());
self.modules.push(m.into());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/func/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl Engine {

global.debugger_mut().push_call_stack_frame(
self.get_interned_string(name),
args.iter().map(|v| (*v).clone()).collect(),
args.iter().map(|v| (*v).clone()),
source,
pos,
);
Expand Down
2 changes: 1 addition & 1 deletion src/func/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Engine {
#[cfg(feature = "debugging")]
if self.is_debugger_registered() {
let fn_name = fn_def.name.clone();
let args = scope.iter().skip(orig_scope_len).map(|(.., v)| v).collect();
let args = scope.iter().skip(orig_scope_len).map(|(.., v)| v);
let source = global.source.clone();

global
Expand Down
14 changes: 7 additions & 7 deletions src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ impl Module {
#[inline]
pub fn set_fn(
&mut self,
name: impl AsRef<str>,
name: impl Into<Identifier>,
namespace: FnNamespace,
access: FnAccess,
arg_names: Option<&[&str]>,
Expand Down Expand Up @@ -1110,8 +1110,8 @@ impl Module {
(names, return_type)
};

let name = name.as_ref();
let hash_base = calc_fn_hash(None, name, param_types.len());
let name = name.into();
let hash_base = calc_fn_hash(None, &name, param_types.len());
let hash_fn = calc_fn_hash_full(hash_base, param_types.iter().copied());

// Catch hash collisions in testing environment only.
Expand All @@ -1134,7 +1134,7 @@ impl Module {
FuncInfo {
func,
metadata: FuncInfoMetadata {
name: name.into(),
name,
namespace,
access,
#[cfg(not(feature = "no_object"))]
Expand Down Expand Up @@ -1190,7 +1190,7 @@ impl Module {
#[inline]
pub fn set_fn_with_comments<S: AsRef<str>>(
&mut self,
name: impl AsRef<str>,
name: impl Into<Identifier>,
namespace: FnNamespace,
access: FnAccess,
arg_names: Option<&[&str]>,
Expand Down Expand Up @@ -1274,7 +1274,7 @@ impl Module {
#[inline(always)]
pub fn set_raw_fn<T: Variant + Clone>(
&mut self,
name: impl AsRef<str>,
name: impl Into<Identifier>,
namespace: FnNamespace,
access: FnAccess,
arg_types: impl AsRef<[TypeId]>,
Expand Down Expand Up @@ -1323,7 +1323,7 @@ impl Module {
#[inline(always)]
pub fn set_native_fn<A: 'static, const N: usize, const C: bool, T, F>(
&mut self,
name: impl AsRef<str> + Into<Identifier>,
name: impl Into<Identifier>,
func: F,
) -> u64
where
Expand Down
Loading

0 comments on commit 3276ff5

Please sign in to comment.