Move level into GlobalRuntimeState.

This commit is contained in:
Stephen Chung
2022-11-08 21:28:20 +08:00
parent 5bae4d8a19
commit e93923b3b6
21 changed files with 409 additions and 493 deletions

View File

@@ -72,13 +72,11 @@ pub struct NativeCallContext<'a> {
/// Function source, if any.
source: Option<&'a str>,
/// The current [`GlobalRuntimeState`], if any.
global: Option<&'a GlobalRuntimeState>,
global: &'a GlobalRuntimeState,
/// The current stack of loaded [modules][Module].
lib: &'a [SharedModule],
/// [Position] of the function call.
pos: Position,
/// The current nesting level of function calls.
level: usize,
}
/// _(internals)_ Context of a native Rust function call.
@@ -96,8 +94,6 @@ pub struct NativeCallContextStore {
pub lib: StaticVec<SharedModule>,
/// [Position] of the function call.
pub pos: Position,
/// The current nesting level of function calls.
pub level: usize,
}
#[cfg(feature = "internals")]
@@ -118,7 +114,6 @@ impl<'a>
&'a GlobalRuntimeState,
&'a [SharedModule],
Position,
usize,
)> for NativeCallContext<'a>
{
#[inline(always)]
@@ -130,56 +125,20 @@ impl<'a>
&'a GlobalRuntimeState,
&'a [SharedModule],
Position,
usize,
),
) -> Self {
Self {
engine: value.0,
fn_name: value.1,
source: value.2,
global: Some(value.3),
global: value.3,
lib: value.4,
pos: value.5,
level: value.6,
}
}
}
impl<'a> From<(&'a Engine, &'a str, &'a [SharedModule])> for NativeCallContext<'a> {
#[inline(always)]
fn from(value: (&'a Engine, &'a str, &'a [SharedModule])) -> Self {
Self {
engine: value.0,
fn_name: value.1,
source: None,
global: None,
lib: value.2,
pos: Position::NONE,
level: 0,
}
}
}
impl<'a> NativeCallContext<'a> {
/// _(internals)_ Create a new [`NativeCallContext`].
/// Exported under the `internals` feature only.
#[deprecated(
since = "1.3.0",
note = "`NativeCallContext::new` will be moved under `internals`. Use `FnPtr::call` to call a function pointer directly."
)]
#[inline(always)]
#[must_use]
pub fn new(engine: &'a Engine, fn_name: &'a str, lib: &'a [SharedModule]) -> Self {
Self {
engine,
fn_name,
source: None,
global: None,
lib,
pos: Position::NONE,
level: 0,
}
}
/// _(internals)_ Create a new [`NativeCallContext`].
/// Exported under the `internals` feature only.
///
@@ -195,16 +154,14 @@ impl<'a> NativeCallContext<'a> {
global: &'a GlobalRuntimeState,
lib: &'a [SharedModule],
pos: Position,
level: usize,
) -> Self {
Self {
engine,
fn_name,
source,
global: Some(global),
global,
lib,
pos,
level,
}
}
@@ -218,10 +175,9 @@ impl<'a> NativeCallContext<'a> {
engine,
fn_name: &context.fn_name,
source: context.source.as_ref().map(String::as_str),
global: Some(&context.global),
global: &context.global,
lib: &context.lib,
pos: context.pos,
level: context.level,
}
}
/// _(internals)_ Store this [`NativeCallContext`] into a [`NativeCallContextClone`].
@@ -233,10 +189,9 @@ impl<'a> NativeCallContext<'a> {
NativeCallContextStore {
fn_name: self.fn_name.to_string(),
source: self.source.map(|s| s.to_string()),
global: self.global.unwrap().clone(),
global: self.global.clone(),
lib: self.lib.iter().cloned().collect(),
pos: self.pos,
level: self.level,
}
}
@@ -262,7 +217,7 @@ impl<'a> NativeCallContext<'a> {
#[inline(always)]
#[must_use]
pub const fn call_level(&self) -> usize {
self.level
self.global.level
}
/// The current source.
#[inline(always)]
@@ -274,7 +229,7 @@ impl<'a> NativeCallContext<'a> {
#[inline(always)]
#[must_use]
pub fn tag(&self) -> Option<&Dynamic> {
self.global.as_ref().map(|g| &g.tag)
Some(&self.global.tag)
}
/// Get an iterator over the current set of modules imported via `import` statements
/// in reverse order.
@@ -283,7 +238,7 @@ impl<'a> NativeCallContext<'a> {
#[cfg(not(feature = "no_module"))]
#[inline]
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> {
self.global.iter().flat_map(|&g| g.iter_imports())
self.global.iter_imports()
}
/// Get an iterator over the current set of modules imported via `import` statements in reverse order.
#[cfg(not(feature = "no_module"))]
@@ -292,7 +247,7 @@ impl<'a> NativeCallContext<'a> {
pub(crate) fn iter_imports_raw(
&self,
) -> impl Iterator<Item = (&crate::ImmutableString, &SharedModule)> {
self.global.iter().flat_map(|&g| g.iter_imports_raw())
self.global.iter_imports_raw()
}
/// _(internals)_ The current [`GlobalRuntimeState`], if any.
/// Exported under the `internals` feature only.
@@ -301,7 +256,7 @@ impl<'a> NativeCallContext<'a> {
#[cfg(feature = "internals")]
#[inline(always)]
#[must_use]
pub const fn global_runtime_state(&self) -> Option<&GlobalRuntimeState> {
pub const fn global_runtime_state(&self) -> &GlobalRuntimeState {
self.global
}
/// Get an iterator over the namespaces containing definitions of all script-defined functions
@@ -437,10 +392,7 @@ impl<'a> NativeCallContext<'a> {
is_method_call: bool,
args: &mut [&mut Dynamic],
) -> RhaiResult {
let global = &mut self
.global
.cloned()
.unwrap_or_else(|| GlobalRuntimeState::new(self.engine()));
let mut global = &mut self.global.clone();
let caches = &mut Caches::new();
let fn_name = fn_name.as_ref();
@@ -448,6 +400,8 @@ impl<'a> NativeCallContext<'a> {
let op_token = op_token.as_ref();
let args_len = args.len();
global.level += 1;
if native_only {
return self
.engine()
@@ -455,7 +409,6 @@ impl<'a> NativeCallContext<'a> {
global,
caches,
self.lib,
self.level + 1,
fn_name,
op_token,
calc_fn_hash(None, fn_name, args_len),
@@ -483,7 +436,6 @@ impl<'a> NativeCallContext<'a> {
global,
caches,
self.lib,
self.level + 1,
None,
fn_name,
op_token,