Fixes in Engine to properly interpret Shared Dynamic
This commit is contained in:
parent
e5fe222de3
commit
aa87a7f5ef
41
src/any.rs
41
src/any.rs
@ -18,7 +18,7 @@ use crate::stdlib::{
|
|||||||
boxed::Box,
|
boxed::Box,
|
||||||
fmt,
|
fmt,
|
||||||
string::String,
|
string::String,
|
||||||
ops::{Deref, DerefMut}
|
ops::{DerefMut, Deref}
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
@ -259,11 +259,13 @@ impl Dynamic {
|
|||||||
/// If the `Dynamic` is a Shared variant checking is performed on
|
/// If the `Dynamic` is a Shared variant checking is performed on
|
||||||
/// top of it's internal value.
|
/// top of it's internal value.
|
||||||
pub fn is<T: Variant + Clone>(&self) -> bool {
|
pub fn is<T: Variant + Clone>(&self) -> bool {
|
||||||
self.type_id() == TypeId::of::<T>()
|
let mut target_type_id = TypeId::of::<T>();
|
||||||
|| match self.0 {
|
|
||||||
Union::Str(_) => TypeId::of::<String>() == TypeId::of::<T>(),
|
if target_type_id == TypeId::of::<String>() {
|
||||||
_ => false,
|
target_type_id = TypeId::of::<ImmutableString>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.type_id() == target_type_id
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the TypeId of the value held by this `Dynamic`.
|
/// Get the TypeId of the value held by this `Dynamic`.
|
||||||
@ -606,6 +608,7 @@ impl Dynamic {
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
if type_id == TypeId::of::<FLOAT>() {
|
if type_id == TypeId::of::<FLOAT>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
@ -613,30 +616,35 @@ impl Dynamic {
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if type_id == TypeId::of::<bool>() {
|
if type_id == TypeId::of::<bool>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
Union::Bool(value) => unsafe_try_cast(value),
|
Union::Bool(value) => unsafe_try_cast(value),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if type_id == TypeId::of::<ImmutableString>() {
|
if type_id == TypeId::of::<ImmutableString>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
Union::Str(value) => unsafe_try_cast(value),
|
Union::Str(value) => unsafe_try_cast(value),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if type_id == TypeId::of::<String>() {
|
if type_id == TypeId::of::<String>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
Union::Str(value) => unsafe_try_cast(value.into_owned()),
|
Union::Str(value) => unsafe_try_cast(value.into_owned()),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if type_id == TypeId::of::<char>() {
|
if type_id == TypeId::of::<char>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
Union::Char(value) => unsafe_try_cast(value),
|
Union::Char(value) => unsafe_try_cast(value),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
if type_id == TypeId::of::<Array>() {
|
if type_id == TypeId::of::<Array>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
@ -644,6 +652,7 @@ impl Dynamic {
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
if type_id == TypeId::of::<Map>() {
|
if type_id == TypeId::of::<Map>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
@ -651,12 +660,14 @@ impl Dynamic {
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if type_id == TypeId::of::<FnPtr>() {
|
if type_id == TypeId::of::<FnPtr>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
Union::FnPtr(value) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v),
|
Union::FnPtr(value) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if type_id == TypeId::of::<()>() {
|
if type_id == TypeId::of::<()>() {
|
||||||
return match self.0 {
|
return match self.0 {
|
||||||
Union::Unit(value) => unsafe_try_cast(value),
|
Union::Unit(value) => unsafe_try_cast(value),
|
||||||
@ -951,6 +962,7 @@ impl Dynamic {
|
|||||||
pub fn as_int(&self) -> Result<INT, &'static str> {
|
pub fn as_int(&self) -> Result<INT, &'static str> {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Int(n) => Ok(n),
|
Union::Int(n) => Ok(n),
|
||||||
|
Union::Shared(_) => self.read::<INT>().ok_or_else(|| self.type_name()),
|
||||||
_ => Err(self.type_name()),
|
_ => Err(self.type_name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -961,6 +973,7 @@ impl Dynamic {
|
|||||||
pub fn as_float(&self) -> Result<FLOAT, &'static str> {
|
pub fn as_float(&self) -> Result<FLOAT, &'static str> {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Float(n) => Ok(n),
|
Union::Float(n) => Ok(n),
|
||||||
|
Union::Shared(_) => self.read::<FLOAT>().ok_or_else(|| self.type_name()),
|
||||||
_ => Err(self.type_name()),
|
_ => Err(self.type_name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -970,6 +983,7 @@ impl Dynamic {
|
|||||||
pub fn as_bool(&self) -> Result<bool, &'static str> {
|
pub fn as_bool(&self) -> Result<bool, &'static str> {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Bool(b) => Ok(b),
|
Union::Bool(b) => Ok(b),
|
||||||
|
Union::Shared(_) => self.read::<bool>().ok_or_else(|| self.type_name()),
|
||||||
_ => Err(self.type_name()),
|
_ => Err(self.type_name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -979,12 +993,15 @@ impl Dynamic {
|
|||||||
pub fn as_char(&self) -> Result<char, &'static str> {
|
pub fn as_char(&self) -> Result<char, &'static str> {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Char(n) => Ok(n),
|
Union::Char(n) => Ok(n),
|
||||||
|
Union::Shared(_) => self.read::<char>().ok_or_else(|| self.type_name()),
|
||||||
_ => Err(self.type_name()),
|
_ => Err(self.type_name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cast the `Dynamic` as a string and return the string slice.
|
/// Cast the `Dynamic` as a string and return the string slice.
|
||||||
/// Returns the name of the actual type if the cast fails.
|
/// Returns the name of the actual type if the cast fails.
|
||||||
|
///
|
||||||
|
/// Cast is failing if `self` is Shared Dynamic
|
||||||
pub fn as_str(&self) -> Result<&str, &'static str> {
|
pub fn as_str(&self) -> Result<&str, &'static str> {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
Union::Str(s) => Ok(s),
|
Union::Str(s) => Ok(s),
|
||||||
@ -1006,6 +1023,20 @@ impl Dynamic {
|
|||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Str(s) => Ok(s),
|
Union::Str(s) => Ok(s),
|
||||||
Union::FnPtr(f) => Ok(f.take_data().0),
|
Union::FnPtr(f) => Ok(f.take_data().0),
|
||||||
|
Union::Shared(cell) => {
|
||||||
|
#[cfg(not(feature = "sync"))]
|
||||||
|
match &cell.container.borrow().deref().0 {
|
||||||
|
Union::Str(s) => Ok(s.clone()),
|
||||||
|
Union::FnPtr(f) => Ok(f.clone().take_data().0),
|
||||||
|
_ => Err(cell.value_type_name),
|
||||||
|
}
|
||||||
|
#[cfg(feature = "sync")]
|
||||||
|
match &cell.container.read().deref().0 {
|
||||||
|
Union::Str(s) => Ok(s.clone()),
|
||||||
|
Union::FnPtr(f) => Ok(f.clone().take_data().0),
|
||||||
|
_ => Err(cell.value_type_name),
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => Err(self.type_name()),
|
_ => Err(self.type_name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Main module defining the script evaluation `Engine`.
|
//! Main module defining the script evaluation `Engine`.
|
||||||
|
|
||||||
use crate::any::{map_std_type_name, Dynamic, Union};
|
use crate::any::{map_std_type_name, Dynamic, Union, DynamicWriteLock};
|
||||||
use crate::calc_fn_hash;
|
use crate::calc_fn_hash;
|
||||||
use crate::fn_call::run_builtin_op_assignment;
|
use crate::fn_call::run_builtin_op_assignment;
|
||||||
use crate::fn_native::{CallableFunction, Callback, FnPtr};
|
use crate::fn_native::{CallableFunction, Callback, FnPtr};
|
||||||
@ -39,6 +39,7 @@ use crate::stdlib::{
|
|||||||
iter::{empty, once},
|
iter::{empty, once},
|
||||||
string::{String, ToString},
|
string::{String, ToString},
|
||||||
vec::Vec,
|
vec::Vec,
|
||||||
|
ops::DerefMut,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -123,6 +124,9 @@ pub enum ChainType {
|
|||||||
pub enum Target<'a> {
|
pub enum Target<'a> {
|
||||||
/// The target is a mutable reference to a `Dynamic` value somewhere.
|
/// The target is a mutable reference to a `Dynamic` value somewhere.
|
||||||
Ref(&'a mut Dynamic),
|
Ref(&'a mut Dynamic),
|
||||||
|
/// The target is a mutable reference to a Shared `Dynamic` value.
|
||||||
|
/// It holds the access guard and the original container both for cloning purposes
|
||||||
|
LockGuard((DynamicWriteLock<'a, Dynamic>, Dynamic)),
|
||||||
/// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects).
|
/// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects).
|
||||||
Value(Dynamic),
|
Value(Dynamic),
|
||||||
/// The target is a character inside a String.
|
/// The target is a character inside a String.
|
||||||
@ -137,6 +141,7 @@ impl Target<'_> {
|
|||||||
pub fn is_ref(&self) -> bool {
|
pub fn is_ref(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Ref(_) => true,
|
Self::Ref(_) => true,
|
||||||
|
Self::LockGuard(_) => true,
|
||||||
Self::Value(_) => false,
|
Self::Value(_) => false,
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Self::StringChar(_, _, _) => false,
|
Self::StringChar(_, _, _) => false,
|
||||||
@ -146,6 +151,7 @@ impl Target<'_> {
|
|||||||
pub fn is_value(&self) -> bool {
|
pub fn is_value(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Ref(_) => false,
|
Self::Ref(_) => false,
|
||||||
|
Self::LockGuard(_) => false,
|
||||||
Self::Value(_) => true,
|
Self::Value(_) => true,
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Self::StringChar(_, _, _) => false,
|
Self::StringChar(_, _, _) => false,
|
||||||
@ -156,6 +162,7 @@ impl Target<'_> {
|
|||||||
pub fn is<T: Variant + Clone>(&self) -> bool {
|
pub fn is<T: Variant + Clone>(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Target::Ref(r) => r.is::<T>(),
|
Target::Ref(r) => r.is::<T>(),
|
||||||
|
Target::LockGuard((r, _)) => r.is::<T>(),
|
||||||
Target::Value(r) => r.is::<T>(),
|
Target::Value(r) => r.is::<T>(),
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Target::StringChar(_, _, _) => TypeId::of::<T>() == TypeId::of::<char>(),
|
Target::StringChar(_, _, _) => TypeId::of::<T>() == TypeId::of::<char>(),
|
||||||
@ -165,6 +172,7 @@ impl Target<'_> {
|
|||||||
pub fn clone_into_dynamic(self) -> Dynamic {
|
pub fn clone_into_dynamic(self) -> Dynamic {
|
||||||
match self {
|
match self {
|
||||||
Self::Ref(r) => r.clone(), // Referenced value is cloned
|
Self::Ref(r) => r.clone(), // Referenced value is cloned
|
||||||
|
Self::LockGuard((_, orig)) => orig, // Return original container of the Shared Dynamic
|
||||||
Self::Value(v) => v, // Owned value is simply taken
|
Self::Value(v) => v, // Owned value is simply taken
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Self::StringChar(_, _, ch) => ch, // Character is taken
|
Self::StringChar(_, _, ch) => ch, // Character is taken
|
||||||
@ -174,6 +182,7 @@ impl Target<'_> {
|
|||||||
pub fn as_mut(&mut self) -> &mut Dynamic {
|
pub fn as_mut(&mut self) -> &mut Dynamic {
|
||||||
match self {
|
match self {
|
||||||
Self::Ref(r) => *r,
|
Self::Ref(r) => *r,
|
||||||
|
Self::LockGuard((r, _)) => r.deref_mut(),
|
||||||
Self::Value(ref mut r) => r,
|
Self::Value(ref mut r) => r,
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Self::StringChar(_, _, ref mut r) => r,
|
Self::StringChar(_, _, ref mut r) => r,
|
||||||
@ -184,13 +193,16 @@ impl Target<'_> {
|
|||||||
pub fn set_value(&mut self, new_val: Dynamic) -> Result<(), Box<EvalAltResult>> {
|
pub fn set_value(&mut self, new_val: Dynamic) -> Result<(), Box<EvalAltResult>> {
|
||||||
match self {
|
match self {
|
||||||
Self::Ref(r) => **r = new_val,
|
Self::Ref(r) => **r = new_val,
|
||||||
|
Self::LockGuard((r, _)) => **r = new_val,
|
||||||
Self::Value(_) => {
|
Self::Value(_) => {
|
||||||
return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
||||||
Position::none(),
|
Position::none(),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Self::StringChar(Dynamic(Union::Str(ref mut s)), index, _) => {
|
Self::StringChar(string, index, _) if string.is::<ImmutableString>() => {
|
||||||
|
let mut s = string.write_lock::<ImmutableString>().unwrap();
|
||||||
|
|
||||||
// Replace the character at the specified index position
|
// Replace the character at the specified index position
|
||||||
let new_ch = new_val
|
let new_ch = new_val
|
||||||
.as_char()
|
.as_char()
|
||||||
@ -216,9 +228,15 @@ impl Target<'_> {
|
|||||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||||
impl<'a> From<&'a mut Dynamic> for Target<'a> {
|
impl<'a> From<&'a mut Dynamic> for Target<'a> {
|
||||||
fn from(value: &'a mut Dynamic) -> Self {
|
fn from(value: &'a mut Dynamic) -> Self {
|
||||||
|
if value.is_shared() {
|
||||||
|
// clone is cheap since it holds Arc/Rw under the hood
|
||||||
|
let container = value.clone();
|
||||||
|
Self::LockGuard((value.write_lock::<Dynamic>().unwrap(), container))
|
||||||
|
} else {
|
||||||
Self::Ref(value)
|
Self::Ref(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||||
impl<T: Into<Dynamic>> From<T> for Target<'_> {
|
impl<T: Into<Dynamic>> From<T> for Target<'_> {
|
||||||
fn from(value: T) -> Self {
|
fn from(value: T) -> Self {
|
||||||
@ -676,15 +694,46 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
// xxx[rhs] = new_val
|
// xxx[rhs] = new_val
|
||||||
_ if _new_val.is_some() => {
|
_ if _new_val.is_some() => {
|
||||||
let mut new_val = _new_val.unwrap();
|
|
||||||
let mut idx_val2 = idx_val.clone();
|
let mut idx_val2 = idx_val.clone();
|
||||||
|
|
||||||
|
// `next` is introduced to bypass double mutable borrowing of target
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
let mut next: Option<(u8, Dynamic)>;
|
||||||
|
|
||||||
match self.get_indexed_mut(state, lib, target, idx_val, pos, true, level) {
|
match self.get_indexed_mut(state, lib, target, idx_val, pos, true, level) {
|
||||||
// Indexed value is an owned value - the only possibility is an indexer
|
// Indexed value is an owned value - the only possibility is an indexer
|
||||||
// Try to call an index setter
|
// Try to call an index setter
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Ok(obj_ptr) if obj_ptr.is_value() => {
|
Ok(obj_ptr) if obj_ptr.is_value() => {
|
||||||
let args = &mut [target.as_mut(), &mut idx_val2, &mut new_val];
|
next = Some((1, _new_val.unwrap()));
|
||||||
|
}
|
||||||
|
// Indexed value is a reference - update directly
|
||||||
|
Ok(ref mut obj_ptr) => {
|
||||||
|
obj_ptr
|
||||||
|
.set_value(_new_val.unwrap())
|
||||||
|
.map_err(|err| err.new_position(rhs.position()))?;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
{
|
||||||
|
next = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => match *err {
|
||||||
|
// No index getter - try to call an index setter
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
EvalAltResult::ErrorIndexingType(_, _) => {
|
||||||
|
next = Some((2, _new_val.unwrap()));
|
||||||
|
}
|
||||||
|
// Error
|
||||||
|
err => return Err(Box::new(err)),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
match &mut next {
|
||||||
|
// next step is custom index setter call
|
||||||
|
Some((1, _new_val)) => {
|
||||||
|
let args = &mut [target.as_mut(), &mut idx_val2, _new_val];
|
||||||
|
|
||||||
self.exec_fn_call(
|
self.exec_fn_call(
|
||||||
state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false,
|
state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false,
|
||||||
@ -698,27 +747,20 @@ impl Engine {
|
|||||||
_ => Err(err),
|
_ => Err(err),
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
// Indexed value is a reference - update directly
|
|
||||||
Ok(ref mut obj_ptr) => {
|
// next step is custom index setter call in case of error
|
||||||
obj_ptr
|
Some((2, _new_val)) => {
|
||||||
.set_value(new_val)
|
let args = &mut [target.as_mut(), &mut idx_val2, _new_val];
|
||||||
.map_err(|err| err.new_position(rhs.position()))?;
|
|
||||||
}
|
|
||||||
Err(err) => match *err {
|
|
||||||
// No index getter - try to call an index setter
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
EvalAltResult::ErrorIndexingType(_, _) => {
|
|
||||||
let args = &mut [target.as_mut(), &mut idx_val2, &mut new_val];
|
|
||||||
|
|
||||||
self.exec_fn_call(
|
self.exec_fn_call(
|
||||||
state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false,
|
state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false,
|
||||||
None, level,
|
None, level,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
// Error
|
None => (),
|
||||||
err => return Err(Box::new(err)),
|
_ => unreachable!()
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Default::default())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
// xxx[rhs]
|
// xxx[rhs]
|
||||||
@ -835,11 +877,10 @@ impl Engine {
|
|||||||
.map_err(|err| err.new_position(*pos))?;
|
.map_err(|err| err.new_position(*pos))?;
|
||||||
|
|
||||||
let val = &mut val;
|
let val = &mut val;
|
||||||
let target = &mut val.into();
|
|
||||||
|
|
||||||
let (result, may_be_changed) = self
|
let (result, may_be_changed) = self
|
||||||
.eval_dot_index_chain_helper(
|
.eval_dot_index_chain_helper(
|
||||||
state, lib, this_ptr, target, expr, idx_values, next_chain,
|
state, lib, this_ptr, &mut val.into(), expr, idx_values, next_chain,
|
||||||
level, _new_val,
|
level, _new_val,
|
||||||
)
|
)
|
||||||
.map_err(|err| err.new_position(*pos))?;
|
.map_err(|err| err.new_position(*pos))?;
|
||||||
@ -1069,18 +1110,6 @@ impl Engine {
|
|||||||
|
|
||||||
let val = target.as_mut();
|
let val = target.as_mut();
|
||||||
|
|
||||||
// if val.is_shared() {
|
|
||||||
// return self.get_indexed_mut(
|
|
||||||
// state,
|
|
||||||
// lib,
|
|
||||||
// &mut Target::Value(val.read::<Dynamic>().unwrap()),
|
|
||||||
// idx,
|
|
||||||
// idx_pos,
|
|
||||||
// create,
|
|
||||||
// level,
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
match val {
|
match val {
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Dynamic(Union::Array(arr)) => {
|
Dynamic(Union::Array(arr)) => {
|
||||||
|
@ -699,7 +699,6 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle shared()
|
// Handle shared()
|
||||||
#[cfg(not(feature = "no_shared"))]
|
|
||||||
if name == KEYWORD_SHARED && args_expr.len() == 1 {
|
if name == KEYWORD_SHARED && args_expr.len() == 1 {
|
||||||
let expr = args_expr.get(0).unwrap();
|
let expr = args_expr.get(0).unwrap();
|
||||||
let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
|
let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
|
||||||
|
@ -18,7 +18,7 @@ use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, iter::empty, string::Stri
|
|||||||
use crate::stdlib::mem;
|
use crate::stdlib::mem;
|
||||||
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
use crate::stdlib::{rc::Rc, cell::{RefCell, Ref, RefMut}};
|
use crate::stdlib::{rc::Rc, cell::RefCell};
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
use crate::stdlib::sync::{Arc, RwLock};
|
use crate::stdlib::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
|
|||||||
ref_T.clone()
|
ref_T.clone()
|
||||||
} else if TypeId::of::<T>() == TypeId::of::<String>() {
|
} else if TypeId::of::<T>() == TypeId::of::<String>() {
|
||||||
// If T is String, data must be ImmutableString, so map directly to it
|
// If T is String, data must be ImmutableString, so map directly to it
|
||||||
*unsafe_cast_box(Box::new(data.as_str().unwrap().to_string())).unwrap()
|
*unsafe_cast_box(Box::new(data.clone().take_string().unwrap())).unwrap()
|
||||||
} else {
|
} else {
|
||||||
// We consume the argument and then replace it with () - the argument is not supposed to be used again.
|
// We consume the argument and then replace it with () - the argument is not supposed to be used again.
|
||||||
// This way, we avoid having to clone the argument again, because it is already a clone when passed here.
|
// This way, we avoid having to clone the argument again, because it is already a clone when passed here.
|
||||||
|
@ -411,6 +411,11 @@ struct ParseState<'e> {
|
|||||||
/// Tracks a list of external variables (variables that are not explicitly declared in the scope).
|
/// Tracks a list of external variables (variables that are not explicitly declared in the scope).
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
externals: HashMap<String, Position>,
|
externals: HashMap<String, Position>,
|
||||||
|
/// An indicator that prevents variables capturing into externals one time.
|
||||||
|
/// If set to true the next call of `access_var` will not capture the variable.
|
||||||
|
/// All consequent calls to `access_var` will not be affected
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
capture: bool,
|
||||||
/// Encapsulates a local stack with variable names to simulate an actual runtime scope.
|
/// Encapsulates a local stack with variable names to simulate an actual runtime scope.
|
||||||
modules: Vec<String>,
|
modules: Vec<String>,
|
||||||
/// Maximum levels of expression nesting.
|
/// Maximum levels of expression nesting.
|
||||||
@ -436,6 +441,8 @@ impl<'e> ParseState<'e> {
|
|||||||
max_function_expr_depth,
|
max_function_expr_depth,
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
externals: Default::default(),
|
externals: Default::default(),
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
capture: true,
|
||||||
stack: Default::default(),
|
stack: Default::default(),
|
||||||
modules: Default::default(),
|
modules: Default::default(),
|
||||||
}
|
}
|
||||||
@ -458,9 +465,13 @@ impl<'e> ParseState<'e> {
|
|||||||
.and_then(|(i, _)| NonZeroUsize::new(i + 1));
|
.and_then(|(i, _)| NonZeroUsize::new(i + 1));
|
||||||
|
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
if self.capture {
|
||||||
if index.is_none() && !self.externals.contains_key(name) {
|
if index.is_none() && !self.externals.contains_key(name) {
|
||||||
self.externals.insert(name.to_string(), pos);
|
self.externals.insert(name.to_string(), pos);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
self.capture = true
|
||||||
|
}
|
||||||
|
|
||||||
index
|
index
|
||||||
}
|
}
|
||||||
@ -2171,9 +2182,18 @@ fn parse_binary_op(
|
|||||||
|
|
||||||
let (op_token, pos) = input.next().unwrap();
|
let (op_token, pos) = input.next().unwrap();
|
||||||
|
|
||||||
let rhs = parse_unary(input, state, lib, settings)?;
|
let next = input.peek().unwrap();
|
||||||
|
let next_precedence = next.0.precedence(custom);
|
||||||
|
|
||||||
let next_precedence = input.peek().unwrap().0.precedence(custom);
|
#[cfg(any(not(feature = "no_object"), not(feature = "no_capture")))]
|
||||||
|
if op_token == Token::Period {
|
||||||
|
if let (Token::Identifier(_), _) = next {
|
||||||
|
// prevents capturing of the object properties as vars: xxx.<var>
|
||||||
|
state.capture = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let rhs = parse_unary(input, state, lib, settings)?;
|
||||||
|
|
||||||
// Bind to right if the next operator has higher precedence
|
// Bind to right if the next operator has higher precedence
|
||||||
// If same precedence, then check if the operator binds right
|
// If same precedence, then check if the operator binds right
|
||||||
|
11
src/token.rs
11
src/token.rs
@ -1430,13 +1430,20 @@ fn get_identifier(
|
|||||||
/// Is this keyword allowed as a function?
|
/// Is this keyword allowed as a function?
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn is_keyword_function(name: &str) -> bool {
|
pub fn is_keyword_function(name: &str) -> bool {
|
||||||
name == KEYWORD_PRINT
|
let mut result = name == KEYWORD_PRINT
|
||||||
|| name == KEYWORD_DEBUG
|
|| name == KEYWORD_DEBUG
|
||||||
|| name == KEYWORD_TYPE_OF
|
|| name == KEYWORD_TYPE_OF
|
||||||
|| name == KEYWORD_EVAL
|
|| name == KEYWORD_EVAL
|
||||||
|| name == KEYWORD_FN_PTR
|
|| name == KEYWORD_FN_PTR
|
||||||
|| name == KEYWORD_FN_PTR_CALL
|
|| name == KEYWORD_FN_PTR_CALL
|
||||||
|| name == KEYWORD_FN_PTR_CURRY
|
|| name == KEYWORD_FN_PTR_CURRY;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no-shared"))]
|
||||||
|
{
|
||||||
|
result = result || name == KEYWORD_SHARED;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_valid_identifier(name: impl Iterator<Item = char>) -> bool {
|
pub fn is_valid_identifier(name: impl Iterator<Item = char>) -> bool {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#![cfg(not(feature = "no_function"))]
|
#![cfg(not(feature = "no_function"))]
|
||||||
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT};
|
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT, Array};
|
||||||
use std::any::TypeId;
|
use std::any::{TypeId, Any};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
|
fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
|
||||||
@ -58,3 +58,150 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
fn test_shared() -> Result<(), Box<EvalAltResult>> {
|
||||||
|
let engine = Engine::new();
|
||||||
|
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<INT>(
|
||||||
|
// r#"
|
||||||
|
// shared(42)
|
||||||
|
// "#
|
||||||
|
// )?,
|
||||||
|
// 42
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<bool>(
|
||||||
|
// r#"
|
||||||
|
// shared(true)
|
||||||
|
// "#
|
||||||
|
// )?,
|
||||||
|
// true
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// #[cfg(not(feature = "no_float"))]
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<f64>(
|
||||||
|
// r#"
|
||||||
|
// shared(4.2)
|
||||||
|
// "#
|
||||||
|
// )?,
|
||||||
|
// 4.2
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<String>(
|
||||||
|
// r#"
|
||||||
|
// shared("test")
|
||||||
|
// "#
|
||||||
|
// )?,
|
||||||
|
// "test"
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// #[cfg(not(feature = "no_index"))]
|
||||||
|
// {
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<Array>(
|
||||||
|
// r#"
|
||||||
|
// let x = shared([1, 2, 3]);
|
||||||
|
// let y = shared([4, 5]);
|
||||||
|
// x + y
|
||||||
|
// "#
|
||||||
|
// )?.len(),
|
||||||
|
// 5
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<INT>(
|
||||||
|
// r"
|
||||||
|
// let x = shared([2, 9]);
|
||||||
|
// x.insert(-1, 1);
|
||||||
|
// x.insert(999, 3);
|
||||||
|
//
|
||||||
|
// let r = x.remove(2);
|
||||||
|
//
|
||||||
|
// let y = shared([4, 5]);
|
||||||
|
// x.append(y);
|
||||||
|
//
|
||||||
|
// x.len + r
|
||||||
|
// "
|
||||||
|
// )?,
|
||||||
|
// 14
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<bool>(
|
||||||
|
// r#"
|
||||||
|
// let x = shared([1, 2, 3]);
|
||||||
|
//
|
||||||
|
// if x[0] + x[2] == 4 {
|
||||||
|
// true
|
||||||
|
// } else {
|
||||||
|
// false
|
||||||
|
// }
|
||||||
|
// "#
|
||||||
|
// )?,
|
||||||
|
// true
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[cfg(not(feature = "no_object"))]
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<INT>(r#"
|
||||||
|
// let y = shared(#{a: 1, b: 2, c: 3});
|
||||||
|
// y.c = shared(5);
|
||||||
|
// y.c
|
||||||
|
// "#)?,
|
||||||
|
// 5
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// #[cfg(not(feature = "no_object"))]
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<INT>(r#"
|
||||||
|
// let y = shared(#{a: 1, b: 2, c: shared(3)});
|
||||||
|
// let c = y.c;
|
||||||
|
// c = 5;// "c" still holds Dynamic Shared
|
||||||
|
// y.c
|
||||||
|
// "#)?,
|
||||||
|
// 5
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// #[cfg(not(feature = "no_capture"))]
|
||||||
|
// assert_eq!(
|
||||||
|
// engine.eval::<INT>(r#"
|
||||||
|
// let x = shared(1);
|
||||||
|
// (|| x = x + 41).call();
|
||||||
|
// x
|
||||||
|
// "#)?,
|
||||||
|
// 42
|
||||||
|
// );
|
||||||
|
|
||||||
|
#[cfg(all(not(feature = "no_object"), not(feature = "no_capture")))]
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<INT>(r#"
|
||||||
|
// let x = shared(#{a: 1, b: shared(2), c: 3});
|
||||||
|
// let a = x.a;
|
||||||
|
// let b = x.b;
|
||||||
|
// a = 100;
|
||||||
|
// b = 20;
|
||||||
|
//
|
||||||
|
// let f = |a| {
|
||||||
|
// x.c = x.a + x.b;// + a;
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// f.call(20);
|
||||||
|
//
|
||||||
|
// x.c
|
||||||
|
|
||||||
|
let x = #{a: 1, b: 2};
|
||||||
|
|
||||||
|
x.a + x.b
|
||||||
|
"#)?,
|
||||||
|
42
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user