Add contains for array.
This commit is contained in:
parent
7805540b7b
commit
ff7844893d
@ -164,7 +164,7 @@ mod array_functions {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw)]
|
#[rhai_fn(return_raw, pure)]
|
||||||
pub fn map(
|
pub fn map(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -197,7 +197,7 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok(ar.into())
|
Ok(ar.into())
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw)]
|
#[rhai_fn(return_raw, pure)]
|
||||||
pub fn filter(
|
pub fn filter(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -233,8 +233,70 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok(ar.into())
|
Ok(ar.into())
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw)]
|
#[rhai_fn(return_raw, pure)]
|
||||||
|
pub fn contains(
|
||||||
|
ctx: NativeCallContext,
|
||||||
|
array: &mut Array,
|
||||||
|
mut value: Dynamic,
|
||||||
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
|
for item in array.iter() {
|
||||||
|
if ctx
|
||||||
|
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [&mut value, &mut item.clone()])
|
||||||
|
.or_else(|err| match *err {
|
||||||
|
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
|
||||||
|
if fn_sig.starts_with(OP_EQUALS) =>
|
||||||
|
{
|
||||||
|
if item.type_id() == value.type_id() {
|
||||||
|
// No default when comparing same type
|
||||||
|
Err(err)
|
||||||
|
} else {
|
||||||
|
Ok(Dynamic::FALSE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Err(err),
|
||||||
|
})?
|
||||||
|
.as_bool()
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
return Ok(Dynamic::TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Dynamic::FALSE)
|
||||||
|
}
|
||||||
|
#[rhai_fn(return_raw, pure)]
|
||||||
pub fn index_of(
|
pub fn index_of(
|
||||||
|
ctx: NativeCallContext,
|
||||||
|
array: &mut Array,
|
||||||
|
mut value: Dynamic,
|
||||||
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
|
for (i, item) in array.iter().enumerate() {
|
||||||
|
if ctx
|
||||||
|
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [&mut value, &mut item.clone()])
|
||||||
|
.or_else(|err| match *err {
|
||||||
|
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
|
||||||
|
if fn_sig.starts_with(OP_EQUALS) =>
|
||||||
|
{
|
||||||
|
if item.type_id() == value.type_id() {
|
||||||
|
// No default when comparing same type
|
||||||
|
Err(err)
|
||||||
|
} else {
|
||||||
|
Ok(Dynamic::FALSE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Err(err),
|
||||||
|
})?
|
||||||
|
.as_bool()
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
return Ok((i as INT).into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((-1 as INT).into())
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "index_of", return_raw, pure)]
|
||||||
|
pub fn index_of_filter(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
filter: FnPtr,
|
filter: FnPtr,
|
||||||
@ -267,7 +329,7 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok((-1 as INT).into())
|
Ok((-1 as INT).into())
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw)]
|
#[rhai_fn(return_raw, pure)]
|
||||||
pub fn some(
|
pub fn some(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -301,7 +363,7 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok(false.into())
|
Ok(false.into())
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw)]
|
#[rhai_fn(return_raw, pure)]
|
||||||
pub fn all(
|
pub fn all(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -335,7 +397,7 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok(true.into())
|
Ok(true.into())
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw)]
|
#[rhai_fn(return_raw, pure)]
|
||||||
pub fn reduce(
|
pub fn reduce(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -366,7 +428,7 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
#[rhai_fn(name = "reduce", return_raw)]
|
#[rhai_fn(name = "reduce", return_raw, pure)]
|
||||||
pub fn reduce_with_initial(
|
pub fn reduce_with_initial(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -405,7 +467,7 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw)]
|
#[rhai_fn(return_raw, pure)]
|
||||||
pub fn reduce_rev(
|
pub fn reduce_rev(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -436,7 +498,7 @@ mod array_functions {
|
|||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
#[rhai_fn(name = "reduce_rev", return_raw)]
|
#[rhai_fn(name = "reduce_rev", return_raw, pure)]
|
||||||
pub fn reduce_rev_with_initial(
|
pub fn reduce_rev_with_initial(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -634,7 +696,7 @@ mod array_functions {
|
|||||||
|
|
||||||
drained
|
drained
|
||||||
}
|
}
|
||||||
#[rhai_fn(name = "==", return_raw)]
|
#[rhai_fn(name = "==", return_raw, pure)]
|
||||||
pub fn equals(
|
pub fn equals(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
@ -648,18 +710,31 @@ mod array_functions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (a1, a2) in array.iter_mut().zip(array2.iter_mut()) {
|
for (a1, a2) in array.iter_mut().zip(array2.iter_mut()) {
|
||||||
let equals = ctx
|
if !ctx
|
||||||
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [a1, a2])
|
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [a1, a2])
|
||||||
.map(|v| v.as_bool().unwrap_or(false))?;
|
.or_else(|err| match *err {
|
||||||
|
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
|
||||||
if !equals {
|
if fn_sig.starts_with(OP_EQUALS) =>
|
||||||
return Ok(false.into());
|
{
|
||||||
|
if a1.type_id() == a2.type_id() {
|
||||||
|
// No default when comparing same type
|
||||||
|
Err(err)
|
||||||
|
} else {
|
||||||
|
Ok(Dynamic::FALSE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Err(err),
|
||||||
|
})?
|
||||||
|
.as_bool()
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
return Ok(Dynamic::FALSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(true.into())
|
Ok(Dynamic::TRUE)
|
||||||
}
|
}
|
||||||
#[rhai_fn(name = "!=", return_raw)]
|
#[rhai_fn(name = "!=", return_raw, pure)]
|
||||||
pub fn not_equals(
|
pub fn not_equals(
|
||||||
ctx: NativeCallContext,
|
ctx: NativeCallContext,
|
||||||
array: &mut Array,
|
array: &mut Array,
|
||||||
|
Loading…
Reference in New Issue
Block a user