Use ImmutableString for import alias.

This commit is contained in:
Stephen Chung 2020-10-02 18:52:18 +08:00
parent 038b058e63
commit 08ca90a136
3 changed files with 10 additions and 7 deletions

View File

@ -36,7 +36,6 @@ use crate::utils::ImmutableString;
use crate::any::DynamicWriteLock; use crate::any::DynamicWriteLock;
use crate::stdlib::{ use crate::stdlib::{
borrow::Cow,
boxed::Box, boxed::Box,
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
fmt, format, fmt, format,
@ -70,7 +69,11 @@ pub type Map = HashMap<ImmutableString, Dynamic>;
/// ## WARNING /// ## WARNING
/// ///
/// This type is volatile and may change. /// This type is volatile and may change.
pub type Imports<'a> = Vec<(Cow<'a, str>, Module)>; //
// Note - We cannot use &str or Cow<str> 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(not(feature = "unchecked"))]
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -1826,7 +1829,7 @@ impl Engine {
if let Some((name, _)) = alias { if let Some((name, _)) = alias {
module.index_all_sub_modules(); module.index_all_sub_modules();
mods.push((name.clone().into(), module)); mods.push((name.clone(), module));
} }
state.modules += 1; state.modules += 1;

View File

@ -592,7 +592,7 @@ pub enum Stmt {
ReturnWithVal(Box<((ReturnType, Position), Option<Expr>, Position)>), ReturnWithVal(Box<((ReturnType, Position), Option<Expr>, Position)>),
/// import expr as module /// import expr as module
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Import(Box<(Expr, Option<(String, Position)>, Position)>), Import(Box<(Expr, Option<(ImmutableString, Position)>, Position)>),
/// expr id as name, ... /// expr id as name, ...
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Export( Export(
@ -2753,7 +2753,7 @@ fn parse_import(
Ok(Stmt::Import(Box::new(( Ok(Stmt::Import(Box::new((
expr, expr,
Some((name, settings.pos)), Some((name.into(), settings.pos)),
token_pos, token_pos,
)))) ))))
} }

View File

@ -74,8 +74,8 @@ impl fmt::Debug for CustomSyntax {
/// Context of a script evaluation process. /// Context of a script evaluation process.
#[derive(Debug)] #[derive(Debug)]
pub struct EvalContext<'a, 'b: 'a, 's, 'm, 't, 'd: 't> { pub struct EvalContext<'a, 's, 'm, 't, 'd: 't> {
pub(crate) mods: &'a mut Imports<'b>, pub(crate) mods: &'a mut Imports,
pub(crate) state: &'s mut State, pub(crate) state: &'s mut State,
pub(crate) lib: &'m Module, pub(crate) lib: &'m Module,
pub(crate) this_ptr: &'t mut Option<&'d mut Dynamic>, pub(crate) this_ptr: &'t mut Option<&'d mut Dynamic>,