Build in array/blob concat functions.

This commit is contained in:
Stephen Chung 2021-12-22 19:59:48 +08:00
parent 422db4269e
commit 3751b6d018
4 changed files with 55 additions and 49 deletions

View File

@ -48,6 +48,8 @@ fn is_numeric(type_id: TypeId) -> bool {
} }
/// Build in common binary operator implementations to avoid the cost of calling a registered function. /// Build in common binary operator implementations to avoid the cost of calling a registered function.
///
/// The return function will be registered as a _method_, so the first parameter cannot be consumed.
#[must_use] #[must_use]
pub fn get_builtin_binary_op_fn( pub fn get_builtin_binary_op_fn(
op: &str, op: &str,
@ -291,28 +293,6 @@ pub fn get_builtin_binary_op_fn(
} }
if type1 == type2 { if type1 == type2 {
return match op { return match op {
"+" => Some(|_, args| {
let blob1 = &*args[0].read_lock::<Blob>().expect(BUILTIN);
let blob2 = &*args[1].read_lock::<Blob>().expect(BUILTIN);
let result = if blob1.is_empty() {
if blob2.is_empty() {
Blob::new()
} else {
blob2.clone()
}
} else {
if blob2.is_empty() {
blob1.clone()
} else {
let mut blob = Blob::with_capacity(blob1.len() + blob2.len());
blob.extend_from_slice(blob1);
blob.extend_from_slice(blob2);
blob
}
};
Ok(Dynamic::from_blob(result)) // do not use result.into() because it'll convert into an Array
}),
"==" => Some(impl_op!(Blob == Blob)), "==" => Some(impl_op!(Blob == Blob)),
"!=" => Some(impl_op!(Blob != Blob)), "!=" => Some(impl_op!(Blob != Blob)),
_ => None, _ => None,
@ -533,6 +513,8 @@ pub fn get_builtin_binary_op_fn(
} }
/// Build in common operator assignment implementations to avoid the cost of calling a registered function. /// Build in common operator assignment implementations to avoid the cost of calling a registered function.
///
/// The return function is registered as a _method_, so the first parameter cannot be consumed.
#[must_use] #[must_use]
pub fn get_builtin_op_assignment_fn( pub fn get_builtin_op_assignment_fn(
op: &str, op: &str,
@ -770,22 +752,31 @@ pub fn get_builtin_op_assignment_fn(
}; };
} }
#[cfg(not(feature = "no_index"))]
if type1 == TypeId::of::<crate::Array>() {
use crate::packages::array_basic::array_functions::*;
use crate::Array;
return match op {
"+=" => Some(|_, args| {
let array2 = std::mem::take(args[1]).cast::<Array>();
let array1 = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
Ok(append(array1, array2).into())
}),
_ => None,
};
}
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
if type1 == TypeId::of::<crate::Blob>() { if type1 == TypeId::of::<crate::Blob>() {
use crate::packages::blob_basic::blob_functions::*;
use crate::Blob; use crate::Blob;
return match op { return match op {
"+=" => Some(|_, args| { "+=" => Some(|_, args| {
let blob2 = std::mem::take(args[1]).cast::<Blob>(); let blob2 = std::mem::take(args[1]).cast::<Blob>();
let mut blob1 = args[0].write_lock::<Blob>().expect(BUILTIN); let blob1 = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
if !blob2.is_empty() { Ok(append(blob1, blob2).into())
if blob1.is_empty() {
*blob1 = blob2;
} else {
blob1.extend_from_slice(&blob2);
}
}
Ok(Dynamic::UNIT)
}), }),
_ => None, _ => None,
}; };

View File

@ -24,7 +24,7 @@ def_package! {
} }
#[export_module] #[export_module]
mod array_functions { pub mod array_functions {
#[rhai_fn(name = "len", get = "len", pure)] #[rhai_fn(name = "len", get = "len", pure)]
pub fn len(array: &mut Array) -> INT { pub fn len(array: &mut Array) -> INT {
array.len() as INT array.len() as INT
@ -33,27 +33,30 @@ mod array_functions {
pub fn push(array: &mut Array, item: Dynamic) { pub fn push(array: &mut Array, item: Dynamic) {
array.push(item); array.push(item);
} }
#[rhai_fn(name = "append", name = "+=")] #[rhai_fn(name = "append")]
pub fn append(array: &mut Array, y: Array) { pub fn append(array1: &mut Array, array2: Array) {
if !y.is_empty() { if !array2.is_empty() {
if array.is_empty() { if array1.is_empty() {
*array = y; *array1 = array2;
} else { } else {
array.extend(y); array1.extend(array2);
} }
} }
} }
#[rhai_fn(name = "+")] #[rhai_fn(name = "+")]
pub fn concat(mut array: Array, y: Array) -> Array { pub fn concat(array1: Array, array2: Array) -> Array {
if !y.is_empty() { if !array2.is_empty() {
if array.is_empty() { if array1.is_empty() {
array = y; array2
} else { } else {
array.extend(y); let mut array = array1;
} array.extend(array2);
}
array array
} }
} else {
array1
}
}
pub fn insert(array: &mut Array, position: INT, item: Dynamic) { pub fn insert(array: &mut Array, position: INT, item: Dynamic) {
if array.is_empty() { if array.is_empty() {
array.push(item); array.push(item);

View File

@ -26,7 +26,7 @@ def_package! {
} }
#[export_module] #[export_module]
mod blob_functions { pub mod blob_functions {
pub const fn blob() -> Blob { pub const fn blob() -> Blob {
Blob::new() Blob::new()
} }
@ -64,12 +64,10 @@ mod blob_functions {
pub fn len(blob: &mut Blob) -> INT { pub fn len(blob: &mut Blob) -> INT {
blob.len() as INT blob.len() as INT
} }
#[rhai_fn(name = "push")]
pub fn push(blob: &mut Blob, item: INT) { pub fn push(blob: &mut Blob, item: INT) {
let item = (item & 0x000000ff) as u8; let item = (item & 0x000000ff) as u8;
blob.push(item); blob.push(item);
} }
#[rhai_fn(name = "append")]
pub fn append(blob: &mut Blob, y: Blob) { pub fn append(blob: &mut Blob, y: Blob) {
if !y.is_empty() { if !y.is_empty() {
if blob.is_empty() { if blob.is_empty() {
@ -79,6 +77,20 @@ mod blob_functions {
} }
} }
} }
#[rhai_fn(name = "+")]
pub fn concat(blob1: Blob, blob2: Blob) -> Blob {
if !blob2.is_empty() {
if blob1.is_empty() {
blob2
} else {
let mut blob = blob1;
blob.extend(blob2);
blob
}
} else {
blob1
}
}
pub fn insert(blob: &mut Blob, position: INT, item: INT) { pub fn insert(blob: &mut Blob, position: INT, item: INT) {
let item = (item & 0x000000ff) as u8; let item = (item & 0x000000ff) as u8;

View File

@ -3,8 +3,8 @@
use crate::{Module, Shared}; use crate::{Module, Shared};
pub(crate) mod arithmetic; pub(crate) mod arithmetic;
mod array_basic; pub(crate) mod array_basic;
mod blob_basic; pub(crate) mod blob_basic;
mod fn_basic; mod fn_basic;
mod iter_basic; mod iter_basic;
mod lang_core; mod lang_core;