This commit is contained in:
Stephen Chung 2022-11-27 17:21:07 +08:00
parent c311758b5c
commit 4ab306607d
4 changed files with 22 additions and 20 deletions

View File

@ -306,7 +306,7 @@ pub enum Expr {
Variable(
#[cfg(not(feature = "no_module"))]
Box<(Option<NonZeroUsize>, super::Namespace, u64, ImmutableString)>,
#[cfg(feature = "no_module")] Box<(Option<NonZeroUsize>, (), u64, ImmutableString)>,
#[cfg(feature = "no_module")] Box<(Option<NonZeroUsize>, [(); 0], u64, ImmutableString)>,
Option<NonZeroU8>,
Position,
),

View File

@ -191,7 +191,7 @@ impl fmt::Debug for Engine {
#[cfg(not(feature = "unchecked"))]
f.field("limits", &self.limits);
#[cfg(not(feature = "debugging"))]
#[cfg(feature = "debugging")]
f.field("debugger_interface", &self.debugger_interface.is_some());
f.finish()

View File

@ -62,25 +62,22 @@ impl Engine {
}
Expr::Variable(v, None, ..) => match &**v {
// Normal variable access
#[cfg(not(feature = "no_module"))]
(_, ns, ..) if ns.is_empty() => {
self.search_scope_only(global, caches, scope, this_ptr, expr)
}
#[cfg(feature = "no_module")]
(_, (), ..) => self.search_scope_only(global, caches, scope, this_ptr, expr),
// Qualified variable access
#[cfg(not(feature = "no_module"))]
(_, namespace, hash_var, var_name) => {
(_, ns, hash_var, var_name) => {
// foo:bar::baz::VARIABLE
if let Some(module) = self.search_imports(global, namespace) {
if let Some(module) = self.search_imports(global, ns) {
return module.get_qualified_var(*hash_var).map_or_else(
|| {
let sep = crate::tokenizer::Token::DoubleColon.literal_syntax();
Err(ERR::ErrorVariableNotFound(
format!("{namespace}{sep}{var_name}"),
namespace.position(),
format!("{ns}{sep}{var_name}"),
ns.position(),
)
.into())
},
@ -94,7 +91,7 @@ impl Engine {
// global::VARIABLE
#[cfg(not(feature = "no_function"))]
if namespace.len() == 1 && namespace.root() == crate::engine::KEYWORD_GLOBAL {
if ns.len() == 1 && ns.root() == crate::engine::KEYWORD_GLOBAL {
if let Some(ref constants) = global.constants {
if let Some(value) =
crate::func::locked_write(constants).get_mut(var_name.as_str())
@ -109,17 +106,17 @@ impl Engine {
let sep = crate::tokenizer::Token::DoubleColon.literal_syntax();
return Err(ERR::ErrorVariableNotFound(
format!("{namespace}{sep}{var_name}"),
namespace.position(),
format!("{ns}{sep}{var_name}"),
ns.position(),
)
.into());
}
Err(
ERR::ErrorModuleNotFound(namespace.to_string(), namespace.position())
.into(),
)
Err(ERR::ErrorModuleNotFound(ns.to_string(), ns.position()).into())
}
#[cfg(feature = "no_module")]
_ => unreachable!("Invalid expression {:?}", expr),
},
_ => unreachable!("Expr::Variable expected but gets {:?}", expr),
}
@ -142,14 +139,18 @@ impl Engine {
let index = match expr {
// Check if the variable is `this`
Expr::Variable(v, None, ..) if v.0.is_none() && v.3 == KEYWORD_THIS => {
Expr::Variable(v, None, ..)
if v.0.is_none() && v.1.is_empty() && v.3 == KEYWORD_THIS =>
{
return if this_ptr.is_null() {
Err(ERR::ErrorUnboundThis(expr.position()).into())
} else {
Ok(this_ptr.into())
};
}
_ if global.always_search_scope => 0,
Expr::Variable(_, Some(i), ..) => i.get() as usize,
// Scripted function with the same name
#[cfg(not(feature = "no_function"))]
@ -165,6 +166,7 @@ impl Engine {
return Ok(val.into());
}
Expr::Variable(v, None, ..) => v.0.map_or(0, NonZeroUsize::get),
_ => unreachable!("Expr::Variable expected but gets {:?}", expr),
};

View File

@ -1595,7 +1595,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
let ns = crate::ast::Namespace::NONE;
#[cfg(feature = "no_module")]
let ns = ();
let ns = [];
let s = match input.next().expect(NEVER_ENDS) {
(Token::Identifier(s), ..) => s,
@ -1660,7 +1660,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
let ns = crate::ast::Namespace::NONE;
#[cfg(feature = "no_module")]
let ns = ();
let ns = [];
let s = match input.next().expect(NEVER_ENDS) {
(Token::Reserved(s), ..) => s,
@ -2542,7 +2542,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
let ns = crate::ast::Namespace::NONE;
#[cfg(feature = "no_module")]
let ns = ();
let ns = [];
segments.push(name.clone());
tokens.push(state.get_interned_string(CUSTOM_SYNTAX_MARKER_IDENT));