2022-01-07 11:43:47 +08:00
|
|
|
//! Types to support chaining operations (i.e. indexing and dotting).
|
|
|
|
#![cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
|
|
|
|
2022-04-16 16:36:53 +08:00
|
|
|
use super::{Caches, GlobalRuntimeState, Target};
|
2022-02-25 11:42:59 +08:00
|
|
|
use crate::ast::{ASTFlags, Expr, OpAssignment};
|
2022-01-07 12:19:01 +08:00
|
|
|
use crate::types::dynamic::Union;
|
2022-06-08 09:19:21 +08:00
|
|
|
use crate::{Dynamic, Engine, FnArgsVec, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR};
|
2022-01-07 11:43:47 +08:00
|
|
|
use std::hash::Hash;
|
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
|
|
|
|
/// Method of chaining.
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
|
|
|
pub enum ChainType {
|
|
|
|
/// Indexing.
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
Indexing,
|
|
|
|
/// Dotting.
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
Dotting,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&Expr> for ChainType {
|
|
|
|
#[inline]
|
|
|
|
fn from(expr: &Expr) -> Self {
|
|
|
|
match expr {
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Index(..) => Self::Indexing,
|
2022-01-07 11:43:47 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Dot(..) => Self::Dotting,
|
2022-01-07 11:43:47 +08:00
|
|
|
expr => unreachable!("Expr::Index or Expr::Dot expected but gets {:?}", expr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
impl Engine {
|
|
|
|
/// Chain-evaluate a dot/index chain.
|
2022-08-10 12:48:37 +08:00
|
|
|
/// [`Position`] in [`EvalAltResult`][crate::EvalAltResult] may be [`NONE`][Position::NONE] and should be set afterwards.
|
2022-01-07 12:19:01 +08:00
|
|
|
fn eval_dot_index_chain_helper(
|
|
|
|
&self,
|
|
|
|
global: &mut GlobalRuntimeState,
|
2022-04-16 16:36:53 +08:00
|
|
|
caches: &mut Caches,
|
2022-01-07 12:19:01 +08:00
|
|
|
lib: &[&Module],
|
|
|
|
this_ptr: &mut Option<&mut Dynamic>,
|
|
|
|
target: &mut Target,
|
|
|
|
root: (&str, Position),
|
2022-04-11 16:29:16 +08:00
|
|
|
_parent: &Expr,
|
2022-01-07 12:19:01 +08:00
|
|
|
rhs: &Expr,
|
2022-04-11 16:29:16 +08:00
|
|
|
_parent_options: ASTFlags,
|
2022-06-08 09:19:21 +08:00
|
|
|
idx_values: &mut FnArgsVec<Dynamic>,
|
2022-01-07 12:19:01 +08:00
|
|
|
chain_type: ChainType,
|
|
|
|
level: usize,
|
2022-09-27 23:04:22 +08:00
|
|
|
new_val: &mut Option<(Dynamic, &OpAssignment)>,
|
2022-01-07 12:19:01 +08:00
|
|
|
) -> RhaiResultOf<(Dynamic, bool)> {
|
|
|
|
let is_ref_mut = target.is_ref();
|
|
|
|
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
|
|
|
let scope = &mut Scope::new();
|
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
match chain_type {
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
ChainType::Indexing => {
|
2022-06-12 00:32:12 +08:00
|
|
|
// Check for existence with the null conditional operator
|
|
|
|
if _parent_options.contains(ASTFlags::NEGATED) && target.is::<()>() {
|
|
|
|
return Ok((Dynamic::UNIT, false));
|
|
|
|
}
|
|
|
|
|
2022-02-04 12:04:33 +08:00
|
|
|
let pos = rhs.start_position();
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
match rhs {
|
|
|
|
// xxx[idx].expr... | xxx[idx][expr]...
|
2022-02-25 11:42:59 +08:00
|
|
|
Expr::Dot(x, options, x_pos) | Expr::Index(x, options, x_pos)
|
|
|
|
if !_parent_options.contains(ASTFlags::BREAK) =>
|
2022-01-07 12:19:01 +08:00
|
|
|
{
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, _parent, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-09-27 23:04:22 +08:00
|
|
|
let idx_val = &mut idx_values.pop().unwrap();
|
2022-01-07 12:19:01 +08:00
|
|
|
let mut idx_val_for_setter = idx_val.clone();
|
2022-02-04 12:04:33 +08:00
|
|
|
let idx_pos = x.lhs.start_position();
|
2022-01-07 12:19:01 +08:00
|
|
|
let rhs_chain = rhs.into();
|
|
|
|
|
|
|
|
let (try_setter, result) = {
|
|
|
|
let mut obj = self.get_indexed_mut(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, idx_val, idx_pos, false, true, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)?;
|
|
|
|
let is_obj_temp_val = obj.is_temp_value();
|
|
|
|
let obj_ptr = &mut obj;
|
|
|
|
|
|
|
|
match self.eval_dot_index_chain_helper(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, this_ptr, obj_ptr, root, rhs, &x.rhs,
|
|
|
|
*options, idx_values, rhs_chain, level, new_val,
|
2022-01-07 12:19:01 +08:00
|
|
|
) {
|
|
|
|
Ok((result, true)) if is_obj_temp_val => {
|
|
|
|
(Some(obj.take_or_clone()), (result, true))
|
|
|
|
}
|
|
|
|
Ok(result) => (None, result),
|
|
|
|
Err(err) => return Err(err.fill_position(*x_pos)),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(mut new_val) = try_setter {
|
|
|
|
// Try to call index setter if value is changed
|
2022-02-09 13:12:43 +08:00
|
|
|
let idx = &mut idx_val_for_setter;
|
|
|
|
let new_val = &mut new_val;
|
|
|
|
self.call_indexer_set(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, idx, new_val, is_ref_mut, level,
|
2022-02-09 13:12:43 +08:00
|
|
|
)
|
2022-02-24 10:36:20 +08:00
|
|
|
.or_else(|e| match *e {
|
2022-02-09 13:12:43 +08:00
|
|
|
ERR::ErrorIndexingType(..) => Ok((Dynamic::UNIT, false)),
|
2022-02-24 10:36:20 +08:00
|
|
|
_ => Err(e),
|
2022-02-09 13:12:43 +08:00
|
|
|
})?;
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
// xxx[rhs] op= new_val
|
|
|
|
_ if new_val.is_some() => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, _parent, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-09-27 23:04:22 +08:00
|
|
|
let (new_val, op_info) = new_val.take().expect("`Some`");
|
|
|
|
let idx_val = &mut idx_values.pop().unwrap();
|
|
|
|
let idx = &mut idx_val.clone();
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
let try_setter = match self.get_indexed_mut(
|
2022-09-27 23:04:22 +08:00
|
|
|
global, caches, lib, target, idx, pos, true, false, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
) {
|
2022-04-14 22:55:39 +08:00
|
|
|
// Indexed value is not a temp value - update directly
|
2022-01-07 12:19:01 +08:00
|
|
|
Ok(ref mut obj_ptr) => {
|
|
|
|
self.eval_op_assignment(
|
2022-04-18 23:12:47 +08:00
|
|
|
global, caches, lib, op_info, obj_ptr, root, new_val, level,
|
|
|
|
)?;
|
2022-01-07 12:19:01 +08:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2022-04-18 23:12:47 +08:00
|
|
|
self.check_data_size(obj_ptr, op_info.pos)?;
|
2022-01-07 12:19:01 +08:00
|
|
|
None
|
|
|
|
}
|
2022-04-14 22:55:39 +08:00
|
|
|
// Indexed value cannot be referenced - use indexer
|
2022-01-07 12:19:01 +08:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Err(err) if matches!(*err, ERR::ErrorIndexingType(..)) => Some(new_val),
|
2022-01-07 12:19:01 +08:00
|
|
|
// Any other error
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(mut new_val) = try_setter {
|
2022-04-14 22:55:39 +08:00
|
|
|
// Is this an op-assignment?
|
2022-04-18 23:12:47 +08:00
|
|
|
if op_info.is_op_assignment() {
|
2022-09-27 23:04:22 +08:00
|
|
|
let idx = &mut idx_val.clone();
|
|
|
|
|
2022-04-14 22:55:39 +08:00
|
|
|
// Call the index getter to get the current value
|
|
|
|
if let Ok(val) =
|
2022-04-16 16:36:53 +08:00
|
|
|
self.call_indexer_get(global, caches, lib, target, idx, level)
|
2022-04-14 22:55:39 +08:00
|
|
|
{
|
2022-08-27 16:26:41 +08:00
|
|
|
let mut val = val.into();
|
2022-04-14 22:55:39 +08:00
|
|
|
// Run the op-assignment
|
|
|
|
self.eval_op_assignment(
|
2022-08-27 16:26:41 +08:00
|
|
|
global, caches, lib, op_info, &mut val, root, new_val,
|
2022-04-18 23:12:47 +08:00
|
|
|
level,
|
|
|
|
)?;
|
2022-04-14 22:55:39 +08:00
|
|
|
// Replace new value
|
2022-08-27 16:26:41 +08:00
|
|
|
new_val = val.take_or_clone();
|
2022-04-14 22:55:39 +08:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2022-04-18 23:12:47 +08:00
|
|
|
self.check_data_size(&new_val, op_info.pos)?;
|
2022-04-14 22:55:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
// Try to call index setter
|
2022-02-09 13:12:43 +08:00
|
|
|
let new_val = &mut new_val;
|
2022-09-27 23:04:22 +08:00
|
|
|
|
2022-02-09 13:12:43 +08:00
|
|
|
self.call_indexer_set(
|
2022-09-27 23:04:22 +08:00
|
|
|
global, caches, lib, target, idx_val, new_val, is_ref_mut, level,
|
2022-02-10 17:55:32 +08:00
|
|
|
)?;
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok((Dynamic::UNIT, true))
|
|
|
|
}
|
|
|
|
// xxx[rhs]
|
2022-01-25 12:24:30 +08:00
|
|
|
_ => {
|
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, _parent, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-09-27 23:04:22 +08:00
|
|
|
let idx_val = &mut idx_values.pop().unwrap();
|
2022-06-08 09:19:21 +08:00
|
|
|
|
2022-01-25 12:24:30 +08:00
|
|
|
self.get_indexed_mut(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, idx_val, pos, false, true, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
2022-01-25 12:24:30 +08:00
|
|
|
.map(|v| (v.take_or_clone(), false))
|
|
|
|
}
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
ChainType::Dotting => {
|
2022-06-10 10:26:06 +08:00
|
|
|
// Check for existence with the Elvis operator
|
|
|
|
if _parent_options.contains(ASTFlags::NEGATED) && target.is::<()>() {
|
|
|
|
return Ok((Dynamic::UNIT, false));
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
match rhs {
|
|
|
|
// xxx.fn_name(arg_expr_list)
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(x, pos) if !x.is_qualified() && new_val.is_none() => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
let reset_debugger =
|
|
|
|
self.run_debugger_with_reset(scope, global, lib, this_ptr, rhs, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-06-08 09:19:21 +08:00
|
|
|
let crate::ast::FnCallExpr {
|
|
|
|
name, hashes, args, ..
|
2022-07-05 16:26:38 +08:00
|
|
|
} = &**x;
|
2022-06-08 09:19:21 +08:00
|
|
|
|
2022-06-08 16:34:56 +08:00
|
|
|
let offset = idx_values.len() - args.len();
|
|
|
|
let call_args = &mut idx_values[offset..];
|
|
|
|
let pos1 = args.get(0).map_or(Position::NONE, Expr::position);
|
2022-06-08 09:19:21 +08:00
|
|
|
|
2022-01-25 12:24:30 +08:00
|
|
|
let result = self.make_method_call(
|
2022-06-08 16:34:56 +08:00
|
|
|
global, caches, lib, name, *hashes, target, call_args, pos1, *pos,
|
|
|
|
level,
|
2022-01-25 12:24:30 +08:00
|
|
|
);
|
|
|
|
|
2022-06-08 16:34:56 +08:00
|
|
|
idx_values.truncate(offset);
|
|
|
|
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
|
|
|
global.debugger.reset_status(reset_debugger);
|
|
|
|
|
|
|
|
result
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
// xxx.fn_name(...) = ???
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(..) if new_val.is_some() => {
|
2022-01-07 12:19:01 +08:00
|
|
|
unreachable!("method call cannot be assigned to")
|
|
|
|
}
|
|
|
|
// xxx.module::fn_name(...) - syntax error
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(..) => {
|
2022-01-07 12:19:01 +08:00
|
|
|
unreachable!("function call in dot chain should not be namespace-qualified")
|
|
|
|
}
|
|
|
|
// {xxx:map}.id op= ???
|
2022-01-28 10:11:40 +08:00
|
|
|
Expr::Property(x, pos) if target.is::<crate::Map>() && new_val.is_some() => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, rhs, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-09-27 23:04:22 +08:00
|
|
|
let index = &mut x.2.clone().into();
|
|
|
|
let (new_val, op_info) = new_val.take().expect("`Some`");
|
2022-01-07 12:19:01 +08:00
|
|
|
{
|
|
|
|
let val_target = &mut self.get_indexed_mut(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, index, *pos, true, false, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)?;
|
|
|
|
self.eval_op_assignment(
|
2022-04-18 23:12:47 +08:00
|
|
|
global, caches, lib, op_info, val_target, root, new_val, level,
|
|
|
|
)?;
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2022-04-18 23:12:47 +08:00
|
|
|
self.check_data_size(target.source(), op_info.pos)?;
|
2022-01-07 12:19:01 +08:00
|
|
|
Ok((Dynamic::UNIT, true))
|
|
|
|
}
|
|
|
|
// {xxx:map}.id
|
2022-01-28 10:11:40 +08:00
|
|
|
Expr::Property(x, pos) if target.is::<crate::Map>() => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, rhs, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-09-27 23:04:22 +08:00
|
|
|
let index = &mut x.2.clone().into();
|
2022-01-07 12:19:01 +08:00
|
|
|
let val = self.get_indexed_mut(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, index, *pos, false, false, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)?;
|
|
|
|
Ok((val.take_or_clone(), false))
|
|
|
|
}
|
|
|
|
// xxx.id op= ???
|
2022-01-28 10:11:40 +08:00
|
|
|
Expr::Property(x, pos) if new_val.is_some() => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, rhs, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-07-05 16:26:38 +08:00
|
|
|
let ((getter, hash_get), (setter, hash_set), name) = &**x;
|
2022-09-27 23:04:22 +08:00
|
|
|
let (mut new_val, op_info) = new_val.take().expect("`Some`");
|
2022-01-07 12:19:01 +08:00
|
|
|
|
2022-04-18 23:12:47 +08:00
|
|
|
if op_info.is_op_assignment() {
|
2022-01-07 12:19:01 +08:00
|
|
|
let args = &mut [target.as_mut()];
|
2022-02-08 09:02:15 +08:00
|
|
|
let (mut orig_val, ..) = self
|
2022-05-19 14:36:58 +08:00
|
|
|
.call_native_fn(
|
|
|
|
global, caches, lib, getter, *hash_get, args, is_ref_mut,
|
|
|
|
false, *pos, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
|
|
|
.or_else(|err| match *err {
|
|
|
|
// Try an indexer if property does not exist
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorDotExpr(..) => {
|
2022-02-09 13:12:43 +08:00
|
|
|
let mut prop = name.into();
|
|
|
|
self.call_indexer_get(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, &mut prop, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
2022-04-14 22:55:39 +08:00
|
|
|
.map(|r| (r, false))
|
2022-02-24 10:36:20 +08:00
|
|
|
.map_err(|e| {
|
|
|
|
match *e {
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorIndexingType(..) => err,
|
2022-02-24 10:36:20 +08:00
|
|
|
_ => e,
|
|
|
|
}
|
|
|
|
})
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})?;
|
|
|
|
|
2022-02-24 10:36:20 +08:00
|
|
|
{
|
|
|
|
let orig_val = &mut (&mut orig_val).into();
|
|
|
|
|
|
|
|
self.eval_op_assignment(
|
2022-04-18 23:12:47 +08:00
|
|
|
global, caches, lib, op_info, orig_val, root, new_val, level,
|
|
|
|
)?;
|
2022-02-24 10:36:20 +08:00
|
|
|
}
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
new_val = orig_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = &mut [target.as_mut(), &mut new_val];
|
2022-05-19 14:36:58 +08:00
|
|
|
self.call_native_fn(
|
|
|
|
global, caches, lib, setter, *hash_set, args, is_ref_mut, false, *pos,
|
2022-01-07 12:19:01 +08:00
|
|
|
level,
|
|
|
|
)
|
|
|
|
.or_else(|err| match *err {
|
|
|
|
// Try an indexer if property does not exist
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorDotExpr(..) => {
|
2022-02-09 13:12:43 +08:00
|
|
|
let idx = &mut name.into();
|
|
|
|
let new_val = &mut new_val;
|
|
|
|
self.call_indexer_set(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, idx, new_val, is_ref_mut, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
2022-02-24 10:36:20 +08:00
|
|
|
.map_err(|e| match *e {
|
|
|
|
ERR::ErrorIndexingType(..) => err,
|
|
|
|
_ => e,
|
|
|
|
})
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// xxx.id
|
2022-01-28 10:11:40 +08:00
|
|
|
Expr::Property(x, pos) => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, rhs, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-07-05 16:26:38 +08:00
|
|
|
let ((getter, hash_get), _, name) = &**x;
|
2022-01-07 12:19:01 +08:00
|
|
|
let args = &mut [target.as_mut()];
|
2022-05-19 14:36:58 +08:00
|
|
|
self.call_native_fn(
|
|
|
|
global, caches, lib, getter, *hash_get, args, is_ref_mut, false, *pos,
|
2022-01-07 12:19:01 +08:00
|
|
|
level,
|
|
|
|
)
|
|
|
|
.map_or_else(
|
|
|
|
|err| match *err {
|
|
|
|
// Try an indexer if property does not exist
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorDotExpr(..) => {
|
2022-02-09 13:12:43 +08:00
|
|
|
let mut prop = name.into();
|
|
|
|
self.call_indexer_get(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, &mut prop, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
2022-04-14 22:55:39 +08:00
|
|
|
.map(|r| (r, false))
|
2022-02-24 10:36:20 +08:00
|
|
|
.map_err(|e| match *e {
|
|
|
|
ERR::ErrorIndexingType(..) => err,
|
|
|
|
_ => e,
|
2022-01-07 12:19:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
},
|
|
|
|
// Assume getters are always pure
|
2022-02-08 09:02:15 +08:00
|
|
|
|(v, ..)| Ok((v, false)),
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
// {xxx:map}.sub_lhs[expr] | {xxx:map}.sub_lhs.expr
|
2022-02-25 11:42:59 +08:00
|
|
|
Expr::Index(x, options, x_pos) | Expr::Dot(x, options, x_pos)
|
2022-01-07 12:19:01 +08:00
|
|
|
if target.is::<crate::Map>() =>
|
|
|
|
{
|
2022-01-25 14:32:14 +08:00
|
|
|
let _node = &x.lhs;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
let val_target = &mut match x.lhs {
|
2022-01-28 10:11:40 +08:00
|
|
|
Expr::Property(ref p, pos) => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, _node, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-09-27 23:04:22 +08:00
|
|
|
let index = &mut p.2.clone().into();
|
2022-01-07 12:19:01 +08:00
|
|
|
self.get_indexed_mut(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, index, pos, false, true, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)?
|
|
|
|
}
|
|
|
|
// {xxx:map}.fn_name(arg_expr_list)[expr] | {xxx:map}.fn_name(arg_expr_list).expr
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(ref x, pos) if !x.is_qualified() => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-02-03 11:56:08 +08:00
|
|
|
let reset_debugger = self.run_debugger_with_reset(
|
2022-04-16 23:32:14 +08:00
|
|
|
scope, global, lib, this_ptr, _node, level,
|
2022-02-03 11:56:08 +08:00
|
|
|
)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-06-08 09:19:21 +08:00
|
|
|
let crate::ast::FnCallExpr {
|
|
|
|
name, hashes, args, ..
|
2022-07-05 16:26:38 +08:00
|
|
|
} = &**x;
|
2022-06-08 09:19:21 +08:00
|
|
|
|
2022-06-08 16:34:56 +08:00
|
|
|
let offset = idx_values.len() - args.len();
|
|
|
|
let call_args = &mut idx_values[offset..];
|
|
|
|
let pos1 = args.get(0).map_or(Position::NONE, Expr::position);
|
2022-06-08 09:19:21 +08:00
|
|
|
|
2022-01-25 12:24:30 +08:00
|
|
|
let result = self.make_method_call(
|
2022-06-08 16:34:56 +08:00
|
|
|
global, caches, lib, name, *hashes, target, call_args, pos1,
|
|
|
|
pos, level,
|
2022-01-25 12:24:30 +08:00
|
|
|
);
|
|
|
|
|
2022-06-08 16:34:56 +08:00
|
|
|
idx_values.truncate(offset);
|
|
|
|
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
|
|
|
global.debugger.reset_status(reset_debugger);
|
|
|
|
|
|
|
|
result?.0.into()
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
// {xxx:map}.module::fn_name(...) - syntax error
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(..) => unreachable!(
|
2022-01-07 12:19:01 +08:00
|
|
|
"function call in dot chain should not be namespace-qualified"
|
|
|
|
),
|
|
|
|
// Others - syntax error
|
|
|
|
ref expr => unreachable!("invalid dot expression: {:?}", expr),
|
|
|
|
};
|
|
|
|
let rhs_chain = rhs.into();
|
|
|
|
|
|
|
|
self.eval_dot_index_chain_helper(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, this_ptr, val_target, root, rhs, &x.rhs, *options,
|
2022-01-07 12:19:01 +08:00
|
|
|
idx_values, rhs_chain, level, new_val,
|
|
|
|
)
|
|
|
|
.map_err(|err| err.fill_position(*x_pos))
|
|
|
|
}
|
|
|
|
// xxx.sub_lhs[expr] | xxx.sub_lhs.expr
|
2022-02-25 11:42:59 +08:00
|
|
|
Expr::Index(x, options, x_pos) | Expr::Dot(x, options, x_pos) => {
|
2022-01-25 14:32:14 +08:00
|
|
|
let _node = &x.lhs;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
match x.lhs {
|
|
|
|
// xxx.prop[expr] | xxx.prop.expr
|
2022-01-28 10:11:40 +08:00
|
|
|
Expr::Property(ref p, pos) => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, _node, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-07-05 16:26:38 +08:00
|
|
|
let ((getter, hash_get), (setter, hash_set), name) = &**p;
|
2022-01-07 12:19:01 +08:00
|
|
|
let rhs_chain = rhs.into();
|
|
|
|
let mut arg_values = [target.as_mut(), &mut Dynamic::UNIT.clone()];
|
|
|
|
let args = &mut arg_values[..1];
|
|
|
|
|
|
|
|
// Assume getters are always pure
|
2022-02-08 09:02:15 +08:00
|
|
|
let (mut val, ..) = self
|
2022-05-19 14:36:58 +08:00
|
|
|
.call_native_fn(
|
|
|
|
global, caches, lib, getter, *hash_get, args, is_ref_mut,
|
|
|
|
false, pos, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
|
|
|
.or_else(|err| match *err {
|
|
|
|
// Try an indexer if property does not exist
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorDotExpr(..) => {
|
2022-02-09 13:12:43 +08:00
|
|
|
let mut prop = name.into();
|
|
|
|
self.call_indexer_get(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, &mut prop, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
2022-04-14 22:55:39 +08:00
|
|
|
.map(|r| (r, false))
|
2022-01-07 12:19:01 +08:00
|
|
|
.map_err(
|
2022-02-24 10:36:20 +08:00
|
|
|
|e| match *e {
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorIndexingType(..) => err,
|
2022-02-24 10:36:20 +08:00
|
|
|
_ => e,
|
2022-01-07 12:19:01 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})?;
|
|
|
|
|
2022-02-24 10:36:20 +08:00
|
|
|
let val = &mut (&mut val).into();
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
let (result, may_be_changed) = self
|
|
|
|
.eval_dot_index_chain_helper(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, this_ptr, val, root, rhs, &x.rhs,
|
2022-02-25 11:42:59 +08:00
|
|
|
*options, idx_values, rhs_chain, level, new_val,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
|
|
|
.map_err(|err| err.fill_position(*x_pos))?;
|
|
|
|
|
|
|
|
// Feed the value back via a setter just in case it has been updated
|
|
|
|
if may_be_changed {
|
|
|
|
// Re-use args because the first &mut parameter will not be consumed
|
2022-02-24 10:36:20 +08:00
|
|
|
let mut arg_values = [target.as_mut(), val.as_mut()];
|
2022-01-07 12:19:01 +08:00
|
|
|
let args = &mut arg_values;
|
2022-05-19 14:36:58 +08:00
|
|
|
self.call_native_fn(
|
|
|
|
global, caches, lib, setter, *hash_set, args, is_ref_mut,
|
|
|
|
false, pos, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
|
|
|
.or_else(
|
|
|
|
|err| match *err {
|
|
|
|
// Try an indexer if property does not exist
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorDotExpr(..) => {
|
2022-02-09 13:12:43 +08:00
|
|
|
let idx = &mut name.into();
|
|
|
|
let new_val = val;
|
|
|
|
self.call_indexer_set(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, target, idx, new_val,
|
2022-02-09 13:12:43 +08:00
|
|
|
is_ref_mut, level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
2022-02-24 10:36:20 +08:00
|
|
|
.or_else(|e| match *e {
|
2022-02-10 17:55:32 +08:00
|
|
|
// If there is no setter, no need to feed it
|
|
|
|
// back because the property is read-only
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorIndexingType(..) => {
|
2022-01-07 12:19:01 +08:00
|
|
|
Ok((Dynamic::UNIT, false))
|
|
|
|
}
|
2022-02-24 10:36:20 +08:00
|
|
|
_ => Err(e),
|
2022-01-07 12:19:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((result, may_be_changed))
|
|
|
|
}
|
|
|
|
// xxx.fn_name(arg_expr_list)[expr] | xxx.fn_name(arg_expr_list).expr
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(ref f, pos) if !f.is_qualified() => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-02-03 11:56:08 +08:00
|
|
|
let reset_debugger = self.run_debugger_with_reset(
|
2022-04-16 23:32:14 +08:00
|
|
|
scope, global, lib, this_ptr, _node, level,
|
2022-02-03 11:56:08 +08:00
|
|
|
)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-06-08 09:19:21 +08:00
|
|
|
let crate::ast::FnCallExpr {
|
|
|
|
name, hashes, args, ..
|
2022-07-05 16:26:38 +08:00
|
|
|
} = &**f;
|
2022-06-08 09:19:21 +08:00
|
|
|
let rhs_chain = rhs.into();
|
|
|
|
|
2022-06-08 16:34:56 +08:00
|
|
|
let offset = idx_values.len() - args.len();
|
|
|
|
let call_args = &mut idx_values[offset..];
|
|
|
|
let pos1 = args.get(0).map_or(Position::NONE, Expr::position);
|
2022-06-08 09:19:21 +08:00
|
|
|
|
2022-01-25 12:24:30 +08:00
|
|
|
let result = self.make_method_call(
|
2022-06-08 16:34:56 +08:00
|
|
|
global, caches, lib, name, *hashes, target, call_args, pos1,
|
|
|
|
pos, level,
|
2022-01-25 12:24:30 +08:00
|
|
|
);
|
|
|
|
|
2022-06-08 16:34:56 +08:00
|
|
|
idx_values.truncate(offset);
|
|
|
|
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
|
|
|
global.debugger.reset_status(reset_debugger);
|
|
|
|
|
2022-02-24 10:36:20 +08:00
|
|
|
let (val, _) = &mut result?;
|
|
|
|
let val = &mut val.into();
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
self.eval_dot_index_chain_helper(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, this_ptr, val, root, rhs, &x.rhs,
|
|
|
|
*options, idx_values, rhs_chain, level, new_val,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
|
|
|
.map_err(|err| err.fill_position(pos))
|
|
|
|
}
|
|
|
|
// xxx.module::fn_name(...) - syntax error
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(..) => unreachable!(
|
2022-01-07 12:19:01 +08:00
|
|
|
"function call in dot chain should not be namespace-qualified"
|
|
|
|
),
|
|
|
|
// Others - syntax error
|
|
|
|
ref expr => unreachable!("invalid dot expression: {:?}", expr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Syntax error
|
2022-02-04 12:04:33 +08:00
|
|
|
_ => Err(ERR::ErrorDotExpr("".into(), rhs.start_position()).into()),
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate a dot/index chain.
|
|
|
|
pub(crate) fn eval_dot_index_chain(
|
|
|
|
&self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
global: &mut GlobalRuntimeState,
|
2022-04-16 16:36:53 +08:00
|
|
|
caches: &mut Caches,
|
2022-01-07 12:19:01 +08:00
|
|
|
lib: &[&Module],
|
|
|
|
this_ptr: &mut Option<&mut Dynamic>,
|
|
|
|
expr: &Expr,
|
|
|
|
level: usize,
|
2022-09-27 23:04:22 +08:00
|
|
|
new_val: &mut Option<(Dynamic, &OpAssignment)>,
|
2022-01-07 12:19:01 +08:00
|
|
|
) -> RhaiResult {
|
2022-06-08 17:06:49 +08:00
|
|
|
let chain_type = ChainType::from(expr);
|
|
|
|
let (crate::ast::BinaryExpr { lhs, rhs }, options, op_pos) = match expr {
|
2022-01-07 12:19:01 +08:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-07-05 16:26:38 +08:00
|
|
|
Expr::Index(x, options, pos) => (&**x, *options, *pos),
|
2022-01-07 12:19:01 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-07-05 16:26:38 +08:00
|
|
|
Expr::Dot(x, options, pos) => (&**x, *options, *pos),
|
2022-01-07 12:19:01 +08:00
|
|
|
expr => unreachable!("Expr::Index or Expr::Dot expected but gets {:?}", expr),
|
|
|
|
};
|
|
|
|
|
2022-06-08 09:19:21 +08:00
|
|
|
let idx_values = &mut FnArgsVec::new_const();
|
2022-01-07 12:19:01 +08:00
|
|
|
|
2022-06-07 20:38:05 +08:00
|
|
|
match rhs {
|
|
|
|
// Short-circuit for simple property access: {expr}.prop
|
2022-06-07 20:52:04 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-06-08 09:19:21 +08:00
|
|
|
Expr::Property(..) if chain_type == ChainType::Dotting => (),
|
2022-06-07 20:52:04 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-06-07 20:38:05 +08:00
|
|
|
Expr::Property(..) => unreachable!("unexpected Expr::Property for indexing"),
|
|
|
|
// Short-circuit for simple method call: {expr}.func()
|
2022-06-07 20:52:04 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-06-08 09:19:21 +08:00
|
|
|
Expr::FnCall(x, ..) if chain_type == ChainType::Dotting && x.args.is_empty() => (),
|
2022-06-07 20:38:05 +08:00
|
|
|
// Short-circuit for method call with all literal arguments: {expr}.func(1, 2, 3)
|
2022-06-07 20:52:04 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-06-07 20:38:05 +08:00
|
|
|
Expr::FnCall(x, ..)
|
|
|
|
if chain_type == ChainType::Dotting && x.args.iter().all(Expr::is_constant) =>
|
|
|
|
{
|
2022-06-08 16:34:56 +08:00
|
|
|
idx_values.extend(x.args.iter().map(|expr| expr.get_literal_value().unwrap()))
|
|
|
|
}
|
|
|
|
// Short-circuit for indexing with literal: {expr}[1]
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
_ if chain_type == ChainType::Indexing && rhs.is_constant() => {
|
|
|
|
idx_values.push(rhs.get_literal_value().unwrap())
|
2022-06-07 20:38:05 +08:00
|
|
|
}
|
2022-06-08 16:34:56 +08:00
|
|
|
// All other patterns - evaluate the arguments chain
|
2022-06-07 20:38:05 +08:00
|
|
|
_ => {
|
|
|
|
self.eval_dot_index_chain_arguments(
|
2022-09-27 23:04:22 +08:00
|
|
|
scope, global, caches, lib, this_ptr, rhs, options, chain_type, idx_values,
|
2022-06-07 20:38:05 +08:00
|
|
|
level,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
match lhs {
|
|
|
|
// id.??? or id[???]
|
2022-03-05 17:57:23 +08:00
|
|
|
Expr::Variable(x, .., var_pos) => {
|
2022-01-25 12:24:30 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-04-16 23:32:14 +08:00
|
|
|
self.run_debugger(scope, global, lib, this_ptr, lhs, level)?;
|
2022-01-25 12:24:30 +08:00
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.inc_operations(&mut global.num_operations, *var_pos)?;
|
|
|
|
|
2022-02-08 09:02:15 +08:00
|
|
|
let (mut target, ..) =
|
2022-04-16 23:32:14 +08:00
|
|
|
self.search_namespace(scope, global, lib, this_ptr, lhs, level)?;
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
let obj_ptr = &mut target;
|
2022-03-05 17:57:23 +08:00
|
|
|
let root = (x.3.as_str(), *var_pos);
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
self.eval_dot_index_chain_helper(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, &mut None, obj_ptr, root, expr, rhs, options, idx_values,
|
2022-01-07 12:19:01 +08:00
|
|
|
chain_type, level, new_val,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// {expr}.??? = ??? or {expr}[???] = ???
|
2022-06-07 20:38:05 +08:00
|
|
|
_ if new_val.is_some() => unreachable!("cannot assign to an expression"),
|
2022-01-07 12:19:01 +08:00
|
|
|
// {expr}.??? or {expr}[???]
|
|
|
|
expr => {
|
2022-06-07 11:31:46 +08:00
|
|
|
let value = self
|
|
|
|
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)?
|
|
|
|
.flatten();
|
2022-01-07 12:19:01 +08:00
|
|
|
let obj_ptr = &mut value.into();
|
2022-02-04 12:04:33 +08:00
|
|
|
let root = ("", expr.start_position());
|
2022-06-07 20:38:05 +08:00
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
self.eval_dot_index_chain_helper(
|
2022-04-16 16:36:53 +08:00
|
|
|
global, caches, lib, this_ptr, obj_ptr, root, expr, rhs, options, idx_values,
|
2022-01-25 12:24:30 +08:00
|
|
|
chain_type, level, new_val,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 20:38:05 +08:00
|
|
|
.map(|(v, ..)| v)
|
|
|
|
.map_err(|err| err.fill_position(op_pos))
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
|
2022-08-10 12:48:37 +08:00
|
|
|
/// Evaluate a chain of indexes and store the results in a [`FnArgsVec`].
|
2022-01-07 12:19:01 +08:00
|
|
|
fn eval_dot_index_chain_arguments(
|
|
|
|
&self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
global: &mut GlobalRuntimeState,
|
2022-04-16 16:36:53 +08:00
|
|
|
caches: &mut Caches,
|
2022-01-07 12:19:01 +08:00
|
|
|
lib: &[&Module],
|
|
|
|
this_ptr: &mut Option<&mut Dynamic>,
|
|
|
|
expr: &Expr,
|
2022-02-25 11:42:59 +08:00
|
|
|
parent_options: ASTFlags,
|
2022-04-11 16:29:16 +08:00
|
|
|
_parent_chain_type: ChainType,
|
2022-06-08 09:19:21 +08:00
|
|
|
idx_values: &mut FnArgsVec<Dynamic>,
|
2022-01-07 12:19:01 +08:00
|
|
|
level: usize,
|
|
|
|
) -> RhaiResultOf<()> {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.inc_operations(&mut global.num_operations, expr.position())?;
|
|
|
|
|
|
|
|
match expr {
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(x, ..)
|
2022-02-08 09:46:14 +08:00
|
|
|
if _parent_chain_type == ChainType::Dotting && !x.is_qualified() =>
|
|
|
|
{
|
2022-07-05 16:26:38 +08:00
|
|
|
for arg_expr in &x.args {
|
2022-06-08 16:34:56 +08:00
|
|
|
idx_values.push(
|
|
|
|
self.get_arg_value(scope, global, caches, lib, this_ptr, arg_expr, level)?
|
|
|
|
.0
|
|
|
|
.flatten(),
|
|
|
|
);
|
2022-06-08 09:19:21 +08:00
|
|
|
}
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(..) if _parent_chain_type == ChainType::Dotting => {
|
2022-01-07 12:19:01 +08:00
|
|
|
unreachable!("function call in dot chain should not be namespace-qualified")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-06-08 09:19:21 +08:00
|
|
|
Expr::Property(..) if _parent_chain_type == ChainType::Dotting => (),
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Property(..) => unreachable!("unexpected Expr::Property for indexing"),
|
2022-01-07 12:19:01 +08:00
|
|
|
|
2022-02-25 11:42:59 +08:00
|
|
|
Expr::Index(x, options, ..) | Expr::Dot(x, options, ..)
|
|
|
|
if !parent_options.contains(ASTFlags::BREAK) =>
|
|
|
|
{
|
2022-07-05 16:26:38 +08:00
|
|
|
let crate::ast::BinaryExpr { lhs, rhs, .. } = &**x;
|
2022-01-07 12:19:01 +08:00
|
|
|
|
2022-06-08 17:06:49 +08:00
|
|
|
let mut _arg_values = FnArgsVec::new_const();
|
2022-06-08 09:19:21 +08:00
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
// Evaluate in left-to-right order
|
2022-06-08 09:19:21 +08:00
|
|
|
match lhs {
|
2022-01-07 12:19:01 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-06-08 09:19:21 +08:00
|
|
|
Expr::Property(..) if _parent_chain_type == ChainType::Dotting => (),
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Property(..) => unreachable!("unexpected Expr::Property for indexing"),
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(x, ..)
|
2022-01-07 12:19:01 +08:00
|
|
|
if _parent_chain_type == ChainType::Dotting && !x.is_qualified() =>
|
|
|
|
{
|
2022-07-05 16:26:38 +08:00
|
|
|
for arg_expr in &x.args {
|
2022-06-08 17:06:49 +08:00
|
|
|
_arg_values.push(
|
2022-06-08 16:34:56 +08:00
|
|
|
self.get_arg_value(
|
|
|
|
scope, global, caches, lib, this_ptr, arg_expr, level,
|
|
|
|
)?
|
|
|
|
.0
|
|
|
|
.flatten(),
|
|
|
|
);
|
2022-06-08 09:19:21 +08:00
|
|
|
}
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-03-04 12:22:44 +08:00
|
|
|
Expr::MethodCall(..) if _parent_chain_type == ChainType::Dotting => {
|
2022-01-07 12:19:01 +08:00
|
|
|
unreachable!("function call in dot chain should not be namespace-qualified")
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
expr if _parent_chain_type == ChainType::Dotting => {
|
|
|
|
unreachable!("invalid dot expression: {:?}", expr);
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-06-08 09:19:21 +08:00
|
|
|
_ if _parent_chain_type == ChainType::Indexing => {
|
2022-06-08 17:06:49 +08:00
|
|
|
_arg_values.push(
|
2022-06-08 09:19:21 +08:00
|
|
|
self.eval_expr(scope, global, caches, lib, this_ptr, lhs, level)?
|
|
|
|
.flatten(),
|
|
|
|
);
|
|
|
|
}
|
2022-01-07 12:19:01 +08:00
|
|
|
expr => unreachable!("unknown chained expression: {:?}", expr),
|
2022-06-08 09:19:21 +08:00
|
|
|
}
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
// Push in reverse order
|
|
|
|
let chain_type = expr.into();
|
|
|
|
|
|
|
|
self.eval_dot_index_chain_arguments(
|
2022-04-16 16:36:53 +08:00
|
|
|
scope, global, caches, lib, this_ptr, rhs, *options, chain_type, idx_values,
|
2022-09-27 23:04:22 +08:00
|
|
|
level,
|
2022-01-07 12:19:01 +08:00
|
|
|
)?;
|
|
|
|
|
2022-06-08 17:06:49 +08:00
|
|
|
if !_arg_values.is_empty() {
|
|
|
|
idx_values.extend(_arg_values);
|
|
|
|
}
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
_ if _parent_chain_type == ChainType::Dotting => {
|
|
|
|
unreachable!("invalid dot expression: {:?}", expr);
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
_ if _parent_chain_type == ChainType::Indexing => idx_values.push(
|
2022-06-08 09:19:21 +08:00
|
|
|
self.eval_expr(scope, global, caches, lib, this_ptr, expr, level)?
|
|
|
|
.flatten(),
|
2022-01-07 12:19:01 +08:00
|
|
|
),
|
|
|
|
_ => unreachable!("unknown chained expression: {:?}", expr),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-09 13:12:43 +08:00
|
|
|
/// Call a get indexer.
|
|
|
|
#[inline(always)]
|
|
|
|
fn call_indexer_get(
|
|
|
|
&self,
|
|
|
|
global: &mut GlobalRuntimeState,
|
2022-04-16 16:36:53 +08:00
|
|
|
caches: &mut Caches,
|
2022-02-09 13:12:43 +08:00
|
|
|
lib: &[&Module],
|
|
|
|
target: &mut Dynamic,
|
|
|
|
idx: &mut Dynamic,
|
|
|
|
level: usize,
|
2022-04-14 22:55:39 +08:00
|
|
|
) -> RhaiResultOf<Dynamic> {
|
2022-02-09 13:12:43 +08:00
|
|
|
let args = &mut [target, idx];
|
2022-05-19 14:36:58 +08:00
|
|
|
let hash = global.hash_idx_get();
|
2022-02-09 13:12:43 +08:00
|
|
|
let fn_name = crate::engine::FN_IDX_GET;
|
|
|
|
let pos = Position::NONE;
|
2022-05-19 14:36:58 +08:00
|
|
|
let level = level + 1;
|
2022-02-09 13:12:43 +08:00
|
|
|
|
2022-05-19 14:36:58 +08:00
|
|
|
self.call_native_fn(
|
|
|
|
global, caches, lib, fn_name, hash, args, true, false, pos, level,
|
2022-02-09 13:12:43 +08:00
|
|
|
)
|
2022-04-14 22:55:39 +08:00
|
|
|
.map(|(r, ..)| r)
|
2022-02-09 13:12:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Call a set indexer.
|
|
|
|
#[inline(always)]
|
|
|
|
fn call_indexer_set(
|
|
|
|
&self,
|
|
|
|
global: &mut GlobalRuntimeState,
|
2022-04-16 16:36:53 +08:00
|
|
|
caches: &mut Caches,
|
2022-02-09 13:12:43 +08:00
|
|
|
lib: &[&Module],
|
|
|
|
target: &mut Dynamic,
|
|
|
|
idx: &mut Dynamic,
|
|
|
|
new_val: &mut Dynamic,
|
|
|
|
is_ref_mut: bool,
|
|
|
|
level: usize,
|
|
|
|
) -> RhaiResultOf<(Dynamic, bool)> {
|
2022-05-19 14:36:58 +08:00
|
|
|
let hash = global.hash_idx_set();
|
2022-02-09 13:12:43 +08:00
|
|
|
let args = &mut [target, idx, new_val];
|
|
|
|
let fn_name = crate::engine::FN_IDX_SET;
|
|
|
|
let pos = Position::NONE;
|
2022-05-19 14:36:58 +08:00
|
|
|
let level = level + 1;
|
2022-02-09 13:12:43 +08:00
|
|
|
|
2022-05-19 14:36:58 +08:00
|
|
|
self.call_native_fn(
|
|
|
|
global, caches, lib, fn_name, hash, args, is_ref_mut, false, pos, level,
|
2022-02-09 13:12:43 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
/// Get the value at the indexed position of a base type.
|
2022-08-10 12:48:37 +08:00
|
|
|
/// [`Position`] in [`EvalAltResult`][crate::EvalAltResult] may be [`NONE`][Position::NONE] and should be set afterwards.
|
2022-01-07 12:19:01 +08:00
|
|
|
fn get_indexed_mut<'t>(
|
|
|
|
&self,
|
|
|
|
global: &mut GlobalRuntimeState,
|
2022-04-16 16:36:53 +08:00
|
|
|
caches: &mut Caches,
|
2022-01-07 12:19:01 +08:00
|
|
|
lib: &[&Module],
|
|
|
|
target: &'t mut Dynamic,
|
2022-09-27 23:04:22 +08:00
|
|
|
idx: &mut Dynamic,
|
2022-02-10 17:55:32 +08:00
|
|
|
idx_pos: Position,
|
2022-04-11 16:29:16 +08:00
|
|
|
_add_if_not_found: bool,
|
2022-01-07 12:19:01 +08:00
|
|
|
use_indexers: bool,
|
|
|
|
level: usize,
|
|
|
|
) -> RhaiResultOf<Target<'t>> {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.inc_operations(&mut global.num_operations, Position::NONE)?;
|
|
|
|
|
|
|
|
match target {
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Dynamic(Union::Array(arr, ..)) => {
|
2022-01-07 12:19:01 +08:00
|
|
|
// val_array[idx]
|
|
|
|
let index = idx
|
|
|
|
.as_int()
|
2022-02-10 17:55:32 +08:00
|
|
|
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, idx_pos))?;
|
2022-01-13 18:13:27 +08:00
|
|
|
let len = arr.len();
|
2022-01-16 22:54:28 +08:00
|
|
|
let arr_idx = super::calc_index(len, index, true, || {
|
2022-02-10 17:55:32 +08:00
|
|
|
ERR::ErrorArrayBounds(len, index, idx_pos).into()
|
2022-01-15 10:24:08 +08:00
|
|
|
})?;
|
2022-01-07 12:19:01 +08:00
|
|
|
|
2022-01-13 18:13:27 +08:00
|
|
|
Ok(arr.get_mut(arr_idx).map(Target::from).unwrap())
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Dynamic(Union::Blob(arr, ..)) => {
|
2022-01-07 12:19:01 +08:00
|
|
|
// val_blob[idx]
|
|
|
|
let index = idx
|
|
|
|
.as_int()
|
2022-02-10 17:55:32 +08:00
|
|
|
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, idx_pos))?;
|
2022-01-13 18:13:27 +08:00
|
|
|
let len = arr.len();
|
2022-01-16 22:54:28 +08:00
|
|
|
let arr_idx = super::calc_index(len, index, true, || {
|
2022-02-10 17:55:32 +08:00
|
|
|
ERR::ErrorArrayBounds(len, index, idx_pos).into()
|
2022-01-15 10:24:08 +08:00
|
|
|
})?;
|
2022-01-07 12:19:01 +08:00
|
|
|
|
2022-01-13 18:13:27 +08:00
|
|
|
let value = arr.get(arr_idx).map(|&v| (v as crate::INT).into()).unwrap();
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
Ok(Target::BlobByte {
|
|
|
|
source: target,
|
|
|
|
value,
|
|
|
|
index: arr_idx,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Dynamic(Union::Map(map, ..)) => {
|
2022-01-07 12:19:01 +08:00
|
|
|
// val_map[idx]
|
|
|
|
let index = idx.read_lock::<crate::ImmutableString>().ok_or_else(|| {
|
2022-02-10 17:55:32 +08:00
|
|
|
self.make_type_mismatch_err::<crate::ImmutableString>(idx.type_name(), idx_pos)
|
2022-01-07 12:19:01 +08:00
|
|
|
})?;
|
|
|
|
|
2022-03-03 13:02:57 +08:00
|
|
|
if _add_if_not_found && (map.is_empty() || !map.contains_key(index.as_str())) {
|
2022-01-07 12:19:01 +08:00
|
|
|
map.insert(index.clone().into(), Dynamic::UNIT);
|
|
|
|
}
|
|
|
|
|
2022-08-27 16:26:41 +08:00
|
|
|
map.get_mut(index.as_str()).map_or_else(
|
|
|
|
|| {
|
|
|
|
if self.fail_on_invalid_map_property() {
|
|
|
|
Err(ERR::ErrorPropertyNotFound(index.to_string(), idx_pos).into())
|
|
|
|
} else {
|
|
|
|
Ok(Target::from(Dynamic::UNIT))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|value| Ok(Target::from(value)),
|
|
|
|
)
|
2022-01-07 12:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Dynamic(Union::Int(value, ..))
|
2022-01-07 12:19:01 +08:00
|
|
|
if idx.is::<crate::ExclusiveRange>() || idx.is::<crate::InclusiveRange>() =>
|
|
|
|
{
|
|
|
|
// val_int[range]
|
|
|
|
let (shift, mask) = if let Some(range) = idx.read_lock::<crate::ExclusiveRange>() {
|
|
|
|
let start = range.start;
|
|
|
|
let end = range.end;
|
|
|
|
|
2022-03-09 09:25:55 +08:00
|
|
|
let start = super::calc_index(crate::INT_BITS, start, false, || {
|
|
|
|
ERR::ErrorBitFieldBounds(crate::INT_BITS, start, idx_pos).into()
|
2022-01-13 18:13:27 +08:00
|
|
|
})?;
|
2022-03-09 09:25:55 +08:00
|
|
|
let end = super::calc_index(crate::INT_BITS, end, false, || {
|
|
|
|
ERR::ErrorBitFieldBounds(crate::INT_BITS, end, idx_pos).into()
|
2022-01-13 18:13:27 +08:00
|
|
|
})?;
|
|
|
|
|
|
|
|
if end <= start {
|
2022-01-07 12:19:01 +08:00
|
|
|
(0, 0)
|
2022-03-09 09:25:55 +08:00
|
|
|
} else if end == crate::INT_BITS && start == 0 {
|
2022-01-07 12:19:01 +08:00
|
|
|
// -1 = all bits set
|
|
|
|
(0, -1)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
start as u8,
|
|
|
|
// 2^bits - 1
|
2022-01-13 22:51:56 +08:00
|
|
|
(((2 as crate::UNSIGNED_INT).pow((end - start) as u32) - 1)
|
|
|
|
as crate::INT)
|
2022-01-13 18:13:27 +08:00
|
|
|
<< start,
|
2022-01-07 12:19:01 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if let Some(range) = idx.read_lock::<crate::InclusiveRange>() {
|
|
|
|
let start = *range.start();
|
|
|
|
let end = *range.end();
|
|
|
|
|
2022-03-09 09:25:55 +08:00
|
|
|
let start = super::calc_index(crate::INT_BITS, start, false, || {
|
|
|
|
ERR::ErrorBitFieldBounds(crate::INT_BITS, start, idx_pos).into()
|
2022-01-13 18:13:27 +08:00
|
|
|
})?;
|
2022-03-09 09:25:55 +08:00
|
|
|
let end = super::calc_index(crate::INT_BITS, end, false, || {
|
|
|
|
ERR::ErrorBitFieldBounds(crate::INT_BITS, end, idx_pos).into()
|
2022-01-13 18:13:27 +08:00
|
|
|
})?;
|
|
|
|
|
|
|
|
if end < start {
|
2022-01-07 12:19:01 +08:00
|
|
|
(0, 0)
|
2022-03-09 09:25:55 +08:00
|
|
|
} else if end == crate::INT_BITS - 1 && start == 0 {
|
2022-01-07 12:19:01 +08:00
|
|
|
// -1 = all bits set
|
|
|
|
(0, -1)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
start as u8,
|
|
|
|
// 2^bits - 1
|
2022-01-13 22:51:56 +08:00
|
|
|
(((2 as crate::UNSIGNED_INT).pow((end - start + 1) as u32) - 1)
|
|
|
|
as crate::INT)
|
2022-01-07 12:19:01 +08:00
|
|
|
<< start,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!("Range or RangeInclusive expected but gets {:?}", idx);
|
|
|
|
};
|
|
|
|
|
|
|
|
let field_value = (*value & mask) >> shift;
|
|
|
|
|
|
|
|
Ok(Target::BitField {
|
|
|
|
source: target,
|
|
|
|
value: field_value.into(),
|
|
|
|
mask,
|
|
|
|
shift,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Dynamic(Union::Int(value, ..)) => {
|
2022-01-07 12:19:01 +08:00
|
|
|
// val_int[idx]
|
|
|
|
let index = idx
|
|
|
|
.as_int()
|
2022-02-10 17:55:32 +08:00
|
|
|
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, idx_pos))?;
|
2022-01-07 12:19:01 +08:00
|
|
|
|
2022-03-09 09:25:55 +08:00
|
|
|
let bit = super::calc_index(crate::INT_BITS, index, true, || {
|
|
|
|
ERR::ErrorBitFieldBounds(crate::INT_BITS, index, idx_pos).into()
|
2022-01-13 18:13:27 +08:00
|
|
|
})?;
|
|
|
|
|
|
|
|
let bit_value = (*value & (1 << bit)) != 0;
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
Ok(Target::Bit {
|
|
|
|
source: target,
|
|
|
|
value: bit_value.into(),
|
2022-01-13 18:13:27 +08:00
|
|
|
bit: bit as u8,
|
2022-01-07 12:19:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Dynamic(Union::Str(s, ..)) => {
|
2022-01-07 12:19:01 +08:00
|
|
|
// val_string[idx]
|
|
|
|
let index = idx
|
|
|
|
.as_int()
|
2022-02-10 17:55:32 +08:00
|
|
|
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, idx_pos))?;
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
let (ch, offset) = if index >= 0 {
|
2022-08-27 16:26:41 +08:00
|
|
|
if index >= crate::MAX_USIZE_INT {
|
|
|
|
return Err(
|
|
|
|
ERR::ErrorStringBounds(s.chars().count(), index, idx_pos).into()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:19:01 +08:00
|
|
|
let offset = index as usize;
|
|
|
|
(
|
|
|
|
s.chars().nth(offset).ok_or_else(|| {
|
2022-08-27 16:26:41 +08:00
|
|
|
ERR::ErrorStringBounds(s.chars().count(), index, idx_pos)
|
2022-01-07 12:19:01 +08:00
|
|
|
})?,
|
|
|
|
offset,
|
|
|
|
)
|
2022-07-27 16:04:24 +08:00
|
|
|
} else {
|
2022-08-27 16:26:41 +08:00
|
|
|
let abs_index = index.unsigned_abs();
|
|
|
|
|
2022-08-29 14:27:05 +08:00
|
|
|
if abs_index as u64 > usize::MAX as u64 {
|
2022-08-27 16:26:41 +08:00
|
|
|
return Err(
|
|
|
|
ERR::ErrorStringBounds(s.chars().count(), index, idx_pos).into()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let offset = abs_index as usize;
|
2022-01-07 12:19:01 +08:00
|
|
|
(
|
|
|
|
// Count from end if negative
|
|
|
|
s.chars().rev().nth(offset - 1).ok_or_else(|| {
|
2022-08-27 16:26:41 +08:00
|
|
|
ERR::ErrorStringBounds(s.chars().count(), index, idx_pos)
|
2022-01-07 12:19:01 +08:00
|
|
|
})?,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Target::StringChar {
|
|
|
|
source: target,
|
|
|
|
value: ch.into(),
|
|
|
|
index: offset,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-07 11:31:46 +08:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
Dynamic(Union::Shared(..)) => {
|
|
|
|
unreachable!("`get_indexed_mut` cannot handle shared values")
|
|
|
|
}
|
|
|
|
|
2022-02-09 13:12:43 +08:00
|
|
|
_ if use_indexers => self
|
2022-09-27 23:04:22 +08:00
|
|
|
.call_indexer_get(global, caches, lib, target, idx, level)
|
2022-04-14 22:55:39 +08:00
|
|
|
.map(Into::into),
|
2022-01-07 12:19:01 +08:00
|
|
|
|
|
|
|
_ => Err(ERR::ErrorIndexingType(
|
|
|
|
format!(
|
|
|
|
"{} [{}]",
|
|
|
|
self.map_type_name(target.type_name()),
|
|
|
|
self.map_type_name(idx.type_name())
|
|
|
|
),
|
|
|
|
Position::NONE,
|
|
|
|
)
|
|
|
|
.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|