Use ImmutableString for source.

This commit is contained in:
Stephen Chung
2022-10-29 14:12:18 +08:00
parent d0998a44b9
commit 4100e6da64
14 changed files with 112 additions and 126 deletions

View File

@@ -2,7 +2,7 @@
use crate::func::{CallableFunction, StraightHashMap};
use crate::types::BloomFilterU64;
use crate::{Identifier, StaticVec};
use crate::{ImmutableString, StaticVec};
use std::marker::PhantomData;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -14,7 +14,7 @@ pub struct FnResolutionCacheEntry {
/// Function.
pub func: CallableFunction,
/// Optional source.
pub source: Option<Box<Identifier>>,
pub source: Option<ImmutableString>,
}
/// _(internals)_ A function resolution cache with a bloom filter.

View File

@@ -3,7 +3,10 @@
use super::{EvalContext, GlobalRuntimeState};
use crate::ast::{ASTNode, Expr, Stmt};
use crate::{Dynamic, Engine, EvalAltResult, Identifier, Module, Position, RhaiResultOf, Scope};
use crate::{
Dynamic, Engine, EvalAltResult, Identifier, ImmutableString, Module, Position, RhaiResultOf,
Scope,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{fmt, iter::repeat, mem};
@@ -226,8 +229,8 @@ pub struct CallStackFrame {
pub fn_name: Identifier,
/// Copies of function call arguments, if any.
pub args: crate::StaticVec<Dynamic>,
/// Source of the function, empty if none.
pub source: Identifier,
/// Source of the function.
pub source: Option<ImmutableString>,
/// [Position][`Position`] of the function call.
pub pos: Position,
}
@@ -243,8 +246,8 @@ impl fmt::Display for CallStackFrame {
fp.finish()?;
if !self.pos.is_none() {
if !self.source.is_empty() {
write!(f, ": {}", self.source)?;
if let Some(ref source) = self.source {
write!(f, ": {source}")?;
}
write!(f, " @ {:?}", self.pos)?;
}
@@ -295,13 +298,13 @@ impl Debugger {
&mut self,
fn_name: impl Into<Identifier>,
args: crate::StaticVec<Dynamic>,
source: impl Into<Identifier>,
source: Option<ImmutableString>,
pos: Position,
) {
self.call_stack.push(CallStackFrame {
fn_name: fn_name.into(),
args,
source: source.into(),
source,
pos,
});
}
@@ -487,7 +490,10 @@ impl Engine {
let event = match event {
Some(e) => e,
None => match global.debugger.is_break_point(&global.source, node) {
None => match global
.debugger
.is_break_point(global.source().unwrap_or(""), node)
{
Some(bp) => DebuggerEvent::BreakPoint(bp),
None => return Ok(None),
},
@@ -512,17 +518,12 @@ impl Engine {
event: DebuggerEvent,
level: usize,
) -> Result<Option<DebuggerStatus>, Box<crate::EvalAltResult>> {
let source = global.source.clone();
let source = if source.is_empty() {
None
} else {
Some(source.as_str())
};
let src = global.source_raw().cloned();
let src = src.as_ref().map(|s| s.as_str());
let context = crate::EvalContext::new(self, scope, global, None, lib, this_ptr, level);
if let Some((.., ref on_debugger)) = self.debugger {
let command = on_debugger(context, event, node, source, node.position())?;
let command = on_debugger(context, event, node, src, node.position())?;
match command {
DebuggerCommand::Continue => {

View File

@@ -58,11 +58,7 @@ impl<'a, 's, 'ps, 'g, 'pg, 'c, 'pc, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'pg, '
#[inline(always)]
#[must_use]
pub fn source(&self) -> Option<&str> {
if self.global.source.is_empty() {
None
} else {
Some(self.global.source.as_str())
}
self.global.source()
}
/// The current [`Scope`].
#[inline(always)]

View File

@@ -1,6 +1,6 @@
//! Global runtime state.
use crate::{Dynamic, Engine, Identifier};
use crate::{Dynamic, Engine, ImmutableString};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{fmt, marker::PhantomData};
@@ -9,7 +9,7 @@ use std::{fmt, marker::PhantomData};
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub type GlobalConstants =
crate::Shared<crate::Locked<std::collections::BTreeMap<crate::ImmutableString, Dynamic>>>;
crate::Shared<crate::Locked<std::collections::BTreeMap<ImmutableString, Dynamic>>>;
/// _(internals)_ Global runtime states.
/// Exported under the `internals` feature only.
@@ -25,14 +25,14 @@ pub type GlobalConstants =
pub struct GlobalRuntimeState<'a> {
/// Names of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))]
imports: crate::StaticVec<crate::ImmutableString>,
imports: crate::StaticVec<ImmutableString>,
/// Stack of imported [modules][crate::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,
pub source: Option<ImmutableString>,
/// Number of operations performed.
pub num_operations: u64,
/// Number of modules loaded.
@@ -84,7 +84,7 @@ impl GlobalRuntimeState<'_> {
imports: crate::StaticVec::new_const(),
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec::new_const(),
source: Identifier::new_const(),
source: None,
num_operations: 0,
#[cfg(not(feature = "no_module"))]
num_modules_loaded: 0,
@@ -168,7 +168,7 @@ impl GlobalRuntimeState<'_> {
#[inline(always)]
pub fn push_import(
&mut self,
name: impl Into<crate::ImmutableString>,
name: impl Into<ImmutableString>,
module: impl Into<crate::Shared<crate::Module>>,
) {
self.imports.push(name.into());
@@ -202,7 +202,7 @@ impl GlobalRuntimeState<'_> {
#[inline]
pub(crate) fn iter_imports_raw(
&self,
) -> impl Iterator<Item = (&crate::ImmutableString, &crate::Shared<crate::Module>)> {
) -> impl Iterator<Item = (&ImmutableString, &crate::Shared<crate::Module>)> {
self.imports.iter().zip(self.modules.iter()).rev()
}
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
@@ -212,7 +212,7 @@ impl GlobalRuntimeState<'_> {
#[inline]
pub fn scan_imports_raw(
&self,
) -> impl Iterator<Item = (&crate::ImmutableString, &crate::Shared<crate::Module>)> {
) -> impl Iterator<Item = (&ImmutableString, &crate::Shared<crate::Module>)> {
self.imports.iter().zip(self.modules.iter())
}
/// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of
@@ -247,11 +247,11 @@ impl GlobalRuntimeState<'_> {
pub fn get_qualified_fn(
&self,
hash: u64,
) -> Option<(&crate::func::CallableFunction, Option<&str>)> {
) -> Option<(&crate::func::CallableFunction, Option<&ImmutableString>)> {
self.modules
.iter()
.rev()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id())))
.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
/// globally-imported [modules][crate::Module]?
@@ -278,14 +278,16 @@ impl GlobalRuntimeState<'_> {
.find_map(|m| m.get_qualified_iter(id))
}
/// Get the current source.
#[inline]
#[inline(always)]
#[must_use]
pub fn source(&self) -> Option<&str> {
if self.source.is_empty() {
None
} else {
Some(self.source.as_str())
}
self.source.as_ref().map(|s| s.as_str())
}
/// Get the current source.
#[inline(always)]
#[must_use]
pub(crate) const fn source_raw(&self) -> Option<&ImmutableString> {
self.source.as_ref()
}
/// Get the pre-calculated index getter hash.
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
@@ -317,10 +319,10 @@ impl GlobalRuntimeState<'_> {
#[cfg(not(feature = "no_module"))]
impl IntoIterator for GlobalRuntimeState<'_> {
type Item = (crate::ImmutableString, crate::Shared<crate::Module>);
type Item = (ImmutableString, crate::Shared<crate::Module>);
type IntoIter = std::iter::Rev<
std::iter::Zip<
smallvec::IntoIter<[crate::ImmutableString; crate::STATIC_VEC_INLINE_SIZE]>,
smallvec::IntoIter<[ImmutableString; crate::STATIC_VEC_INLINE_SIZE]>,
smallvec::IntoIter<[crate::Shared<crate::Module>; crate::STATIC_VEC_INLINE_SIZE]>,
>,
>;
@@ -332,10 +334,10 @@ impl IntoIterator for GlobalRuntimeState<'_> {
#[cfg(not(feature = "no_module"))]
impl<'a> IntoIterator for &'a GlobalRuntimeState<'_> {
type Item = (&'a crate::ImmutableString, &'a crate::Shared<crate::Module>);
type Item = (&'a ImmutableString, &'a crate::Shared<crate::Module>);
type IntoIter = std::iter::Rev<
std::iter::Zip<
std::slice::Iter<'a, crate::ImmutableString>,
std::slice::Iter<'a, ImmutableString>,
std::slice::Iter<'a, crate::Shared<crate::Module>>,
>,
>;
@@ -346,7 +348,7 @@ impl<'a> IntoIterator for &'a GlobalRuntimeState<'_> {
}
#[cfg(not(feature = "no_module"))]
impl<K: Into<crate::ImmutableString>, M: Into<crate::Shared<crate::Module>>> Extend<(K, M)>
impl<K: Into<ImmutableString>, M: Into<crate::Shared<crate::Module>>> Extend<(K, M)>
for GlobalRuntimeState<'_>
{
#[inline]

View File

@@ -719,8 +719,8 @@ impl Engine {
err_map.insert("message".into(), err.to_string().into());
if !global.source.is_empty() {
err_map.insert("source".into(), global.source.clone().into());
if let Some(ref source) = global.source {
err_map.insert("source".into(), source.into());
}
if !err_pos.is_none() {