From 5c31ec7f782e41360fa13ade87b73c4fd0a959fb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 22 Dec 2021 22:22:20 +0800 Subject: [PATCH] Fix array bug. --- src/func/builtin.rs | 45 ++++++++++++++++++++++++------------- src/packages/array_basic.rs | 2 -- src/parser.rs | 6 ++--- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/func/builtin.rs b/src/func/builtin.rs index be09add4..2da123d4 100644 --- a/src/func/builtin.rs +++ b/src/func/builtin.rs @@ -656,7 +656,35 @@ pub fn get_builtin_op_assignment_fn( _ => None, }; } - // Blob op= int + + // array op= any + #[cfg(not(feature = "no_index"))] + if type1 == TypeId::of::() { + use crate::packages::array_basic::array_functions::*; + use crate::Array; + + if type2 == TypeId::of::() { + return match op { + "+=" => Some(|_, args| { + let array2 = std::mem::take(args[1]).cast::(); + let array1 = &mut *args[0].write_lock::().expect(BUILTIN); + Ok(append(array1, array2).into()) + }), + _ => None, + }; + } else { + return match op { + "+=" => Some(|_, args| { + let x = std::mem::take(args[1]); + let array = &mut *args[0].write_lock::().expect(BUILTIN); + Ok(push(array, x).into()) + }), + _ => None, + }; + } + } + + // blob op= int #[cfg(not(feature = "no_index"))] if types_pair == (TypeId::of::(), TypeId::of::()) { use crate::Blob; @@ -752,21 +780,6 @@ pub fn get_builtin_op_assignment_fn( }; } - #[cfg(not(feature = "no_index"))] - if type1 == TypeId::of::() { - use crate::packages::array_basic::array_functions::*; - use crate::Array; - - return match op { - "+=" => Some(|_, args| { - let array2 = std::mem::take(args[1]).cast::(); - let array1 = &mut *args[0].write_lock::().expect(BUILTIN); - Ok(append(array1, array2).into()) - }), - _ => None, - }; - } - #[cfg(not(feature = "no_index"))] if type1 == TypeId::of::() { use crate::packages::blob_basic::blob_functions::*; diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 10c5d7f9..b26a4ace 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -29,11 +29,9 @@ pub mod array_functions { pub fn len(array: &mut Array) -> INT { array.len() as INT } - #[rhai_fn(name = "push", name = "+=")] pub fn push(array: &mut Array, item: Dynamic) { array.push(item); } - #[rhai_fn(name = "append")] pub fn append(array1: &mut Array, array2: Array) { if !array2.is_empty() { if array1.is_empty() { diff --git a/src/parser.rs b/src/parser.rs index 538e990e..e2dd2162 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1110,9 +1110,9 @@ fn parse_switch( value.hash(hasher); let hash = hasher.finish(); - if !table.contains_key(&hash) { - table.insert(hash, (condition.clone(), stmt.into()).into()); - } + table + .entry(hash) + .or_insert_with(|| (condition.clone(), stmt.into()).into()); } // Other range _ => ranges.push((range.0, range.1, range.2, condition, stmt.into())),