Allow non-Dynamic in return_raw.
This commit is contained in:
@@ -24,91 +24,91 @@ macro_rules! gen_arithmetic_functions {
|
||||
#[export_module]
|
||||
pub mod functions {
|
||||
#[rhai_fn(name = "+", return_raw)]
|
||||
pub fn add(x: $arg_type, y: $arg_type) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn add(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_add(y).ok_or_else(|| make_err(format!("Addition overflow: {} + {}", x, y))).map(Dynamic::from)
|
||||
x.checked_add(y).ok_or_else(|| make_err(format!("Addition overflow: {} + {}", x, y)))
|
||||
} else {
|
||||
Ok(Dynamic::from(x + y))
|
||||
Ok(x + y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "-", return_raw)]
|
||||
pub fn subtract(x: $arg_type, y: $arg_type) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn subtract(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_sub(y).ok_or_else(|| make_err(format!("Subtraction overflow: {} - {}", x, y))).map(Dynamic::from)
|
||||
x.checked_sub(y).ok_or_else(|| make_err(format!("Subtraction overflow: {} - {}", x, y)))
|
||||
} else {
|
||||
Ok(Dynamic::from(x - y))
|
||||
Ok(x - y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "*", return_raw)]
|
||||
pub fn multiply(x: $arg_type, y: $arg_type) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn multiply(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_mul(y).ok_or_else(|| make_err(format!("Multiplication overflow: {} * {}", x, y))).map(Dynamic::from)
|
||||
x.checked_mul(y).ok_or_else(|| make_err(format!("Multiplication overflow: {} * {}", x, y)))
|
||||
} else {
|
||||
Ok(Dynamic::from(x * y))
|
||||
Ok(x * y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "/", return_raw)]
|
||||
pub fn divide(x: $arg_type, y: $arg_type) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn divide(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
// Detect division by zero
|
||||
if y == 0 {
|
||||
Err(make_err(format!("Division by zero: {} / {}", x, y)))
|
||||
} else {
|
||||
x.checked_div(y).ok_or_else(|| make_err(format!("Division overflow: {} / {}", x, y))).map(Dynamic::from)
|
||||
x.checked_div(y).ok_or_else(|| make_err(format!("Division overflow: {} / {}", x, y)))
|
||||
}
|
||||
} else {
|
||||
Ok(Dynamic::from(x / y))
|
||||
Ok(x / y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "%", return_raw)]
|
||||
pub fn modulo(x: $arg_type, y: $arg_type) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn modulo(x: $arg_type, y: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_rem(y).ok_or_else(|| make_err(format!("Modulo division by zero or overflow: {} % {}", x, y))).map(Dynamic::from)
|
||||
x.checked_rem(y).ok_or_else(|| make_err(format!("Modulo division by zero or overflow: {} % {}", x, y)))
|
||||
} else {
|
||||
Ok(Dynamic::from(x % y))
|
||||
Ok(x % y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "**", return_raw)]
|
||||
pub fn power(x: INT, y: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn power(x: $arg_type, y: INT) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && y > (u32::MAX as INT) {
|
||||
Err(make_err(format!("Integer raised to too large an index: {} ~ {}", x, y)))
|
||||
} else if y < 0 {
|
||||
Err(make_err(format!("Integer raised to a negative index: {} ~ {}", x, y)))
|
||||
} else {
|
||||
x.checked_pow(y as u32).ok_or_else(|| make_err(format!("Power overflow: {} ~ {}", x, y))).map(Dynamic::from)
|
||||
x.checked_pow(y as u32).ok_or_else(|| make_err(format!("Power overflow: {} ~ {}", x, y)))
|
||||
}
|
||||
} else {
|
||||
Ok(Dynamic::from(x.pow(y as u32)))
|
||||
Ok(x.pow(y as u32))
|
||||
}
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "<<", return_raw)]
|
||||
pub fn shift_left(x: $arg_type, y: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn shift_left(x: $arg_type, y: INT) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && y > (u32::MAX as INT) {
|
||||
Err(make_err(format!("Left-shift by too many bits: {} << {}", x, y)))
|
||||
} else if y < 0 {
|
||||
Err(make_err(format!("Left-shift by a negative number: {} << {}", x, y)))
|
||||
} else {
|
||||
x.checked_shl(y as u32).ok_or_else(|| make_err(format!("Left-shift by too many bits: {} << {}", x, y))).map(Dynamic::from)
|
||||
x.checked_shl(y as u32).ok_or_else(|| make_err(format!("Left-shift by too many bits: {} << {}", x, y)))
|
||||
}
|
||||
} else {
|
||||
Ok(Dynamic::from(x << y))
|
||||
Ok(x << y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = ">>", return_raw)]
|
||||
pub fn shift_right(x: $arg_type, y: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn shift_right(x: $arg_type, y: INT) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && y > (u32::MAX as INT) {
|
||||
Err(make_err(format!("Right-shift by too many bits: {} >> {}", x, y)))
|
||||
} else if y < 0 {
|
||||
Err(make_err(format!("Right-shift by a negative number: {} >> {}", x, y)))
|
||||
} else {
|
||||
x.checked_shr(y as u32).ok_or_else(|| make_err(format!("Right-shift by too many bits: {} >> {}", x, y))).map(Dynamic::from)
|
||||
x.checked_shr(y as u32).ok_or_else(|| make_err(format!("Right-shift by too many bits: {} >> {}", x, y)))
|
||||
}
|
||||
} else {
|
||||
Ok(Dynamic::from(x >> y))
|
||||
Ok(x >> y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "&")]
|
||||
@@ -136,11 +136,11 @@ macro_rules! gen_signed_functions {
|
||||
#[export_module]
|
||||
pub mod functions {
|
||||
#[rhai_fn(name = "-", return_raw)]
|
||||
pub fn neg(x: $arg_type) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn neg(x: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_neg().ok_or_else(|| make_err(format!("Negation overflow: -{}", x))).map(Dynamic::from)
|
||||
x.checked_neg().ok_or_else(|| make_err(format!("Negation overflow: -{}", x)))
|
||||
} else {
|
||||
Ok(Dynamic::from(-x))
|
||||
Ok(-x)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
@@ -148,11 +148,11 @@ macro_rules! gen_signed_functions {
|
||||
x
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn abs(x: $arg_type) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn abs(x: $arg_type) -> Result<$arg_type, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_abs().ok_or_else(|| make_err(format!("Negation overflow: -{}", x))).map(Dynamic::from)
|
||||
x.checked_abs().ok_or_else(|| make_err(format!("Negation overflow: -{}", x)))
|
||||
} else {
|
||||
Ok(Dynamic::from(x.abs()))
|
||||
Ok(x.abs())
|
||||
}
|
||||
}
|
||||
pub fn sign(x: $arg_type) -> INT {
|
||||
@@ -318,14 +318,14 @@ mod f32_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "**", return_raw)]
|
||||
pub fn pow_f_i(x: f32, y: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn pow_f_i(x: f32, y: INT) -> Result<f32, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) && y > (i32::MAX as INT) {
|
||||
Err(make_err(format!(
|
||||
"Number raised to too large an index: {} ~ {}",
|
||||
x, y
|
||||
)))
|
||||
} else {
|
||||
Ok(Dynamic::from(x.powi(y as i32)))
|
||||
Ok(x.powi(y as i32))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -423,14 +423,14 @@ mod f64_functions {
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "**", return_raw)]
|
||||
pub fn pow_f_i(x: FLOAT, y: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn pow_f_i(x: FLOAT, y: INT) -> Result<FLOAT, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) && y > (i32::MAX as INT) {
|
||||
Err(make_err(format!(
|
||||
"Number raised to too large an index: {} ~ {}",
|
||||
x, y
|
||||
)))
|
||||
} else {
|
||||
Ok(x.powi(y as i32).into())
|
||||
Ok(x.powi(y as i32))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -441,37 +441,37 @@ pub mod decimal_functions {
|
||||
use rust_decimal::{prelude::Zero, Decimal};
|
||||
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn add(x: Decimal, y: Decimal) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn add(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_add(y)
|
||||
.ok_or_else(|| make_err(format!("Addition overflow: {} + {}", x, y)))
|
||||
.map(Into::<Dynamic>::into)
|
||||
} else {
|
||||
Ok(Dynamic::from(x + y))
|
||||
Ok(x + y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn subtract(x: Decimal, y: Decimal) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn subtract(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_sub(y)
|
||||
.ok_or_else(|| make_err(format!("Subtraction overflow: {} - {}", x, y)))
|
||||
.map(Into::<Dynamic>::into)
|
||||
} else {
|
||||
Ok(Dynamic::from(x - y))
|
||||
Ok(x - y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn multiply(x: Decimal, y: Decimal) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn multiply(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_mul(y)
|
||||
.ok_or_else(|| make_err(format!("Multiplication overflow: {} * {}", x, y)))
|
||||
.map(Into::<Dynamic>::into)
|
||||
} else {
|
||||
Ok(Dynamic::from(x * y))
|
||||
Ok(x * y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn divide(x: Decimal, y: Decimal) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn divide(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
// Detect division by zero
|
||||
if y == Decimal::zero() {
|
||||
@@ -482,11 +482,11 @@ pub mod decimal_functions {
|
||||
.map(Into::<Dynamic>::into)
|
||||
}
|
||||
} else {
|
||||
Ok(Dynamic::from(x / y))
|
||||
Ok(x / y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(skip, return_raw)]
|
||||
pub fn modulo(x: Decimal, y: Decimal) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn modulo(x: Decimal, y: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
x.checked_rem(y)
|
||||
.ok_or_else(|| {
|
||||
@@ -497,7 +497,7 @@ pub mod decimal_functions {
|
||||
})
|
||||
.map(Into::<Dynamic>::into)
|
||||
} else {
|
||||
Ok(Dynamic::from(x % y))
|
||||
Ok(x % y)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "-")]
|
||||
|
@@ -47,7 +47,7 @@ mod array_functions {
|
||||
array: &mut Array,
|
||||
len: INT,
|
||||
item: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
// Check if array will be over max size limit
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if _ctx.engine().max_array_size() > 0
|
||||
@@ -62,7 +62,7 @@ mod array_functions {
|
||||
array.resize(len as usize, item);
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
Ok(())
|
||||
}
|
||||
pub fn pop(array: &mut Array) -> Dynamic {
|
||||
array.pop().unwrap_or_else(|| ().into())
|
||||
@@ -169,7 +169,7 @@ mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
mapper: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
let mut ar = Array::with_capacity(max(TYPICAL_ARRAY_SIZE, array.len()));
|
||||
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
@@ -195,14 +195,14 @@ mod array_functions {
|
||||
);
|
||||
}
|
||||
|
||||
Ok(ar.into())
|
||||
Ok(ar)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn filter(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
let mut ar = Array::with_capacity(max(TYPICAL_ARRAY_SIZE, array.len()));
|
||||
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
@@ -231,14 +231,14 @@ mod array_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ar.into())
|
||||
Ok(ar)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn contains(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
value: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
for item in array.iter_mut() {
|
||||
if ctx
|
||||
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [item, &mut value.clone()])
|
||||
@@ -258,18 +258,18 @@ mod array_functions {
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return Ok(Dynamic::TRUE);
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Dynamic::FALSE)
|
||||
Ok(false)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn index_of(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
value: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
for (i, item) in array.iter_mut().enumerate() {
|
||||
if ctx
|
||||
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [item, &mut value.clone()])
|
||||
@@ -289,18 +289,18 @@ mod array_functions {
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return Ok((i as INT).into());
|
||||
return Ok(i as INT);
|
||||
}
|
||||
}
|
||||
|
||||
Ok((-1 as INT).into())
|
||||
Ok(-1 as INT)
|
||||
}
|
||||
#[rhai_fn(name = "index_of", return_raw, pure)]
|
||||
pub fn index_of_filter(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<INT, Box<EvalAltResult>> {
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
if filter
|
||||
.call_dynamic(&ctx, None, [item.clone()])
|
||||
@@ -323,18 +323,18 @@ mod array_functions {
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return Ok((i as INT).into());
|
||||
return Ok(i as INT);
|
||||
}
|
||||
}
|
||||
|
||||
Ok((-1 as INT).into())
|
||||
Ok(-1 as INT)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn some(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
if filter
|
||||
.call_dynamic(&ctx, None, [item.clone()])
|
||||
@@ -357,18 +357,18 @@ mod array_functions {
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return Ok(true.into());
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(false.into())
|
||||
Ok(false)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn all(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
if !filter
|
||||
.call_dynamic(&ctx, None, [item.clone()])
|
||||
@@ -391,11 +391,11 @@ mod array_functions {
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return Ok(false.into());
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(true.into())
|
||||
Ok(true)
|
||||
}
|
||||
#[rhai_fn(return_raw, pure)]
|
||||
pub fn reduce(
|
||||
@@ -403,7 +403,7 @@ mod array_functions {
|
||||
array: &mut Array,
|
||||
reducer: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let mut result: Dynamic = Dynamic::UNIT;
|
||||
let mut result = Dynamic::UNIT;
|
||||
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
result = reducer
|
||||
@@ -433,16 +433,9 @@ mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
reducer: FnPtr,
|
||||
initial: FnPtr,
|
||||
initial: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let mut result = initial.call_dynamic(&ctx, None, []).map_err(|err| {
|
||||
Box::new(EvalAltResult::ErrorInFunctionCall(
|
||||
"reduce".to_string(),
|
||||
ctx.source().unwrap_or("").to_string(),
|
||||
err,
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
let mut result = initial;
|
||||
|
||||
for (i, item) in array.iter().enumerate() {
|
||||
result = reducer
|
||||
@@ -473,7 +466,7 @@ mod array_functions {
|
||||
array: &mut Array,
|
||||
reducer: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let mut result: Dynamic = Dynamic::UNIT;
|
||||
let mut result = Dynamic::UNIT;
|
||||
|
||||
for (i, item) in array.iter().enumerate().rev() {
|
||||
result = reducer
|
||||
@@ -503,16 +496,9 @@ mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
reducer: FnPtr,
|
||||
initial: FnPtr,
|
||||
initial: Dynamic,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let mut result = initial.call_dynamic(&ctx, None, []).map_err(|err| {
|
||||
Box::new(EvalAltResult::ErrorInFunctionCall(
|
||||
"reduce_rev".to_string(),
|
||||
ctx.source().unwrap_or("").to_string(),
|
||||
err,
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
let mut result = initial;
|
||||
|
||||
for (i, item) in array.iter().enumerate().rev() {
|
||||
result = reducer
|
||||
@@ -542,7 +528,7 @@ mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
comparer: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
array.sort_by(|x, y| {
|
||||
comparer
|
||||
.call_dynamic(&ctx, None, [x.clone(), y.clone()])
|
||||
@@ -571,14 +557,14 @@ mod array_functions {
|
||||
})
|
||||
});
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
Ok(())
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn drain(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
let mut drained = Array::with_capacity(max(TYPICAL_ARRAY_SIZE, array.len()));
|
||||
|
||||
let mut i = array.len();
|
||||
@@ -611,7 +597,7 @@ mod array_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(drained.into())
|
||||
Ok(drained)
|
||||
}
|
||||
#[rhai_fn(name = "drain")]
|
||||
pub fn drain_range(array: &mut Array, start: INT, len: INT) -> Array {
|
||||
@@ -638,7 +624,7 @@ mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
filter: FnPtr,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<Array, Box<EvalAltResult>> {
|
||||
let mut drained = Array::with_capacity(max(TYPICAL_ARRAY_SIZE, array.len()));
|
||||
|
||||
let mut i = array.len();
|
||||
@@ -671,7 +657,7 @@ mod array_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(drained.into())
|
||||
Ok(drained)
|
||||
}
|
||||
#[rhai_fn(name = "retain")]
|
||||
pub fn retain_range(array: &mut Array, start: INT, len: INT) -> Array {
|
||||
@@ -701,12 +687,12 @@ mod array_functions {
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
mut array2: Array,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
if array.len() != array2.len() {
|
||||
return Ok(false.into());
|
||||
return Ok(false);
|
||||
}
|
||||
if array.is_empty() {
|
||||
return Ok(true.into());
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
for (a1, a2) in array.iter_mut().zip(array2.iter_mut()) {
|
||||
@@ -728,18 +714,18 @@ mod array_functions {
|
||||
.as_bool()
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return Ok(Dynamic::FALSE);
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Dynamic::TRUE)
|
||||
Ok(true)
|
||||
}
|
||||
#[rhai_fn(name = "!=", return_raw, pure)]
|
||||
pub fn not_equals(
|
||||
ctx: NativeCallContext,
|
||||
array: &mut Array,
|
||||
array2: Array,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
equals(ctx, array, array2).map(|r| (!r.as_bool().unwrap()).into())
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
equals(ctx, array, array2).map(|r| !r)
|
||||
}
|
||||
}
|
||||
|
@@ -46,12 +46,12 @@ mod map_functions {
|
||||
ctx: NativeCallContext,
|
||||
map: &mut Map,
|
||||
mut map2: Map,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
if map.len() != map2.len() {
|
||||
return Ok(false.into());
|
||||
return Ok(false);
|
||||
}
|
||||
if map.is_empty() {
|
||||
return Ok(true.into());
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
for (m1, v1) in map.iter_mut() {
|
||||
@@ -61,22 +61,22 @@ mod map_functions {
|
||||
.map(|v| v.as_bool().unwrap_or(false))?;
|
||||
|
||||
if !equals {
|
||||
return Ok(false.into());
|
||||
return Ok(false);
|
||||
}
|
||||
} else {
|
||||
return Ok(false.into());
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(true.into())
|
||||
Ok(true)
|
||||
}
|
||||
#[rhai_fn(name = "!=", return_raw, pure)]
|
||||
pub fn not_equals(
|
||||
ctx: NativeCallContext,
|
||||
map: &mut Map,
|
||||
map2: Map,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
equals(ctx, map, map2).map(|r| (!r.as_bool().unwrap()).into())
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
equals(ctx, map, map2).map(|r| !r)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
|
@@ -113,7 +113,7 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
||||
#[export_module]
|
||||
mod int_functions {
|
||||
#[rhai_fn(name = "parse_int", return_raw)]
|
||||
pub fn parse_int_radix(s: &str, radix: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn parse_int_radix(s: &str, radix: INT) -> Result<INT, Box<EvalAltResult>> {
|
||||
if radix < 2 || radix > 36 {
|
||||
return EvalAltResult::ErrorArithmetic(
|
||||
format!("Invalid radix: '{}'", radix),
|
||||
@@ -122,18 +122,16 @@ mod int_functions {
|
||||
.into();
|
||||
}
|
||||
|
||||
INT::from_str_radix(s.trim(), radix as u32)
|
||||
.map(Into::<Dynamic>::into)
|
||||
.map_err(|err| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Error parsing integer number '{}': {}", s, err),
|
||||
Position::NONE,
|
||||
)
|
||||
.into()
|
||||
})
|
||||
INT::from_str_radix(s.trim(), radix as u32).map_err(|err| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Error parsing integer number '{}': {}", s, err),
|
||||
Position::NONE,
|
||||
)
|
||||
.into()
|
||||
})
|
||||
}
|
||||
#[rhai_fn(name = "parse_int", return_raw)]
|
||||
pub fn parse_int(s: &str) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn parse_int(s: &str) -> Result<INT, Box<EvalAltResult>> {
|
||||
parse_int_radix(s, 10)
|
||||
}
|
||||
}
|
||||
@@ -261,7 +259,7 @@ mod float_functions {
|
||||
x.is_infinite()
|
||||
}
|
||||
#[rhai_fn(name = "to_int", return_raw)]
|
||||
pub fn f32_to_int(x: f32) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn f32_to_int(x: f32) -> Result<INT, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f32) {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Integer overflow: to_int({})", x),
|
||||
@@ -269,11 +267,11 @@ mod float_functions {
|
||||
)
|
||||
.into()
|
||||
} else {
|
||||
Ok((x.trunc() as INT).into())
|
||||
Ok(x.trunc() as INT)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(name = "to_int", return_raw)]
|
||||
pub fn f64_to_int(x: f64) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn f64_to_int(x: f64) -> Result<INT, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f64) {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Integer overflow: to_int({})", x),
|
||||
@@ -281,21 +279,18 @@ mod float_functions {
|
||||
)
|
||||
.into()
|
||||
} else {
|
||||
Ok((x.trunc() as INT).into())
|
||||
Ok(x.trunc() as INT)
|
||||
}
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn parse_float(s: &str) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
s.trim()
|
||||
.parse::<FLOAT>()
|
||||
.map(Into::<Dynamic>::into)
|
||||
.map_err(|err| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Error parsing floating-point number '{}': {}", s, err),
|
||||
Position::NONE,
|
||||
)
|
||||
.into()
|
||||
})
|
||||
pub fn parse_float(s: &str) -> Result<f64, Box<EvalAltResult>> {
|
||||
s.trim().parse::<FLOAT>().map_err(|err| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Error parsing floating-point number '{}': {}", s, err),
|
||||
Position::NONE,
|
||||
)
|
||||
.into()
|
||||
})
|
||||
}
|
||||
#[cfg(not(feature = "f32_float"))]
|
||||
pub mod f32_f64 {
|
||||
@@ -327,10 +322,10 @@ mod decimal_functions {
|
||||
x.round()
|
||||
}
|
||||
#[rhai_fn(name = "round", return_raw)]
|
||||
pub fn round_dp(x: Decimal, dp: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn round_dp(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) {
|
||||
return Ok(x.into());
|
||||
return Ok(x);
|
||||
}
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -340,13 +335,13 @@ mod decimal_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(x.round_dp(dp as u32).into())
|
||||
Ok(x.round_dp(dp as u32))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn round_up(x: Decimal, dp: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn round_up(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) {
|
||||
return Ok(x.into());
|
||||
return Ok(x);
|
||||
}
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -356,16 +351,13 @@ mod decimal_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(
|
||||
x.round_dp_with_strategy(dp as u32, RoundingStrategy::RoundUp)
|
||||
.into(),
|
||||
)
|
||||
Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::RoundUp))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn round_down(x: Decimal, dp: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn round_down(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) {
|
||||
return Ok(x.into());
|
||||
return Ok(x);
|
||||
}
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -375,16 +367,13 @@ mod decimal_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(
|
||||
x.round_dp_with_strategy(dp as u32, RoundingStrategy::RoundDown)
|
||||
.into(),
|
||||
)
|
||||
Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::RoundDown))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn round_half_up(x: Decimal, dp: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn round_half_up(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) {
|
||||
return Ok(x.into());
|
||||
return Ok(x);
|
||||
}
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -394,16 +383,13 @@ mod decimal_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(
|
||||
x.round_dp_with_strategy(dp as u32, RoundingStrategy::RoundHalfUp)
|
||||
.into(),
|
||||
)
|
||||
Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::RoundHalfUp))
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn round_half_down(x: Decimal, dp: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn round_half_down(x: Decimal, dp: INT) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
if cfg!(not(feature = "unchecked")) {
|
||||
if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) {
|
||||
return Ok(x.into());
|
||||
return Ok(x);
|
||||
}
|
||||
if dp < 0 {
|
||||
return Err(make_err(format!(
|
||||
@@ -413,10 +399,7 @@ mod decimal_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(
|
||||
x.round_dp_with_strategy(dp as u32, RoundingStrategy::RoundHalfDown)
|
||||
.into(),
|
||||
)
|
||||
Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::RoundHalfDown))
|
||||
}
|
||||
#[rhai_fn(name = "int", get = "int")]
|
||||
pub fn int(x: Decimal) -> Decimal {
|
||||
@@ -427,10 +410,9 @@ mod decimal_functions {
|
||||
x.fract()
|
||||
}
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn parse_decimal(s: &str) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn parse_decimal(s: &str) -> Result<Decimal, Box<EvalAltResult>> {
|
||||
Decimal::from_str(s)
|
||||
.or_else(|_| Decimal::from_scientific(s))
|
||||
.map(Into::<Dynamic>::into)
|
||||
.map_err(|err| {
|
||||
EvalAltResult::ErrorArithmetic(
|
||||
format!("Error parsing decimal number '{}': {}", s, err),
|
||||
|
@@ -235,7 +235,7 @@ mod string_functions {
|
||||
string: &mut ImmutableString,
|
||||
len: INT,
|
||||
character: char,
|
||||
) -> Result<Dynamic, Box<crate::EvalAltResult>> {
|
||||
) -> Result<(), Box<crate::EvalAltResult>> {
|
||||
// Check if string will be over max size limit
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
|
||||
@@ -269,7 +269,7 @@ mod string_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
Ok(())
|
||||
}
|
||||
#[rhai_fn(name = "pad", return_raw)]
|
||||
pub fn pad_with_string(
|
||||
@@ -277,7 +277,7 @@ mod string_functions {
|
||||
string: &mut ImmutableString,
|
||||
len: INT,
|
||||
padding: &str,
|
||||
) -> Result<Dynamic, Box<crate::EvalAltResult>> {
|
||||
) -> Result<(), Box<crate::EvalAltResult>> {
|
||||
// Check if string will be over max size limit
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
|
||||
@@ -318,7 +318,7 @@ mod string_functions {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
|
@@ -143,28 +143,28 @@ mod time_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, name = "+")]
|
||||
pub fn add(timestamp: Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
add_impl(timestamp, seconds).map(Into::<Dynamic>::into)
|
||||
pub fn add(timestamp: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
add_impl(timestamp, seconds)
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "+=")]
|
||||
pub fn add_assign(
|
||||
timestamp: &mut Instant,
|
||||
seconds: FLOAT,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
*timestamp = add_impl(*timestamp, seconds)?;
|
||||
Ok(Dynamic::UNIT)
|
||||
Ok(())
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "-")]
|
||||
pub fn subtract(timestamp: Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
subtract_impl(timestamp, seconds).map(Into::<Dynamic>::into)
|
||||
pub fn subtract(timestamp: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
subtract_impl(timestamp, seconds)
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "-=")]
|
||||
pub fn subtract_assign(
|
||||
timestamp: &mut Instant,
|
||||
seconds: FLOAT,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
*timestamp = subtract_impl(*timestamp, seconds)?;
|
||||
Ok(Dynamic::UNIT)
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,28 +202,25 @@ mod time_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, name = "+")]
|
||||
pub fn add(timestamp: Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
add_impl(timestamp, seconds).map(Into::<Dynamic>::into)
|
||||
pub fn add(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
add_impl(timestamp, seconds)
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "+=")]
|
||||
pub fn add_assign(
|
||||
timestamp: &mut Instant,
|
||||
seconds: INT,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
pub fn add_assign(timestamp: &mut Instant, seconds: INT) -> Result<(), Box<EvalAltResult>> {
|
||||
*timestamp = add_impl(*timestamp, seconds)?;
|
||||
Ok(Dynamic::UNIT)
|
||||
Ok(())
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "-")]
|
||||
pub fn subtract(timestamp: Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
subtract_impl(timestamp, seconds).map(Into::<Dynamic>::into)
|
||||
pub fn subtract(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
|
||||
subtract_impl(timestamp, seconds)
|
||||
}
|
||||
#[rhai_fn(return_raw, name = "-=")]
|
||||
pub fn subtract_assign(
|
||||
timestamp: &mut Instant,
|
||||
seconds: INT,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
*timestamp = subtract_impl(*timestamp, seconds)?;
|
||||
Ok(Dynamic::UNIT)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "==")]
|
||||
|
Reference in New Issue
Block a user