From 08ca90a1368656421d5eac440bc27634aa7280d9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 2 Oct 2020 18:52:18 +0800 Subject: [PATCH] Use ImmutableString for import alias. --- src/engine.rs | 9 ++++++--- src/parser.rs | 4 ++-- src/syntax.rs | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index cceda886..0bde53c6 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -36,7 +36,6 @@ use crate::utils::ImmutableString; use crate::any::DynamicWriteLock; use crate::stdlib::{ - borrow::Cow, boxed::Box, collections::{HashMap, HashSet}, fmt, format, @@ -70,7 +69,11 @@ pub type Map = HashMap; /// ## WARNING /// /// This type is volatile and may change. -pub type Imports<'a> = Vec<(Cow<'a, str>, Module)>; +// +// Note - We cannot use &str or Cow here because `eval` may load a module +// and the module name will live beyond the AST of the eval script text. +// The best we can do is a shared reference. +pub type Imports = Vec<(ImmutableString, Module)>; #[cfg(not(feature = "unchecked"))] #[cfg(debug_assertions)] @@ -1826,7 +1829,7 @@ impl Engine { if let Some((name, _)) = alias { module.index_all_sub_modules(); - mods.push((name.clone().into(), module)); + mods.push((name.clone(), module)); } state.modules += 1; diff --git a/src/parser.rs b/src/parser.rs index 356429eb..4618f041 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -592,7 +592,7 @@ pub enum Stmt { ReturnWithVal(Box<((ReturnType, Position), Option, Position)>), /// import expr as module #[cfg(not(feature = "no_module"))] - Import(Box<(Expr, Option<(String, Position)>, Position)>), + Import(Box<(Expr, Option<(ImmutableString, Position)>, Position)>), /// expr id as name, ... #[cfg(not(feature = "no_module"))] Export( @@ -2753,7 +2753,7 @@ fn parse_import( Ok(Stmt::Import(Box::new(( expr, - Some((name, settings.pos)), + Some((name.into(), settings.pos)), token_pos, )))) } diff --git a/src/syntax.rs b/src/syntax.rs index cf9c9519..9f3f7f36 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -74,8 +74,8 @@ impl fmt::Debug for CustomSyntax { /// Context of a script evaluation process. #[derive(Debug)] -pub struct EvalContext<'a, 'b: 'a, 's, 'm, 't, 'd: 't> { - pub(crate) mods: &'a mut Imports<'b>, +pub struct EvalContext<'a, 's, 'm, 't, 'd: 't> { + pub(crate) mods: &'a mut Imports, pub(crate) state: &'s mut State, pub(crate) lib: &'m Module, pub(crate) this_ptr: &'t mut Option<&'d mut Dynamic>,