Reformat.

This commit is contained in:
Stephen Chung 2022-10-24 23:33:33 +08:00
parent d55b5a91f7
commit 05b9c83bb8

View File

@ -2,8 +2,10 @@
use super::call::FnCallArgs; use super::call::FnCallArgs;
use super::native::FnBuiltin; use super::native::FnBuiltin;
use crate::tokenizer::Token; use crate::tokenizer::{Token, Token::*};
use crate::{Dynamic, ExclusiveRange, ImmutableString, InclusiveRange, INT}; use crate::{
Dynamic, ExclusiveRange, ImmutableString, InclusiveRange, NativeCallContext, RhaiResult, INT,
};
use std::any::TypeId; use std::any::TypeId;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -63,6 +65,19 @@ fn is_numeric(type_id: TypeId) -> bool {
false false
} }
/// A function that returns `true`.
#[inline(always)]
#[must_use]
fn const_true_fn(_: NativeCallContext, _: &mut [&mut Dynamic]) -> RhaiResult {
Ok(Dynamic::TRUE)
}
/// A function that returns `false`.
#[inline(always)]
#[must_use]
fn const_false_fn(_: NativeCallContext, _: &mut [&mut Dynamic]) -> RhaiResult {
Ok(Dynamic::FALSE)
}
/// 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. /// The return function will be registered as a _method_, so the first parameter cannot be consumed.
@ -131,46 +146,46 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
match op { match op {
Token::Plus => return Some(impl_op!(INT => add(as_int, as_int))), Plus => return Some(impl_op!(INT => add(as_int, as_int))),
Token::Minus => return Some(impl_op!(INT => subtract(as_int, as_int))), Minus => return Some(impl_op!(INT => subtract(as_int, as_int))),
Token::Multiply => return Some(impl_op!(INT => multiply(as_int, as_int))), Multiply => return Some(impl_op!(INT => multiply(as_int, as_int))),
Token::Divide => return Some(impl_op!(INT => divide(as_int, as_int))), Divide => return Some(impl_op!(INT => divide(as_int, as_int))),
Token::Modulo => return Some(impl_op!(INT => modulo(as_int, as_int))), Modulo => return Some(impl_op!(INT => modulo(as_int, as_int))),
Token::PowerOf => return Some(impl_op!(INT => power(as_int, as_int))), PowerOf => return Some(impl_op!(INT => power(as_int, as_int))),
Token::RightShift => return Some(impl_op!(INT => shift_right(as_int, as_int))), RightShift => return Some(impl_op!(INT => shift_right(as_int, as_int))),
Token::LeftShift => return Some(impl_op!(INT => shift_left(as_int, as_int))), LeftShift => return Some(impl_op!(INT => shift_left(as_int, as_int))),
_ => (), _ => (),
} }
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
match op { match op {
Token::Plus => return Some(impl_op!(INT => as_int + as_int)), Plus => return Some(impl_op!(INT => as_int + as_int)),
Token::Minus => return Some(impl_op!(INT => as_int - as_int)), Minus => return Some(impl_op!(INT => as_int - as_int)),
Token::Multiply => return Some(impl_op!(INT => as_int * as_int)), Multiply => return Some(impl_op!(INT => as_int * as_int)),
Token::Divide => return Some(impl_op!(INT => as_int / as_int)), Divide => return Some(impl_op!(INT => as_int / as_int)),
Token::Modulo => return Some(impl_op!(INT => as_int % as_int)), Modulo => return Some(impl_op!(INT => as_int % as_int)),
Token::PowerOf => return Some(impl_op!(INT => as_int.pow(as_int as u32))), PowerOf => return Some(impl_op!(INT => as_int.pow(as_int as u32))),
Token::RightShift => return Some(impl_op!(INT => as_int >> as_int)), RightShift => return Some(impl_op!(INT => as_int >> as_int)),
Token::LeftShift => return Some(impl_op!(INT => as_int << as_int)), LeftShift => return Some(impl_op!(INT => as_int << as_int)),
_ => (), _ => (),
} }
return match op { return match op {
Token::EqualsTo => Some(impl_op!(INT => as_int == as_int)), EqualsTo => Some(impl_op!(INT => as_int == as_int)),
Token::NotEqualsTo => Some(impl_op!(INT => as_int != as_int)), NotEqualsTo => Some(impl_op!(INT => as_int != as_int)),
Token::GreaterThan => Some(impl_op!(INT => as_int > as_int)), GreaterThan => Some(impl_op!(INT => as_int > as_int)),
Token::GreaterThanEqualsTo => Some(impl_op!(INT => as_int >= as_int)), GreaterThanEqualsTo => Some(impl_op!(INT => as_int >= as_int)),
Token::LessThan => Some(impl_op!(INT => as_int < as_int)), LessThan => Some(impl_op!(INT => as_int < as_int)),
Token::LessThanEqualsTo => Some(impl_op!(INT => as_int <= as_int)), LessThanEqualsTo => Some(impl_op!(INT => as_int <= as_int)),
Token::Ampersand => Some(impl_op!(INT => as_int & as_int)), Ampersand => Some(impl_op!(INT => as_int & as_int)),
Token::Pipe => Some(impl_op!(INT => as_int | as_int)), Pipe => Some(impl_op!(INT => as_int | as_int)),
Token::XOr => Some(impl_op!(INT => as_int ^ as_int)), XOr => Some(impl_op!(INT => as_int ^ as_int)),
Token::ExclusiveRange => Some(|_, args| { ExclusiveRange => Some(|_, args| {
let x = args[0].as_int().expect(BUILTIN); let x = args[0].as_int().expect(BUILTIN);
let y = args[1].as_int().expect(BUILTIN); let y = args[1].as_int().expect(BUILTIN);
Ok((x..y).into()) Ok((x..y).into())
}), }),
Token::InclusiveRange => Some(|_, args| { InclusiveRange => Some(|_, args| {
let x = args[0].as_int().expect(BUILTIN); let x = args[0].as_int().expect(BUILTIN);
let y = args[1].as_int().expect(BUILTIN); let y = args[1].as_int().expect(BUILTIN);
Ok((x..=y).into()) Ok((x..=y).into())
@ -181,22 +196,22 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
if type1 == TypeId::of::<bool>() { if type1 == TypeId::of::<bool>() {
return match op { return match op {
Token::EqualsTo => Some(impl_op!(bool => as_bool == as_bool)), EqualsTo => Some(impl_op!(bool => as_bool == as_bool)),
Token::NotEqualsTo => Some(impl_op!(bool => as_bool != as_bool)), NotEqualsTo => Some(impl_op!(bool => as_bool != as_bool)),
Token::GreaterThan => Some(impl_op!(bool => as_bool > as_bool)), GreaterThan => Some(impl_op!(bool => as_bool > as_bool)),
Token::GreaterThanEqualsTo => Some(impl_op!(bool => as_bool >= as_bool)), GreaterThanEqualsTo => Some(impl_op!(bool => as_bool >= as_bool)),
Token::LessThan => Some(impl_op!(bool => as_bool < as_bool)), LessThan => Some(impl_op!(bool => as_bool < as_bool)),
Token::LessThanEqualsTo => Some(impl_op!(bool => as_bool <= as_bool)), LessThanEqualsTo => Some(impl_op!(bool => as_bool <= as_bool)),
Token::Ampersand => Some(impl_op!(bool => as_bool & as_bool)), Ampersand => Some(impl_op!(bool => as_bool & as_bool)),
Token::Pipe => Some(impl_op!(bool => as_bool | as_bool)), Pipe => Some(impl_op!(bool => as_bool | as_bool)),
Token::XOr => Some(impl_op!(bool => as_bool ^ as_bool)), XOr => Some(impl_op!(bool => as_bool ^ as_bool)),
_ => None, _ => None,
}; };
} }
if type1 == TypeId::of::<ImmutableString>() { if type1 == TypeId::of::<ImmutableString>() {
return match op { return match op {
Token::Plus => Some(|_ctx, args| { Plus => Some(|_ctx, args| {
let s1 = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN); let s1 = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
let s2 = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN); let s2 = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
@ -209,20 +224,20 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
Ok((s1 + s2).into()) Ok((s1 + s2).into())
}), }),
Token::Minus => Some(impl_op!(ImmutableString - ImmutableString)), Minus => Some(impl_op!(ImmutableString - ImmutableString)),
Token::EqualsTo => Some(impl_op!(ImmutableString == ImmutableString)), EqualsTo => Some(impl_op!(ImmutableString == ImmutableString)),
Token::NotEqualsTo => Some(impl_op!(ImmutableString != ImmutableString)), NotEqualsTo => Some(impl_op!(ImmutableString != ImmutableString)),
Token::GreaterThan => Some(impl_op!(ImmutableString > ImmutableString)), GreaterThan => Some(impl_op!(ImmutableString > ImmutableString)),
Token::GreaterThanEqualsTo => Some(impl_op!(ImmutableString >= ImmutableString)), GreaterThanEqualsTo => Some(impl_op!(ImmutableString >= ImmutableString)),
Token::LessThan => Some(impl_op!(ImmutableString < ImmutableString)), LessThan => Some(impl_op!(ImmutableString < ImmutableString)),
Token::LessThanEqualsTo => Some(impl_op!(ImmutableString <= ImmutableString)), LessThanEqualsTo => Some(impl_op!(ImmutableString <= ImmutableString)),
_ => None, _ => None,
}; };
} }
if type1 == TypeId::of::<char>() { if type1 == TypeId::of::<char>() {
return match op { return match op {
Token::Plus => Some(|_ctx, args| { Plus => Some(|_ctx, args| {
let x = args[0].as_char().expect(BUILTIN); let x = args[0].as_char().expect(BUILTIN);
let y = args[1].as_char().expect(BUILTIN); let y = args[1].as_char().expect(BUILTIN);
@ -234,12 +249,12 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
Ok(result.into()) Ok(result.into())
}), }),
Token::EqualsTo => Some(impl_op!(char => as_char == as_char)), EqualsTo => Some(impl_op!(char => as_char == as_char)),
Token::NotEqualsTo => Some(impl_op!(char => as_char != as_char)), NotEqualsTo => Some(impl_op!(char => as_char != as_char)),
Token::GreaterThan => Some(impl_op!(char => as_char > as_char)), GreaterThan => Some(impl_op!(char => as_char > as_char)),
Token::GreaterThanEqualsTo => Some(impl_op!(char => as_char >= as_char)), GreaterThanEqualsTo => Some(impl_op!(char => as_char >= as_char)),
Token::LessThan => Some(impl_op!(char => as_char < as_char)), LessThan => Some(impl_op!(char => as_char < as_char)),
Token::LessThanEqualsTo => Some(impl_op!(char => as_char <= as_char)), LessThanEqualsTo => Some(impl_op!(char => as_char <= as_char)),
_ => None, _ => None,
}; };
} }
@ -249,7 +264,7 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
use crate::Blob; use crate::Blob;
return match op { return match op {
Token::Plus => Some(|_ctx, args| { Plus => Some(|_ctx, args| {
let blob1 = &*args[0].read_lock::<Blob>().expect(BUILTIN); let blob1 = &*args[0].read_lock::<Blob>().expect(BUILTIN);
let blob2 = &*args[1].read_lock::<Blob>().expect(BUILTIN); let blob2 = &*args[1].read_lock::<Blob>().expect(BUILTIN);
@ -270,20 +285,18 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
blob blob
})) }))
}), }),
Token::EqualsTo => Some(impl_op!(Blob == Blob)), EqualsTo => Some(impl_op!(Blob == Blob)),
Token::NotEqualsTo => Some(impl_op!(Blob != Blob)), NotEqualsTo => Some(impl_op!(Blob != Blob)),
_ => None, _ => None,
}; };
} }
if type1 == TypeId::of::<()>() { if type1 == TypeId::of::<()>() {
return match op { return match op {
Token::EqualsTo => Some(|_, _| Ok(Dynamic::TRUE)), EqualsTo => Some(const_true_fn),
Token::NotEqualsTo NotEqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
| Token::GreaterThan Some(const_false_fn)
| Token::GreaterThanEqualsTo }
| Token::LessThan
| Token::LessThanEqualsTo => Some(|_, _| Ok(Dynamic::FALSE)),
_ => None, _ => None,
}; };
} }
@ -294,19 +307,19 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
($x:ty, $xx:ident, $y:ty, $yy:ident) => { ($x:ty, $xx:ident, $y:ty, $yy:ident) => {
if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) { if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
return match op { return match op {
Token::Plus => Some(impl_op!(FLOAT => $xx + $yy)), Plus => Some(impl_op!(FLOAT => $xx + $yy)),
Token::Minus => Some(impl_op!(FLOAT => $xx - $yy)), Minus => Some(impl_op!(FLOAT => $xx - $yy)),
Token::Multiply => Some(impl_op!(FLOAT => $xx * $yy)), Multiply => Some(impl_op!(FLOAT => $xx * $yy)),
Token::Divide => Some(impl_op!(FLOAT => $xx / $yy)), Divide => Some(impl_op!(FLOAT => $xx / $yy)),
Token::Modulo => Some(impl_op!(FLOAT => $xx % $yy)), Modulo => Some(impl_op!(FLOAT => $xx % $yy)),
Token::PowerOf => Some(impl_op!(FLOAT => $xx.powf($yy as FLOAT))), PowerOf => Some(impl_op!(FLOAT => $xx.powf($yy as FLOAT))),
Token::EqualsTo => Some(impl_op!(FLOAT => $xx == $yy)), EqualsTo => Some(impl_op!(FLOAT => $xx == $yy)),
Token::NotEqualsTo => Some(impl_op!(FLOAT => $xx != $yy)), NotEqualsTo => Some(impl_op!(FLOAT => $xx != $yy)),
Token::GreaterThan => Some(impl_op!(FLOAT => $xx > $yy)), GreaterThan => Some(impl_op!(FLOAT => $xx > $yy)),
Token::GreaterThanEqualsTo => Some(impl_op!(FLOAT => $xx >= $yy)), GreaterThanEqualsTo => Some(impl_op!(FLOAT => $xx >= $yy)),
Token::LessThan => Some(impl_op!(FLOAT => $xx < $yy)), LessThan => Some(impl_op!(FLOAT => $xx < $yy)),
Token::LessThanEqualsTo => Some(impl_op!(FLOAT => $xx <= $yy)), LessThanEqualsTo => Some(impl_op!(FLOAT => $xx <= $yy)),
_ => None, _ => None,
}; };
} }
}; };
@ -328,13 +341,13 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
match op { match op {
Token::Plus => return Some(impl_op!(from Decimal => add($xx, $yy))), Plus => return Some(impl_op!(from Decimal => add($xx, $yy))),
Token::Minus => return Some(impl_op!(from Decimal => subtract($xx, $yy))), Minus => return Some(impl_op!(from Decimal => subtract($xx, $yy))),
Token::Multiply => return Some(impl_op!(from Decimal => multiply($xx, $yy))), Multiply => return Some(impl_op!(from Decimal => multiply($xx, $yy))),
Token::Divide => return Some(impl_op!(from Decimal => divide($xx, $yy))), Divide => return Some(impl_op!(from Decimal => divide($xx, $yy))),
Token::Modulo => return Some(impl_op!(from Decimal => modulo($xx, $yy))), Modulo => return Some(impl_op!(from Decimal => modulo($xx, $yy))),
Token::PowerOf => return Some(impl_op!(from Decimal => power($xx, $yy))), PowerOf => return Some(impl_op!(from Decimal => power($xx, $yy))),
_ => () _ => ()
} }
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
@ -342,23 +355,23 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
match op { match op {
Token::Plus => return Some(impl_op!(from Decimal => $xx + $yy)), Plus => return Some(impl_op!(from Decimal => $xx + $yy)),
Token::Minus => return Some(impl_op!(from Decimal => $xx - $yy)), Minus => return Some(impl_op!(from Decimal => $xx - $yy)),
Token::Multiply => return Some(impl_op!(from Decimal => $xx * $yy)), Multiply => return Some(impl_op!(from Decimal => $xx * $yy)),
Token::Divide => return Some(impl_op!(from Decimal => $xx / $yy)), Divide => return Some(impl_op!(from Decimal => $xx / $yy)),
Token::Modulo => return Some(impl_op!(from Decimal => $xx % $yy)), Modulo => return Some(impl_op!(from Decimal => $xx % $yy)),
Token::PowerOf => return Some(impl_op!(from Decimal => $xx.powd($yy))), PowerOf => return Some(impl_op!(from Decimal => $xx.powd($yy))),
_ => () _ => ()
} }
return match op { return match op {
Token::EqualsTo => Some(impl_op!(from Decimal => $xx == $yy)), EqualsTo => Some(impl_op!(from Decimal => $xx == $yy)),
Token::NotEqualsTo => Some(impl_op!(from Decimal => $xx != $yy)), NotEqualsTo => Some(impl_op!(from Decimal => $xx != $yy)),
Token::GreaterThan => Some(impl_op!(from Decimal => $xx > $yy)), GreaterThan => Some(impl_op!(from Decimal => $xx > $yy)),
Token::GreaterThanEqualsTo => Some(impl_op!(from Decimal => $xx >= $yy)), GreaterThanEqualsTo => Some(impl_op!(from Decimal => $xx >= $yy)),
Token::LessThan => Some(impl_op!(from Decimal => $xx < $yy)), LessThan => Some(impl_op!(from Decimal => $xx < $yy)),
Token::LessThanEqualsTo => Some(impl_op!(from Decimal => $xx <= $yy)), LessThanEqualsTo => Some(impl_op!(from Decimal => $xx <= $yy)),
_ => None _ => None
}; };
} }
}; };
@ -383,7 +396,7 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
} }
return match op { return match op {
Token::Plus => Some(|_ctx, args| { Plus => Some(|_ctx, args| {
let x = args[0].as_char().expect(BUILTIN); let x = args[0].as_char().expect(BUILTIN);
let y = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN); let y = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
let result = format!("{x}{y}"); let result = format!("{x}{y}");
@ -394,12 +407,12 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
Ok(result.into()) Ok(result.into())
}), }),
Token::EqualsTo => Some(impl_op!(get_s1s2(==))), EqualsTo => Some(impl_op!(get_s1s2(==))),
Token::NotEqualsTo => Some(impl_op!(get_s1s2(!=))), NotEqualsTo => Some(impl_op!(get_s1s2(!=))),
Token::GreaterThan => Some(impl_op!(get_s1s2(>))), GreaterThan => Some(impl_op!(get_s1s2(>))),
Token::GreaterThanEqualsTo => Some(impl_op!(get_s1s2(>=))), GreaterThanEqualsTo => Some(impl_op!(get_s1s2(>=))),
Token::LessThan => Some(impl_op!(get_s1s2(<))), LessThan => Some(impl_op!(get_s1s2(<))),
Token::LessThanEqualsTo => Some(impl_op!(get_s1s2(<=))), LessThanEqualsTo => Some(impl_op!(get_s1s2(<=))),
_ => None, _ => None,
}; };
} }
@ -415,7 +428,7 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
} }
return match op { return match op {
Token::Plus => Some(|_ctx, args| { Plus => Some(|_ctx, args| {
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN); let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
let y = args[1].as_char().expect(BUILTIN); let y = args[1].as_char().expect(BUILTIN);
let result = x + y; let result = x + y;
@ -426,43 +439,39 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
Ok(result.into()) Ok(result.into())
}), }),
Token::Minus => Some(|_, args| { Minus => Some(|_, args| {
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN); let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
let y = args[1].as_char().expect(BUILTIN); let y = args[1].as_char().expect(BUILTIN);
Ok((x - y).into()) Ok((x - y).into())
}), }),
Token::EqualsTo => Some(impl_op!(get_s1s2(==))), EqualsTo => Some(impl_op!(get_s1s2(==))),
Token::NotEqualsTo => Some(impl_op!(get_s1s2(!=))), NotEqualsTo => Some(impl_op!(get_s1s2(!=))),
Token::GreaterThan => Some(impl_op!(get_s1s2(>))), GreaterThan => Some(impl_op!(get_s1s2(>))),
Token::GreaterThanEqualsTo => Some(impl_op!(get_s1s2(>=))), GreaterThanEqualsTo => Some(impl_op!(get_s1s2(>=))),
Token::LessThan => Some(impl_op!(get_s1s2(<))), LessThan => Some(impl_op!(get_s1s2(<))),
Token::LessThanEqualsTo => Some(impl_op!(get_s1s2(<=))), LessThanEqualsTo => Some(impl_op!(get_s1s2(<=))),
_ => None, _ => None,
}; };
} }
// () op string // () op string
if (type1, type2) == (TypeId::of::<()>(), TypeId::of::<ImmutableString>()) { if (type1, type2) == (TypeId::of::<()>(), TypeId::of::<ImmutableString>()) {
return match op { return match op {
Token::Plus => Some(|_, args| Ok(args[1].clone())), Plus => Some(|_, args| Ok(args[1].clone())),
Token::EqualsTo EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
| Token::GreaterThan Some(const_false_fn)
| Token::GreaterThanEqualsTo }
| Token::LessThan NotEqualsTo => Some(const_true_fn),
| Token::LessThanEqualsTo => Some(|_, _| Ok(Dynamic::FALSE)),
Token::NotEqualsTo => Some(|_, _| Ok(Dynamic::TRUE)),
_ => None, _ => None,
}; };
} }
// string op () // string op ()
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<()>()) { if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<()>()) {
return match op { return match op {
Token::Plus => Some(|_, args| Ok(args[0].clone())), Plus => Some(|_, args| Ok(args[0].clone())),
Token::EqualsTo EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
| Token::GreaterThan Some(const_false_fn)
| Token::GreaterThanEqualsTo }
| Token::LessThan NotEqualsTo => Some(const_true_fn),
| Token::LessThanEqualsTo => Some(|_, _| Ok(Dynamic::FALSE)),
Token::NotEqualsTo => Some(|_, _| Ok(Dynamic::TRUE)),
_ => None, _ => None,
}; };
} }
@ -474,9 +483,9 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
if type2 == TypeId::of::<char>() { if type2 == TypeId::of::<char>() {
return match op { return match op {
Token::Plus => Some(|_ctx, args| { Plus => Some(|_ctx, args| {
let mut buf = [0_u8; 4];
let mut blob = args[0].read_lock::<Blob>().expect(BUILTIN).clone(); let mut blob = args[0].read_lock::<Blob>().expect(BUILTIN).clone();
let mut buf = [0_u8; 4];
let x = args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf); let x = args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
@ -507,8 +516,8 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
) )
{ {
return match op { return match op {
Token::NotEqualsTo => Some(|_, _| Ok(Dynamic::TRUE)), NotEqualsTo => Some(const_true_fn),
Token::Equals => Some(|_, _| Ok(Dynamic::FALSE)), Equals => Some(const_false_fn),
_ => None, _ => None,
}; };
} }
@ -517,8 +526,8 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
if type1 == TypeId::of::<ExclusiveRange>() { if type1 == TypeId::of::<ExclusiveRange>() {
if type1 == type2 { if type1 == type2 {
return match op { return match op {
Token::EqualsTo => Some(impl_op!(ExclusiveRange == ExclusiveRange)), EqualsTo => Some(impl_op!(ExclusiveRange == ExclusiveRange)),
Token::NotEqualsTo => Some(impl_op!(ExclusiveRange != ExclusiveRange)), NotEqualsTo => Some(impl_op!(ExclusiveRange != ExclusiveRange)),
_ => None, _ => None,
}; };
} }
@ -527,8 +536,8 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
if type1 == TypeId::of::<InclusiveRange>() { if type1 == TypeId::of::<InclusiveRange>() {
if type1 == type2 { if type1 == type2 {
return match op { return match op {
Token::EqualsTo => Some(impl_op!(InclusiveRange == InclusiveRange)), EqualsTo => Some(impl_op!(InclusiveRange == InclusiveRange)),
Token::NotEqualsTo => Some(impl_op!(InclusiveRange != InclusiveRange)), NotEqualsTo => Some(impl_op!(InclusiveRange != InclusiveRange)),
_ => None, _ => None,
}; };
} }
@ -542,12 +551,10 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
} else if type1 != type2 { } else if type1 != type2 {
// If the types are not the same, default to not compare // If the types are not the same, default to not compare
match op { match op {
Token::NotEqualsTo => Some(|_, _| Ok(Dynamic::TRUE)), NotEqualsTo => Some(const_true_fn),
Token::EqualsTo EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
| Token::GreaterThan Some(const_false_fn)
| Token::GreaterThanEqualsTo }
| Token::LessThan
| Token::LessThanEqualsTo => Some(|_, _| Ok(Dynamic::FALSE)),
_ => None, _ => None,
} }
} else { } else {
@ -559,12 +566,10 @@ pub fn get_builtin_binary_op_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Option<
// Default comparison operators for different types // Default comparison operators for different types
if type2 != type1 { if type2 != type1 {
return match op { return match op {
Token::NotEqualsTo => Some(|_, _| Ok(Dynamic::TRUE)), NotEqualsTo => Some(const_true_fn),
Token::EqualsTo EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
| Token::GreaterThan Some(const_false_fn)
| Token::GreaterThanEqualsTo }
| Token::LessThan
| Token::LessThanEqualsTo => Some(|_, _| Ok(Dynamic::FALSE)),
_ => None, _ => None,
}; };
} }
@ -629,51 +634,49 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
match op { match op {
Token::PlusAssign => return Some(impl_op!(INT => add(as_int, as_int))), PlusAssign => return Some(impl_op!(INT => add(as_int, as_int))),
Token::MinusAssign => return Some(impl_op!(INT => subtract(as_int, as_int))), MinusAssign => return Some(impl_op!(INT => subtract(as_int, as_int))),
Token::MultiplyAssign => return Some(impl_op!(INT => multiply(as_int, as_int))), MultiplyAssign => return Some(impl_op!(INT => multiply(as_int, as_int))),
Token::DivideAssign => return Some(impl_op!(INT => divide(as_int, as_int))), DivideAssign => return Some(impl_op!(INT => divide(as_int, as_int))),
Token::ModuloAssign => return Some(impl_op!(INT => modulo(as_int, as_int))), ModuloAssign => return Some(impl_op!(INT => modulo(as_int, as_int))),
Token::PowerOfAssign => return Some(impl_op!(INT => power(as_int, as_int))), PowerOfAssign => return Some(impl_op!(INT => power(as_int, as_int))),
Token::RightShiftAssign => { RightShiftAssign => return Some(impl_op!(INT => shift_right(as_int, as_int))),
return Some(impl_op!(INT => shift_right(as_int, as_int))) LeftShiftAssign => return Some(impl_op!(INT => shift_left(as_int, as_int))),
}
Token::LeftShiftAssign => return Some(impl_op!(INT => shift_left(as_int, as_int))),
_ => (), _ => (),
} }
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
match op { match op {
Token::PlusAssign => return Some(impl_op!(INT += as_int)), PlusAssign => return Some(impl_op!(INT += as_int)),
Token::MinusAssign => return Some(impl_op!(INT -= as_int)), MinusAssign => return Some(impl_op!(INT -= as_int)),
Token::MultiplyAssign => return Some(impl_op!(INT *= as_int)), MultiplyAssign => return Some(impl_op!(INT *= as_int)),
Token::DivideAssign => return Some(impl_op!(INT /= as_int)), DivideAssign => return Some(impl_op!(INT /= as_int)),
Token::ModuloAssign => return Some(impl_op!(INT %= as_int)), ModuloAssign => return Some(impl_op!(INT %= as_int)),
Token::PowerOfAssign => return Some(impl_op!(INT => as_int.pow(as_int as u32))), PowerOfAssign => return Some(impl_op!(INT => as_int.pow(as_int as u32))),
Token::RightShiftAssign => return Some(impl_op!(INT >>= as_int)), RightShiftAssign => return Some(impl_op!(INT >>= as_int)),
Token::LeftShiftAssign => return Some(impl_op!(INT <<= as_int)), LeftShiftAssign => return Some(impl_op!(INT <<= as_int)),
_ => (), _ => (),
} }
return match op { return match op {
Token::AndAssign => Some(impl_op!(INT &= as_int)), AndAssign => Some(impl_op!(INT &= as_int)),
Token::OrAssign => Some(impl_op!(INT |= as_int)), OrAssign => Some(impl_op!(INT |= as_int)),
Token::XOrAssign => Some(impl_op!(INT ^= as_int)), XOrAssign => Some(impl_op!(INT ^= as_int)),
_ => None, _ => None,
}; };
} }
if type1 == TypeId::of::<bool>() { if type1 == TypeId::of::<bool>() {
return match op { return match op {
Token::AndAssign => Some(impl_op!(bool = x && as_bool)), AndAssign => Some(impl_op!(bool = x && as_bool)),
Token::OrAssign => Some(impl_op!(bool = x || as_bool)), OrAssign => Some(impl_op!(bool = x || as_bool)),
_ => None, _ => None,
}; };
} }
if type1 == TypeId::of::<char>() { if type1 == TypeId::of::<char>() {
return match op { return match op {
Token::PlusAssign => Some(|_, args| { PlusAssign => Some(|_, args| {
let y = args[1].as_char().expect(BUILTIN); let y = args[1].as_char().expect(BUILTIN);
let x = &mut *args[0].write_lock::<Dynamic>().expect(BUILTIN); let x = &mut *args[0].write_lock::<Dynamic>().expect(BUILTIN);
Ok((*x = format!("{x}{y}").into()).into()) Ok((*x = format!("{x}{y}").into()).into())
@ -684,7 +687,7 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
if type1 == TypeId::of::<ImmutableString>() { if type1 == TypeId::of::<ImmutableString>() {
return match op { return match op {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
let (first, second) = args.split_first_mut().expect(BUILTIN); let (first, second) = args.split_first_mut().expect(BUILTIN);
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN); let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
let y = std::mem::take(second[0]).cast::<ImmutableString>(); let y = std::mem::take(second[0]).cast::<ImmutableString>();
@ -698,7 +701,7 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
Ok((*x += y).into()) Ok((*x += y).into())
}), }),
Token::MinusAssign => Some(|_, args| { MinusAssign => Some(|_, args| {
let (first, second) = args.split_first_mut().expect(BUILTIN); let (first, second) = args.split_first_mut().expect(BUILTIN);
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN); let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
let y = std::mem::take(second[0]).cast::<ImmutableString>(); let y = std::mem::take(second[0]).cast::<ImmutableString>();
@ -714,29 +717,26 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
use crate::Array; use crate::Array;
return match op { return match op {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
let x = std::mem::take(args[1]).cast::<Array>(); let x = std::mem::take(args[1]).cast::<Array>();
if x.is_empty() { if x.is_empty() {
return Ok(Dynamic::UNIT); return Ok(Dynamic::UNIT);
} }
let _array_was_empty = { let _array_is_empty = args[0].read_lock::<Array>().expect(BUILTIN).is_empty();
let array = &mut &mut *args[0].write_lock::<Array>().expect(BUILTIN);
let array_is_empty = array.is_empty();
append(array, x);
array_is_empty
};
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
if !_array_was_empty { if !_array_is_empty {
_ctx.engine().check_data_size( _ctx.engine().check_data_size(
&*args[0].read_lock().expect(BUILTIN), &*args[0].read_lock().expect(BUILTIN),
crate::Position::NONE, crate::Position::NONE,
)?; )?;
} }
Ok(Dynamic::UNIT) let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
Ok(append(array, x).into())
}), }),
_ => None, _ => None,
}; };
@ -744,10 +744,11 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
#[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 {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
let blob2 = std::mem::take(args[1]).cast::<Blob>(); let blob2 = std::mem::take(args[1]).cast::<Blob>();
let blob1 = &mut *args[0].write_lock::<Blob>().expect(BUILTIN); let blob1 = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
@ -758,7 +759,7 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
0, 0,
))?; ))?;
Ok(crate::packages::blob_basic::blob_functions::append(blob1, blob2).into()) Ok(append(blob1, blob2).into())
}), }),
_ => None, _ => None,
}; };
@ -770,13 +771,13 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
($x:ident, $xx:ident, $y:ty, $yy:ident) => { ($x:ident, $xx:ident, $y:ty, $yy:ident) => {
if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) { if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
return match op { return match op {
Token::PlusAssign => Some(impl_op!($x += $yy)), PlusAssign => Some(impl_op!($x += $yy)),
Token::MinusAssign => Some(impl_op!($x -= $yy)), MinusAssign => Some(impl_op!($x -= $yy)),
Token::MultiplyAssign => Some(impl_op!($x *= $yy)), MultiplyAssign => Some(impl_op!($x *= $yy)),
Token::DivideAssign => Some(impl_op!($x /= $yy)), DivideAssign => Some(impl_op!($x /= $yy)),
Token::ModuloAssign => Some(impl_op!($x %= $yy)), ModuloAssign => Some(impl_op!($x %= $yy)),
Token::PowerOfAssign => Some(impl_op!($x => $xx.powf($yy as $x))), PowerOfAssign => Some(impl_op!($x => $xx.powf($yy as $x))),
_ => None, _ => None,
}; };
} }
} }
@ -797,13 +798,13 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
return match op { return match op {
Token::PlusAssign => Some(impl_op!(from $x => add($xx, $yy))), PlusAssign => Some(impl_op!(from $x => add($xx, $yy))),
Token::MinusAssign => Some(impl_op!(from $x => subtract($xx, $yy))), MinusAssign => Some(impl_op!(from $x => subtract($xx, $yy))),
Token::MultiplyAssign => Some(impl_op!(from $x => multiply($xx, $yy))), MultiplyAssign => Some(impl_op!(from $x => multiply($xx, $yy))),
Token::DivideAssign => Some(impl_op!(from $x => divide($xx, $yy))), DivideAssign => Some(impl_op!(from $x => divide($xx, $yy))),
Token::ModuloAssign => Some(impl_op!(from $x => modulo($xx, $yy))), ModuloAssign => Some(impl_op!(from $x => modulo($xx, $yy))),
Token::PowerOfAssign => Some(impl_op!(from $x => power($xx, $yy))), PowerOfAssign => Some(impl_op!(from $x => power($xx, $yy))),
_ => None, _ => None,
}; };
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
@ -811,13 +812,13 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
return match op { return match op {
Token::PlusAssign => Some(impl_op!(from $x += $yy)), PlusAssign => Some(impl_op!(from $x += $yy)),
Token::MinusAssign => Some(impl_op!(from $x -= $yy)), MinusAssign => Some(impl_op!(from $x -= $yy)),
Token::MultiplyAssign => Some(impl_op!(from $x *= $yy)), MultiplyAssign => Some(impl_op!(from $x *= $yy)),
Token::DivideAssign => Some(impl_op!(from $x /= $yy)), DivideAssign => Some(impl_op!(from $x /= $yy)),
Token::ModuloAssign => Some(impl_op!(from $x %= $yy)), ModuloAssign => Some(impl_op!(from $x %= $yy)),
Token::PowerOfAssign => Some(impl_op!(from $x => $xx.powd($yy))), PowerOfAssign => Some(impl_op!(from $x => $xx.powd($yy))),
_ => None, _ => None,
}; };
} }
}; };
@ -832,7 +833,7 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
// string op= char // string op= char
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) { if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) {
return match op { return match op {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
let mut buf = [0_u8; 4]; let mut buf = [0_u8; 4];
let ch = &*args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf); let ch = &*args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
let mut x = args[0].write_lock::<ImmutableString>().expect(BUILTIN); let mut x = args[0].write_lock::<ImmutableString>().expect(BUILTIN);
@ -843,14 +844,14 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
Ok((*x += ch).into()) Ok((*x += ch).into())
}), }),
Token::MinusAssign => Some(impl_op!(ImmutableString -= as_char as char)), MinusAssign => Some(impl_op!(ImmutableString -= as_char as char)),
_ => None, _ => None,
}; };
} }
// char op= string // char op= string
if (type1, type2) == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) { if (type1, type2) == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) {
return match op { return match op {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
let ch = { let ch = {
let s = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN); let s = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
@ -883,7 +884,7 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
use crate::Array; use crate::Array;
return match op { return match op {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
{ {
let x = std::mem::take(args[1]); let x = std::mem::take(args[1]);
let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN); let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
@ -908,8 +909,10 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
// blob op= int // blob op= int
if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<INT>()) { if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<INT>()) {
use crate::packages::blob_basic::blob_functions::*;
return match op { return match op {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
let x = args[1].as_int().expect(BUILTIN); let x = args[1].as_int().expect(BUILTIN);
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN); let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
@ -917,7 +920,7 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
_ctx.engine() _ctx.engine()
.raise_err_if_over_data_size_limit((blob.len() + 1, 0, 0))?; .raise_err_if_over_data_size_limit((blob.len() + 1, 0, 0))?;
Ok(crate::packages::blob_basic::blob_functions::push(blob, x).into()) Ok(push(blob, x).into())
}), }),
_ => None, _ => None,
}; };
@ -925,8 +928,10 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
// blob op= char // blob op= char
if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<char>()) { if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<char>()) {
use crate::packages::blob_basic::blob_functions::*;
return match op { return match op {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
let x = args[1].as_char().expect(BUILTIN); let x = args[1].as_char().expect(BUILTIN);
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN); let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
@ -934,7 +939,7 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
_ctx.engine() _ctx.engine()
.raise_err_if_over_data_size_limit((blob.len() + 1, 0, 0))?; .raise_err_if_over_data_size_limit((blob.len() + 1, 0, 0))?;
Ok(crate::packages::blob_basic::blob_functions::append_char(blob, x).into()) Ok(append_char(blob, x).into())
}), }),
_ => None, _ => None,
}; };
@ -942,8 +947,10 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
// blob op= string // blob op= string
if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<ImmutableString>()) { if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<ImmutableString>()) {
use crate::packages::blob_basic::blob_functions::*;
return match op { return match op {
Token::PlusAssign => Some(|_ctx, args| { PlusAssign => Some(|_ctx, args| {
let s = std::mem::take(args[1]).cast::<ImmutableString>(); let s = std::mem::take(args[1]).cast::<ImmutableString>();
if s.is_empty() { if s.is_empty() {
@ -959,7 +966,7 @@ pub fn get_builtin_op_assignment_fn(op: &Token, x: &Dynamic, y: &Dynamic) -> Opt
0, 0,
))?; ))?;
Ok(crate::packages::blob_basic::blob_functions::append_str(blob, &s).into()) Ok(append_str(blob, &s).into())
}), }),
_ => None, _ => None,
}; };