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