Use modules to implement packages.
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
#![cfg(not(feature = "no_index"))]
|
||||
|
||||
use super::{reg_binary, reg_binary_mut, reg_trinary_mut, reg_unary_mut};
|
||||
|
||||
use crate::any::{Dynamic, Variant};
|
||||
use crate::def_package;
|
||||
use crate::engine::Array;
|
||||
use crate::fn_register::{map_dynamic as map, map_identity as pass};
|
||||
use crate::module::FuncReturn;
|
||||
use crate::parser::INT;
|
||||
|
||||
use crate::stdlib::{any::TypeId, boxed::Box, string::String};
|
||||
|
||||
// Register array utility functions
|
||||
fn push<T: Variant + Clone>(list: &mut Array, item: T) {
|
||||
fn push<T: Variant + Clone>(list: &mut Array, item: T) -> FuncReturn<()> {
|
||||
list.push(Dynamic::from(item));
|
||||
Ok(())
|
||||
}
|
||||
fn ins<T: Variant + Clone>(list: &mut Array, position: INT, item: T) {
|
||||
fn ins<T: Variant + Clone>(list: &mut Array, position: INT, item: T) -> FuncReturn<()> {
|
||||
if position <= 0 {
|
||||
list.insert(0, Dynamic::from(item));
|
||||
} else if (position as usize) >= list.len() - 1 {
|
||||
@@ -22,20 +21,26 @@ fn ins<T: Variant + Clone>(list: &mut Array, position: INT, item: T) {
|
||||
} else {
|
||||
list.insert(position as usize, Dynamic::from(item));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn pad<T: Variant + Clone>(list: &mut Array, len: INT, item: T) {
|
||||
fn pad<T: Variant + Clone>(list: &mut Array, len: INT, item: T) -> FuncReturn<()> {
|
||||
if len >= 0 {
|
||||
while list.len() < len as usize {
|
||||
push(list, item.clone());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
macro_rules! reg_op { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
|
||||
$(reg_binary_mut($lib, $op, $func::<$par>, map);)* };
|
||||
macro_rules! reg_op {
|
||||
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
|
||||
$( $lib.set_fn_2_mut($op, $func::<$par>); )*
|
||||
};
|
||||
}
|
||||
macro_rules! reg_tri { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
|
||||
$(reg_trinary_mut($lib, $op, $func::<$par>, map);)* };
|
||||
macro_rules! reg_tri {
|
||||
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
|
||||
$( $lib.set_fn_3_mut($op, $func::<$par>); )*
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
@@ -44,15 +49,16 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
reg_tri!(lib, "pad", pad, INT, bool, char, String, Array, ());
|
||||
reg_tri!(lib, "insert", ins, INT, bool, char, String, Array, ());
|
||||
|
||||
reg_binary_mut(lib, "append", |x: &mut Array, y: Array| x.extend(y), map);
|
||||
reg_binary(
|
||||
lib,
|
||||
lib.set_fn_2_mut("append", |x: &mut Array, y: Array| {
|
||||
x.extend(y);
|
||||
Ok(())
|
||||
});
|
||||
lib.set_fn_2(
|
||||
"+",
|
||||
|mut x: Array, y: Array| {
|
||||
x.extend(y);
|
||||
x
|
||||
Ok(x)
|
||||
},
|
||||
map,
|
||||
);
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
@@ -70,40 +76,36 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
reg_tri!(lib, "insert", ins, f32, f64);
|
||||
}
|
||||
|
||||
reg_unary_mut(
|
||||
lib,
|
||||
lib.set_fn_1_mut(
|
||||
"pop",
|
||||
|list: &mut Array| list.pop().unwrap_or_else(|| ().into()),
|
||||
pass,
|
||||
|list: &mut Array| Ok(list.pop().unwrap_or_else(|| ().into())),
|
||||
);
|
||||
reg_unary_mut(
|
||||
lib,
|
||||
lib.set_fn_1_mut(
|
||||
"shift",
|
||||
|list: &mut Array| {
|
||||
if list.is_empty() {
|
||||
Ok(if list.is_empty() {
|
||||
().into()
|
||||
} else {
|
||||
list.remove(0)
|
||||
}
|
||||
})
|
||||
},
|
||||
pass,
|
||||
);
|
||||
reg_binary_mut(
|
||||
lib,
|
||||
lib.set_fn_2_mut(
|
||||
"remove",
|
||||
|list: &mut Array, len: INT| {
|
||||
if len < 0 || (len as usize) >= list.len() {
|
||||
Ok(if len < 0 || (len as usize) >= list.len() {
|
||||
().into()
|
||||
} else {
|
||||
list.remove(len as usize)
|
||||
}
|
||||
})
|
||||
},
|
||||
pass,
|
||||
);
|
||||
reg_unary_mut(lib, "len", |list: &mut Array| list.len() as INT, map);
|
||||
reg_unary_mut(lib, "clear", |list: &mut Array| list.clear(), map);
|
||||
reg_binary_mut(
|
||||
lib,
|
||||
lib.set_fn_1_mut("len", |list: &mut Array| Ok(list.len() as INT));
|
||||
lib.set_fn_1_mut("clear", |list: &mut Array| {
|
||||
list.clear();
|
||||
Ok(())
|
||||
});
|
||||
lib.set_fn_2_mut(
|
||||
"truncate",
|
||||
|list: &mut Array, len: INT| {
|
||||
if len >= 0 {
|
||||
@@ -111,12 +113,12 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
} else {
|
||||
list.clear();
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
map,
|
||||
);
|
||||
|
||||
// Register array iterator
|
||||
lib.type_iterators.insert(
|
||||
lib.set_iterator(
|
||||
TypeId::of::<Array>(),
|
||||
Box::new(|arr| Box::new(
|
||||
arr.cast::<Array>().into_iter()) as Box<dyn Iterator<Item = Dynamic>>
|
||||
|
Reference in New Issue
Block a user