Faster processing of this pointer.
This commit is contained in:
parent
0a7cca9910
commit
bfe39a9c7c
@ -109,6 +109,8 @@ impl Expression<'_> {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
Expr::Variable(x, ..) if !x.1.is_empty() => None,
|
||||
Expr::Variable(x, ..) => Some(x.3.as_str()),
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Expr::ThisPtr(..) => Some(crate::engine::KEYWORD_THIS),
|
||||
Expr::StringConstant(x, ..) => Some(x.as_str()),
|
||||
_ => None,
|
||||
}
|
||||
|
@ -297,6 +297,8 @@ pub enum Expr {
|
||||
Option<NonZeroU8>,
|
||||
Position,
|
||||
),
|
||||
/// `this`.
|
||||
ThisPtr(Position),
|
||||
/// Property access - ((getter, hash), (setter, hash), prop)
|
||||
Property(
|
||||
Box<(
|
||||
@ -375,6 +377,7 @@ impl fmt::Debug for Expr {
|
||||
.entries(x.0.iter().map(|(k, v)| (k, v)))
|
||||
.finish()
|
||||
}
|
||||
Self::ThisPtr(..) => f.debug_struct("ThisPtr").finish(),
|
||||
Self::Variable(x, i, ..) => {
|
||||
f.write_str("Variable(")?;
|
||||
|
||||
@ -632,6 +635,7 @@ impl Expr {
|
||||
| Self::Array(..)
|
||||
| Self::Map(..)
|
||||
| Self::Variable(..)
|
||||
| Self::ThisPtr(..)
|
||||
| Self::And(..)
|
||||
| Self::Or(..)
|
||||
| Self::Coalesce(..)
|
||||
@ -662,6 +666,7 @@ impl Expr {
|
||||
| Self::Array(.., pos)
|
||||
| Self::Map(.., pos)
|
||||
| Self::Variable(.., pos)
|
||||
| Self::ThisPtr(pos)
|
||||
| Self::And(.., pos)
|
||||
| Self::Or(.., pos)
|
||||
| Self::Coalesce(.., pos)
|
||||
@ -725,6 +730,7 @@ impl Expr {
|
||||
| Self::Dot(.., pos)
|
||||
| Self::Index(.., pos)
|
||||
| Self::Variable(.., pos)
|
||||
| Self::ThisPtr(pos)
|
||||
| Self::FnCall(.., pos)
|
||||
| Self::MethodCall(.., pos)
|
||||
| Self::InterpolatedString(.., pos)
|
||||
@ -816,6 +822,7 @@ impl Expr {
|
||||
| Self::StringConstant(..)
|
||||
| Self::InterpolatedString(..)
|
||||
| Self::FnCall(..)
|
||||
| Self::ThisPtr(..)
|
||||
| Self::MethodCall(..)
|
||||
| Self::Stmt(..)
|
||||
| Self::Dot(..)
|
||||
|
@ -29,6 +29,7 @@ pub const KEYWORD_IS_SHARED: &str = "is_shared";
|
||||
pub const KEYWORD_IS_DEF_VAR: &str = "is_def_var";
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
pub const KEYWORD_IS_DEF_FN: &str = "is_def_fn";
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
pub const KEYWORD_THIS: &str = "this";
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
|
@ -388,6 +388,23 @@ impl Engine {
|
||||
let scope2 = ();
|
||||
|
||||
match (lhs, new_val) {
|
||||
// this.??? or this[???]
|
||||
(Expr::ThisPtr(var_pos), new_val) => {
|
||||
self.track_operation(global, *var_pos)?;
|
||||
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, scope, this_ptr.as_deref_mut(), lhs)?;
|
||||
|
||||
if let Some(this_ptr) = this_ptr {
|
||||
let target = &mut this_ptr.into();
|
||||
|
||||
self.eval_dot_index_chain_raw(
|
||||
global, caches, scope2, None, lhs, expr, target, rhs, idx_values, new_val,
|
||||
)
|
||||
} else {
|
||||
Err(ERR::ErrorUnboundThis(*var_pos).into())
|
||||
}
|
||||
}
|
||||
// id.??? or id[???]
|
||||
(Expr::Variable(.., var_pos), new_val) => {
|
||||
self.track_operation(global, *var_pos)?;
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
use super::{Caches, EvalContext, GlobalRuntimeState, Target};
|
||||
use crate::ast::Expr;
|
||||
use crate::engine::KEYWORD_THIS;
|
||||
use crate::packages::string_basic::{print_with_func, FUNC_TO_STRING};
|
||||
use crate::types::dynamic::AccessMode;
|
||||
use crate::{Dynamic, Engine, RhaiResult, RhaiResultOf, Scope, SmartString, ERR};
|
||||
@ -140,9 +139,7 @@ impl Engine {
|
||||
|
||||
let index = match expr {
|
||||
// Check if the variable is `this`
|
||||
Expr::Variable(v, None, ..)
|
||||
if v.0.is_none() && v.1.is_empty() && v.3 == KEYWORD_THIS =>
|
||||
{
|
||||
Expr::ThisPtr(..) => {
|
||||
return if let Some(this_ptr) = this_ptr {
|
||||
Ok(this_ptr.into())
|
||||
} else {
|
||||
@ -259,13 +256,9 @@ impl Engine {
|
||||
self.eval_fn_call_expr(global, caches, scope, this_ptr, x, *pos)
|
||||
}
|
||||
|
||||
Expr::Variable(x, index, var_pos)
|
||||
if index.is_none() && x.0.is_none() && x.3 == KEYWORD_THIS =>
|
||||
{
|
||||
this_ptr
|
||||
.ok_or_else(|| ERR::ErrorUnboundThis(*var_pos).into())
|
||||
.cloned()
|
||||
}
|
||||
Expr::ThisPtr(var_pos) => this_ptr
|
||||
.ok_or_else(|| ERR::ErrorUnboundThis(*var_pos).into())
|
||||
.cloned(),
|
||||
|
||||
Expr::Variable(..) => self
|
||||
.search_namespace(global, caches, scope, this_ptr, expr)
|
||||
@ -413,13 +406,7 @@ impl Engine {
|
||||
.and_then(|r| self.check_data_size(r, expr.start_position()))
|
||||
}
|
||||
|
||||
Expr::Stmt(x) => {
|
||||
if x.is_empty() {
|
||||
Ok(Dynamic::UNIT)
|
||||
} else {
|
||||
self.eval_stmt_block(global, caches, scope, this_ptr, x, true)
|
||||
}
|
||||
}
|
||||
Expr::Stmt(x) => self.eval_stmt_block(global, caches, scope, this_ptr, x, true),
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Expr::Index(..) => {
|
||||
|
@ -302,7 +302,26 @@ impl Engine {
|
||||
Stmt::Assignment(x, ..) => {
|
||||
let (op_info, BinaryExpr { lhs, rhs }) = &**x;
|
||||
|
||||
if let Expr::Variable(x, ..) = lhs {
|
||||
if let Expr::ThisPtr(..) = lhs {
|
||||
if this_ptr.is_none() {
|
||||
return Err(ERR::ErrorUnboundThis(lhs.position()).into());
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
{
|
||||
let rhs_val = self
|
||||
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), rhs)?
|
||||
.flatten();
|
||||
|
||||
self.track_operation(global, lhs.position())?;
|
||||
|
||||
let target = &mut this_ptr.unwrap().into();
|
||||
|
||||
self.eval_op_assignment(global, caches, op_info, lhs, target, rhs_val)?;
|
||||
}
|
||||
#[cfg(feature = "no_function")]
|
||||
unreachable!();
|
||||
} else if let Expr::Variable(x, ..) = lhs {
|
||||
let rhs_val = self
|
||||
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), rhs)?
|
||||
.flatten();
|
||||
@ -342,6 +361,10 @@ impl Engine {
|
||||
// Must be either `var[index] op= val` or `var.prop op= val`.
|
||||
// The return value of any op-assignment (should be `()`) is thrown away and not used.
|
||||
let _ = match lhs {
|
||||
// this op= rhs (handled above)
|
||||
Expr::ThisPtr(..) => {
|
||||
unreachable!("Expr::ThisPtr case is already handled")
|
||||
}
|
||||
// name op= rhs (handled above)
|
||||
Expr::Variable(..) => {
|
||||
unreachable!("Expr::Variable case is already handled")
|
||||
|
@ -901,8 +901,8 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
|
||||
*expr = mem::take(&mut m.0).into_iter().find(|(x, ..)| x.as_str() == prop)
|
||||
.map_or_else(|| Expr::Unit(*pos), |(.., mut expr)| { expr.set_position(*pos); expr });
|
||||
}
|
||||
// var.rhs
|
||||
(Expr::Variable(..), rhs) => optimize_expr(rhs, state, true),
|
||||
// var.rhs or this.rhs
|
||||
(Expr::Variable(..) | Expr::ThisPtr(..), rhs) => optimize_expr(rhs, state, true),
|
||||
// const.type_of()
|
||||
(lhs, Expr::MethodCall(x, pos)) if lhs.is_constant() && x.name == KEYWORD_TYPE_OF && x.args.is_empty() => {
|
||||
if let Some(value) = lhs.get_literal_value() {
|
||||
@ -987,8 +987,8 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
|
||||
state.set_dirty();
|
||||
*expr = Expr::CharConstant(s.chars().rev().nth(i.unsigned_abs() as usize - 1).unwrap(), *pos);
|
||||
}
|
||||
// var[rhs]
|
||||
(Expr::Variable(..), rhs) => optimize_expr(rhs, state, true),
|
||||
// var[rhs] or this[rhs]
|
||||
(Expr::Variable(..) | Expr::ThisPtr(..), rhs) => optimize_expr(rhs, state, true),
|
||||
// lhs[rhs]
|
||||
(lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, true); }
|
||||
},
|
||||
|
@ -7,7 +7,7 @@ use crate::ast::{
|
||||
FnCallHashes, Ident, Namespace, OpAssignment, RangeCase, ScriptFnDef, Stmt, StmtBlock,
|
||||
StmtBlockContainer, SwitchCasesCollection,
|
||||
};
|
||||
use crate::engine::{Precedence, KEYWORD_THIS, OP_CONTAINS, OP_NOT};
|
||||
use crate::engine::{Precedence, OP_CONTAINS, OP_NOT};
|
||||
use crate::eval::{Caches, GlobalRuntimeState};
|
||||
use crate::func::{hashing::get_hasher, StraightHashMap};
|
||||
use crate::tokenizer::{
|
||||
@ -1750,21 +1750,19 @@ impl Engine {
|
||||
settings.pos,
|
||||
)
|
||||
}
|
||||
// Access to `this` as a variable is OK within a function scope
|
||||
// Access to `this` as a variable
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
_ if *s == KEYWORD_THIS && settings.has_flag(ParseSettingFlags::FN_SCOPE) => {
|
||||
Expr::Variable(
|
||||
(None, ns, 0, state.get_interned_string(*s)).into(),
|
||||
None,
|
||||
settings.pos,
|
||||
)
|
||||
}
|
||||
// Cannot access to `this` as a variable not in a function scope
|
||||
_ if *s == KEYWORD_THIS => {
|
||||
let msg = format!("'{s}' can only be used in functions");
|
||||
return Err(
|
||||
LexError::ImproperSymbol(s.to_string(), msg).into_err(settings.pos)
|
||||
);
|
||||
_ if *s == crate::engine::KEYWORD_THIS => {
|
||||
// OK within a function scope
|
||||
if settings.has_flag(ParseSettingFlags::FN_SCOPE) {
|
||||
Expr::ThisPtr(settings.pos)
|
||||
} else {
|
||||
// Cannot access to `this` as a variable not in a function scope
|
||||
let msg = format!("'{s}' can only be used in functions");
|
||||
return Err(
|
||||
LexError::ImproperSymbol(s.to_string(), msg).into_err(settings.pos)
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => return Err(PERR::Reserved(s.to_string()).into_err(settings.pos)),
|
||||
}
|
||||
@ -2148,10 +2146,8 @@ impl Engine {
|
||||
};
|
||||
|
||||
match lhs {
|
||||
// const_expr = rhs
|
||||
ref expr if expr.is_constant() => {
|
||||
Err(PERR::AssignmentToConstant(String::new()).into_err(lhs.start_position()))
|
||||
}
|
||||
// this = rhs
|
||||
Expr::ThisPtr(_) => Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into())),
|
||||
// var (non-indexed) = rhs
|
||||
Expr::Variable(ref x, None, _) if x.0.is_none() => {
|
||||
Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into()))
|
||||
@ -2186,8 +2182,8 @@ impl Engine {
|
||||
match valid_lvalue {
|
||||
None => {
|
||||
match x.lhs {
|
||||
// var[???] = rhs, var.??? = rhs
|
||||
Expr::Variable(..) => {
|
||||
// var[???] = rhs, this[???] = rhs, var.??? = rhs, this.??? = rhs
|
||||
Expr::Variable(..) | Expr::ThisPtr(..) => {
|
||||
Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into()))
|
||||
}
|
||||
// expr[???] = rhs, expr.??? = rhs
|
||||
@ -2200,6 +2196,10 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
}
|
||||
// const_expr = rhs
|
||||
ref expr if expr.is_constant() => {
|
||||
Err(PERR::AssignmentToConstant(String::new()).into_err(lhs.start_position()))
|
||||
}
|
||||
// ??? && ??? = rhs, ??? || ??? = rhs, xxx ?? xxx = rhs
|
||||
Expr::And(..) | Expr::Or(..) | Expr::Coalesce(..) if !op_info.is_op_assignment() => {
|
||||
Err(LexError::ImproperSymbol(
|
||||
|
@ -1186,6 +1186,7 @@ impl Dynamic {
|
||||
/// it is cloned into a [`Dynamic`] with a normal value.
|
||||
///
|
||||
/// Returns itself if types mismatched.
|
||||
#[allow(unused_mut)]
|
||||
pub(crate) fn try_cast_raw<T: Any>(mut self) -> Result<T, Self> {
|
||||
// Coded this way in order to maximally leverage potentials for dead-code removal.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user