Refine packages.
This commit is contained in:
parent
38a6c15da1
commit
729ab99ec3
@ -25,11 +25,13 @@ macro_rules! gen_array_functions {
|
||||
use super::super::*;
|
||||
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
pub fn push_func(list: &mut Array, item: $arg_type) {
|
||||
super::super::push(list, item);
|
||||
}
|
||||
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
pub fn insert_func(list: &mut Array, len: INT, item: $arg_type) {
|
||||
super::super::insert(list, len, item);
|
||||
}
|
||||
@ -59,9 +61,6 @@ macro_rules! reg_pad {
|
||||
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
lib.combine(exported_module!(array_functions));
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
lib.combine(exported_module!(object_functions));
|
||||
|
||||
reg_functions!(lib += basic; INT, bool, char, ImmutableString, FnPtr, Array, Unit);
|
||||
reg_pad!(lib, INT, bool, char, ImmutableString, FnPtr, Array, Unit);
|
||||
|
||||
@ -93,21 +92,31 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
|
||||
#[export_module]
|
||||
mod array_functions {
|
||||
#[inline(always)]
|
||||
pub fn len(list: &mut Array) -> INT {
|
||||
list.len() as INT
|
||||
}
|
||||
#[rhai_fn(get = "len")]
|
||||
#[inline(always)]
|
||||
pub fn len_prop(list: &mut Array) -> INT {
|
||||
len(list)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn append(x: &mut Array, y: Array) {
|
||||
x.extend(y);
|
||||
}
|
||||
#[rhai_fn(name = "+=")]
|
||||
#[inline(always)]
|
||||
pub fn append_operator(x: &mut Array, y: Array) {
|
||||
append(x, y)
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
#[inline]
|
||||
pub fn concat(mut x: Array, y: Array) -> Array {
|
||||
x.extend(y);
|
||||
x
|
||||
}
|
||||
#[inline]
|
||||
pub fn pop(list: &mut Array) -> Dynamic {
|
||||
list.pop().unwrap_or_else(|| ().into())
|
||||
}
|
||||
@ -125,6 +134,7 @@ mod array_functions {
|
||||
list.remove(len as usize)
|
||||
}
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn clear(list: &mut Array) {
|
||||
list.clear();
|
||||
}
|
||||
@ -137,16 +147,8 @@ mod array_functions {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
#[export_module]
|
||||
mod object_functions {
|
||||
#[rhai_fn(get = "len")]
|
||||
pub fn len(list: &mut Array) -> INT {
|
||||
array_functions::len(list)
|
||||
}
|
||||
}
|
||||
|
||||
// Register array utility functions
|
||||
#[inline(always)]
|
||||
fn push<T: Variant + Clone>(list: &mut Array, item: T) {
|
||||
list.push(Dynamic::from(item));
|
||||
}
|
||||
|
@ -2,17 +2,20 @@ use crate::def_package;
|
||||
use crate::fn_native::FnPtr;
|
||||
use crate::plugin::*;
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
use crate::engine::make_getter;
|
||||
|
||||
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
|
||||
set_exported_fn!(lib, "name", get_fn_name);
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
set_exported_fn!(lib, make_getter("name"), get_fn_name);
|
||||
lib.combine(exported_module!(fn_ptr_functions));
|
||||
});
|
||||
|
||||
#[export_fn]
|
||||
fn get_fn_name(f: &mut FnPtr) -> ImmutableString {
|
||||
f.get_fn_name().clone()
|
||||
#[export_module]
|
||||
mod fn_ptr_functions {
|
||||
#[inline(always)]
|
||||
fn name(f: &mut FnPtr) -> ImmutableString {
|
||||
f.get_fn_name().clone()
|
||||
}
|
||||
|
||||
#[rhai_fn(get = "name")]
|
||||
#[inline(always)]
|
||||
fn name_prop(f: &mut FnPtr) -> ImmutableString {
|
||||
name(f)
|
||||
}
|
||||
}
|
||||
|
@ -2,25 +2,52 @@ use crate::def_package;
|
||||
use crate::plugin::*;
|
||||
|
||||
macro_rules! gen_cmp_functions {
|
||||
($op_name:literal = $op_fn:ident ( $($arg_type:ident),+ ) -> $return_type:ident) => {
|
||||
pub mod $op_fn { $(
|
||||
($root:ident => $($arg_type:ident),+) => {
|
||||
mod $root { $(
|
||||
pub mod $arg_type {
|
||||
use crate::plugin::*;
|
||||
|
||||
pub const OP_NAME: &'static str = $op_name;
|
||||
|
||||
#[export_fn]
|
||||
pub fn cmp_func(x: $arg_type, y: $arg_type) -> $return_type {
|
||||
super::super::super::$op_fn(x, y)
|
||||
#[export_module]
|
||||
pub mod functions {
|
||||
#[rhai_fn(name = "<")]
|
||||
#[inline(always)]
|
||||
pub fn lt(x: $arg_type, y: $arg_type) -> bool {
|
||||
x < y
|
||||
}
|
||||
#[rhai_fn(name = "<=")]
|
||||
#[inline(always)]
|
||||
pub fn lte(x: $arg_type, y: $arg_type) -> bool {
|
||||
x <= y
|
||||
}
|
||||
#[rhai_fn(name = ">")]
|
||||
#[inline(always)]
|
||||
pub fn gt(x: $arg_type, y: $arg_type) -> bool {
|
||||
x > y
|
||||
}
|
||||
#[rhai_fn(name = ">=")]
|
||||
#[inline(always)]
|
||||
pub fn gte(x: $arg_type, y: $arg_type) -> bool {
|
||||
x >= y
|
||||
}
|
||||
#[rhai_fn(name = "==")]
|
||||
#[inline(always)]
|
||||
pub fn eq(x: $arg_type, y: $arg_type) -> bool {
|
||||
x == y
|
||||
}
|
||||
#[rhai_fn(name = "!=")]
|
||||
#[inline(always)]
|
||||
pub fn ne(x: $arg_type, y: $arg_type) -> bool {
|
||||
x != y
|
||||
}
|
||||
}
|
||||
}
|
||||
)* }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! reg_functions {
|
||||
($mod_name:ident += $root:ident :: $op_name:ident ( $($arg_type:ident),+ )) => {
|
||||
$(set_exported_fn!($mod_name, $root::$op_name::$arg_type::OP_NAME, $root::$op_name::$arg_type::cmp_func);)*
|
||||
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => {
|
||||
$($mod_name.combine(exported_module!($root::$arg_type::functions));)*
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,63 +55,18 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, {
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
{
|
||||
reg_functions!(lib += cmp::lt(i8, u8, i16, u16, i32, u32, u64));
|
||||
reg_functions!(lib += cmp::lte(i8, u8, i16, u16, i32, u32, u64));
|
||||
reg_functions!(lib += cmp::gt(i8, u8, i16, u16, i32, u32, u64));
|
||||
reg_functions!(lib += cmp::gte(i8, u8, i16, u16, i32, u32, u64));
|
||||
reg_functions!(lib += cmp::eq(i8, u8, i16, u16, i32, u32, u64));
|
||||
reg_functions!(lib += cmp::ne(i8, u8, i16, u16, i32, u32, u64));
|
||||
reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64);
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
reg_functions!(lib += cmp_128::lt(i128, u128));
|
||||
reg_functions!(lib += cmp_128::lte(i128, u128));
|
||||
reg_functions!(lib += cmp_128::gt(i128, u128));
|
||||
reg_functions!(lib += cmp_128::gte(i128, u128));
|
||||
reg_functions!(lib += cmp_128::eq(i128, u128));
|
||||
reg_functions!(lib += cmp_128::ne(i128, u128));
|
||||
}
|
||||
reg_functions!(lib += num_128; i128, u128);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
{
|
||||
reg_functions!(lib += cmp_float::lt(f32));
|
||||
reg_functions!(lib += cmp_float::lte(f32));
|
||||
reg_functions!(lib += cmp_float::gt(f32));
|
||||
reg_functions!(lib += cmp_float::gte(f32));
|
||||
reg_functions!(lib += cmp_float::eq(f32));
|
||||
reg_functions!(lib += cmp_float::ne(f32));
|
||||
}
|
||||
reg_functions!(lib += float; f32);
|
||||
|
||||
set_exported_fn!(lib, "!", not);
|
||||
});
|
||||
|
||||
// Comparison operators
|
||||
#[inline(always)]
|
||||
pub fn lt<T: PartialOrd>(x: T, y: T) -> bool {
|
||||
x < y
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn lte<T: PartialOrd>(x: T, y: T) -> bool {
|
||||
x <= y
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn gt<T: PartialOrd>(x: T, y: T) -> bool {
|
||||
x > y
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn gte<T: PartialOrd>(x: T, y: T) -> bool {
|
||||
x >= y
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn eq<T: PartialEq>(x: T, y: T) -> bool {
|
||||
x == y
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn ne<T: PartialEq>(x: T, y: T) -> bool {
|
||||
x != y
|
||||
}
|
||||
|
||||
// Logic operators
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
@ -94,33 +76,12 @@ fn not(x: bool) -> bool {
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
mod cmp {
|
||||
gen_cmp_functions!("<" = lt(i8, u8, i16, u16, i32, u32, u64) -> bool);
|
||||
gen_cmp_functions!("<=" = lte(i8, u8, i16, u16, i32, u32, u64) -> bool);
|
||||
gen_cmp_functions!(">" = gt(i8, u8, i16, u16, i32, u32, u64) -> bool);
|
||||
gen_cmp_functions!(">=" = gte(i8, u8, i16, u16, i32, u32, u64) -> bool);
|
||||
gen_cmp_functions!("==" = eq(i8, u8, i16, u16, i32, u32, u64) -> bool);
|
||||
gen_cmp_functions!("!=" = ne(i8, u8, i16, u16, i32, u32, u64) -> bool);
|
||||
}
|
||||
gen_cmp_functions!(numbers => i8, u8, i16, u16, i32, u32, u64);
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
mod cmp_128 {
|
||||
gen_cmp_functions!("<" = lt(i128, u128) -> bool);
|
||||
gen_cmp_functions!("<=" = lte(i128, u128) -> bool);
|
||||
gen_cmp_functions!(">" = gt(i128, u128) -> bool);
|
||||
gen_cmp_functions!(">=" = gte(i128, u128) -> bool);
|
||||
gen_cmp_functions!("==" = eq(i128, u128) -> bool);
|
||||
gen_cmp_functions!("!=" = ne(i128, u128) -> bool);
|
||||
}
|
||||
gen_cmp_functions!(num_128 => i128, u128);
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
mod cmp_float {
|
||||
gen_cmp_functions!("<" = lt(f32) -> bool);
|
||||
gen_cmp_functions!("<=" = lte(f32) -> bool);
|
||||
gen_cmp_functions!(">" = gt(f32) -> bool);
|
||||
gen_cmp_functions!(">=" = gte(f32) -> bool);
|
||||
gen_cmp_functions!("==" = eq(f32) -> bool);
|
||||
gen_cmp_functions!("!=" = ne(f32) -> bool);
|
||||
}
|
||||
gen_cmp_functions!(float => f32);
|
||||
|
@ -18,19 +18,24 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
||||
|
||||
#[export_module]
|
||||
mod map_functions {
|
||||
#[inline(always)]
|
||||
pub fn has(map: &mut Map, prop: ImmutableString) -> bool {
|
||||
map.contains_key(&prop)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn len(map: &mut Map) -> INT {
|
||||
map.len() as INT
|
||||
}
|
||||
#[rhai_fn(get = "len")]
|
||||
#[inline(always)]
|
||||
pub fn len_prop(map: &mut Map) -> INT {
|
||||
len(map)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn clear(map: &mut Map) {
|
||||
map.clear();
|
||||
}
|
||||
#[inline]
|
||||
pub fn remove(x: &mut Map, name: ImmutableString) -> Dynamic {
|
||||
x.remove(&name).unwrap_or_else(|| ().into())
|
||||
}
|
||||
@ -40,6 +45,7 @@ mod map_functions {
|
||||
});
|
||||
}
|
||||
#[rhai_fn(name = "+=")]
|
||||
#[inline(always)]
|
||||
pub fn mixin_operator(map1: &mut Map, map2: Map) {
|
||||
mixin(map1, map2)
|
||||
}
|
||||
|
@ -8,9 +8,6 @@ use crate::parser::FLOAT;
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
use crate::{result::EvalAltResult, token::Position};
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
use crate::engine::make_getter;
|
||||
|
||||
#[cfg(feature = "no_std")]
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
use num_traits::float::Float;
|
||||
@ -31,19 +28,6 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
||||
// Floating point functions
|
||||
lib.combine(exported_module!(float_functions));
|
||||
|
||||
// Floating point properties
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
{
|
||||
set_exported_fn!(lib, make_getter("floor"), float_funcs::floor);
|
||||
set_exported_fn!(lib, make_getter("ceiling"), float_funcs::ceiling);
|
||||
set_exported_fn!(lib, make_getter("round"), float_funcs::round);
|
||||
set_exported_fn!(lib, make_getter("int"), float_funcs::int);
|
||||
set_exported_fn!(lib, make_getter("fraction"), float_funcs::fraction);
|
||||
set_exported_fn!(lib, make_getter("is_nan"), float_funcs::is_nan);
|
||||
set_exported_fn!(lib, make_getter("is_finite"), float_funcs::is_finite);
|
||||
set_exported_fn!(lib, make_getter("is_infinite"), float_funcs::is_infinite);
|
||||
}
|
||||
|
||||
// Trig functions
|
||||
lib.combine(exported_module!(trig_functions));
|
||||
|
||||
@ -125,39 +109,51 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
||||
mod trig_functions {
|
||||
use crate::parser::FLOAT;
|
||||
|
||||
#[inline(always)]
|
||||
pub fn sin(x: FLOAT) -> FLOAT {
|
||||
x.to_radians().sin()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn cos(x: FLOAT) -> FLOAT {
|
||||
x.to_radians().cos()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn tan(x: FLOAT) -> FLOAT {
|
||||
x.to_radians().tan()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn sinh(x: FLOAT) -> FLOAT {
|
||||
x.to_radians().sinh()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn cosh(x: FLOAT) -> FLOAT {
|
||||
x.to_radians().cosh()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn tanh(x: FLOAT) -> FLOAT {
|
||||
x.to_radians().tanh()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn asin(x: FLOAT) -> FLOAT {
|
||||
x.asin().to_degrees()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn acos(x: FLOAT) -> FLOAT {
|
||||
x.acos().to_degrees()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn atan(x: FLOAT) -> FLOAT {
|
||||
x.atan().to_degrees()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn asinh(x: FLOAT) -> FLOAT {
|
||||
x.asinh().to_degrees()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn acosh(x: FLOAT) -> FLOAT {
|
||||
x.acosh().to_degrees()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn atanh(x: FLOAT) -> FLOAT {
|
||||
x.atanh().to_degrees()
|
||||
}
|
||||
@ -168,84 +164,96 @@ mod trig_functions {
|
||||
mod float_functions {
|
||||
use crate::parser::FLOAT;
|
||||
|
||||
#[inline(always)]
|
||||
pub fn sqrt(x: FLOAT) -> FLOAT {
|
||||
x.sqrt()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn exp(x: FLOAT) -> FLOAT {
|
||||
x.exp()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn ln(x: FLOAT) -> FLOAT {
|
||||
x.ln()
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn log(x: FLOAT, base: FLOAT) -> FLOAT {
|
||||
x.log(base)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn log10(x: FLOAT) -> FLOAT {
|
||||
x.log10()
|
||||
}
|
||||
pub fn floor(x: FLOAT) -> FLOAT {
|
||||
float_funcs::floor(x)
|
||||
}
|
||||
pub fn ceiling(x: FLOAT) -> FLOAT {
|
||||
float_funcs::ceiling(x)
|
||||
}
|
||||
pub fn round(x: FLOAT) -> FLOAT {
|
||||
float_funcs::round(x)
|
||||
}
|
||||
pub fn int(x: FLOAT) -> FLOAT {
|
||||
float_funcs::int(x)
|
||||
}
|
||||
pub fn fraction(x: FLOAT) -> FLOAT {
|
||||
float_funcs::fraction(x)
|
||||
}
|
||||
pub fn is_nan(x: FLOAT) -> bool {
|
||||
float_funcs::is_nan(x)
|
||||
}
|
||||
pub fn is_finite(x: FLOAT) -> bool {
|
||||
float_funcs::is_finite(x)
|
||||
}
|
||||
pub fn is_infinite(x: FLOAT) -> bool {
|
||||
float_funcs::is_infinite(x)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
mod float_funcs {
|
||||
use crate::parser::FLOAT;
|
||||
use crate::plugin::*;
|
||||
#[cfg(feature = "no_std")]
|
||||
use num_traits::float::Float;
|
||||
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
pub fn floor(x: FLOAT) -> FLOAT {
|
||||
x.floor()
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(get = "floor")]
|
||||
#[inline(always)]
|
||||
pub fn floor_prop(x: FLOAT) -> FLOAT {
|
||||
floor(x)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn ceiling(x: FLOAT) -> FLOAT {
|
||||
x.ceil()
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(get = "ceiling")]
|
||||
#[inline(always)]
|
||||
pub fn ceiling_prop(x: FLOAT) -> FLOAT {
|
||||
ceiling(x)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn round(x: FLOAT) -> FLOAT {
|
||||
x.ceil()
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(get = "round")]
|
||||
#[inline(always)]
|
||||
pub fn round_prop(x: FLOAT) -> FLOAT {
|
||||
ceiling(x)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn int(x: FLOAT) -> FLOAT {
|
||||
x.trunc()
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(get = "int")]
|
||||
#[inline(always)]
|
||||
pub fn int_prop(x: FLOAT) -> FLOAT {
|
||||
int(x)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn fraction(x: FLOAT) -> FLOAT {
|
||||
x.fract()
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(get = "fraction")]
|
||||
#[inline(always)]
|
||||
pub fn fraction_prop(x: FLOAT) -> FLOAT {
|
||||
fraction(x)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn is_nan(x: FLOAT) -> bool {
|
||||
x.is_nan()
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(get = "is_nan")]
|
||||
#[inline(always)]
|
||||
pub fn is_nan_prop(x: FLOAT) -> bool {
|
||||
is_nan(x)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn is_finite(x: FLOAT) -> bool {
|
||||
x.is_finite()
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(get = "is_finite")]
|
||||
#[inline(always)]
|
||||
pub fn is_finite_prop(x: FLOAT) -> bool {
|
||||
is_finite(x)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn is_infinite(x: FLOAT) -> bool {
|
||||
x.is_infinite()
|
||||
}
|
||||
#[rhai_fn(get = "is_infinite")]
|
||||
#[inline(always)]
|
||||
pub fn is_infinite_prop(x: FLOAT) -> bool {
|
||||
is_infinite(x)
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ macro_rules! gen_functions {
|
||||
use super::super::*;
|
||||
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
pub fn to_string_func(x: &mut $arg_type) -> ImmutableString {
|
||||
super::super::$fn_name(x)
|
||||
}
|
||||
@ -125,29 +126,36 @@ gen_functions!(print_array => to_debug(Array));
|
||||
|
||||
// Register print and debug
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
fn print_empty_string() -> ImmutableString {
|
||||
"".to_string().into()
|
||||
}
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
fn print_unit(_x: ()) -> ImmutableString {
|
||||
"".to_string().into()
|
||||
}
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
fn print_string(s: ImmutableString) -> ImmutableString {
|
||||
s
|
||||
}
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
|
||||
f.to_string().into()
|
||||
}
|
||||
#[inline(always)]
|
||||
fn to_string<T: Display>(x: &mut T) -> ImmutableString {
|
||||
x.to_string().into()
|
||||
}
|
||||
#[inline]
|
||||
fn to_debug<T: Debug>(x: &mut T) -> ImmutableString {
|
||||
format!("{:?}", x).into()
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
#[export_fn]
|
||||
#[inline]
|
||||
fn format_map(x: &mut Map) -> ImmutableString {
|
||||
format!("#{:?}", x).into()
|
||||
}
|
||||
|
@ -22,11 +22,13 @@ macro_rules! gen_concat_functions {
|
||||
use super::super::*;
|
||||
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
pub fn append_func(x: &mut ImmutableString, y: $arg_type) -> String {
|
||||
super::super::add_append(x, y)
|
||||
}
|
||||
|
||||
#[export_fn]
|
||||
#[inline(always)]
|
||||
pub fn prepend_func(x: &mut $arg_type, y: ImmutableString) -> String {
|
||||
super::super::add_prepend(x, y)
|
||||
}
|
||||
@ -60,9 +62,6 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
lib.combine(exported_module!(index_functions));
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
lib.combine(exported_module!(object_functions));
|
||||
|
||||
lib.combine(exported_module!(string_functions));
|
||||
|
||||
lib.set_raw_fn(
|
||||
@ -120,9 +119,11 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
);
|
||||
});
|
||||
|
||||
#[inline]
|
||||
fn add_prepend<T: Display>(x: &mut T, y: ImmutableString) -> String {
|
||||
format!("{}{}", x, y)
|
||||
}
|
||||
#[inline]
|
||||
fn add_append<T: Display>(x: &mut ImmutableString, y: T) -> String {
|
||||
format!("{}{}", x, y)
|
||||
}
|
||||
@ -144,26 +145,38 @@ gen_concat_functions!(float => f32, f64);
|
||||
#[export_module]
|
||||
mod string_functions {
|
||||
#[rhai_fn(name = "+")]
|
||||
#[inline(always)]
|
||||
pub fn add_append_unit(s: ImmutableString, _x: ()) -> ImmutableString {
|
||||
s
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
#[inline(always)]
|
||||
pub fn add_prepend_unit(_x: (), s: ImmutableString) -> ImmutableString {
|
||||
s
|
||||
}
|
||||
#[rhai_fn(name = "+=")]
|
||||
#[inline(always)]
|
||||
pub fn append_char(s: &mut ImmutableString, ch: char) {
|
||||
*s += ch;
|
||||
}
|
||||
#[rhai_fn(name = "+=")]
|
||||
#[inline(always)]
|
||||
pub fn append_string(s: &mut ImmutableString, add: ImmutableString) {
|
||||
*s += &add;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn len(s: &mut ImmutableString) -> INT {
|
||||
s.chars().count() as INT
|
||||
}
|
||||
|
||||
#[rhai_fn(get = "len")]
|
||||
#[inline(always)]
|
||||
pub fn len_prop(s: &mut ImmutableString) -> INT {
|
||||
len(s)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn clear(s: &mut ImmutableString) {
|
||||
s.make_mut().clear();
|
||||
}
|
||||
@ -186,10 +199,12 @@ mod string_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "contains")]
|
||||
#[inline(always)]
|
||||
pub fn contains_char(s: &mut ImmutableString, ch: char) -> bool {
|
||||
s.contains(ch)
|
||||
}
|
||||
#[rhai_fn(name = "contains")]
|
||||
#[inline(always)]
|
||||
pub fn contains_string(s: &mut ImmutableString, find: ImmutableString) -> bool {
|
||||
s.contains(find.as_str())
|
||||
}
|
||||
@ -270,6 +285,7 @@ mod string_functions {
|
||||
.into()
|
||||
}
|
||||
#[rhai_fn(name = "sub_string")]
|
||||
#[inline(always)]
|
||||
pub fn sub_string_starting_from(s: ImmutableString, start: INT) -> ImmutableString {
|
||||
let len = s.len() as INT;
|
||||
sub_string(s, start, len)
|
||||
@ -302,23 +318,28 @@ mod string_functions {
|
||||
copy.extend(chars.iter().skip(offset).take(len));
|
||||
}
|
||||
#[rhai_fn(name = "crop")]
|
||||
#[inline(always)]
|
||||
pub fn crop_string_starting_from(s: &mut ImmutableString, start: INT) {
|
||||
crop_string(s, start, s.len() as INT);
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "replace")]
|
||||
#[inline(always)]
|
||||
pub fn replace_string(s: &mut ImmutableString, find: ImmutableString, sub: ImmutableString) {
|
||||
*s = s.replace(find.as_str(), sub.as_str()).into();
|
||||
}
|
||||
#[rhai_fn(name = "replace")]
|
||||
#[inline(always)]
|
||||
pub fn replace_string_with_char(s: &mut ImmutableString, find: ImmutableString, sub: char) {
|
||||
*s = s.replace(find.as_str(), &sub.to_string()).into();
|
||||
}
|
||||
#[rhai_fn(name = "replace")]
|
||||
#[inline(always)]
|
||||
pub fn replace_char_with_string(s: &mut ImmutableString, find: char, sub: ImmutableString) {
|
||||
*s = s.replace(&find.to_string(), sub.as_str()).into();
|
||||
}
|
||||
#[rhai_fn(name = "replace")]
|
||||
#[inline(always)]
|
||||
pub fn replace_char(s: &mut ImmutableString, find: char, sub: char) {
|
||||
*s = s.replace(&find.to_string(), &sub.to_string()).into();
|
||||
}
|
||||
@ -330,20 +351,13 @@ mod index_functions {
|
||||
use crate::engine::Array;
|
||||
|
||||
#[rhai_fn(name = "+")]
|
||||
#[inline]
|
||||
pub fn append(x: &mut ImmutableString, y: Array) -> String {
|
||||
format!("{}{:?}", x, y)
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
#[inline]
|
||||
pub fn prepend(x: &mut Array, y: ImmutableString) -> String {
|
||||
format!("{:?}{}", x, y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
#[export_module]
|
||||
mod object_functions {
|
||||
#[rhai_fn(get = "len")]
|
||||
pub fn len(s: &mut ImmutableString) -> INT {
|
||||
string_functions::len(s)
|
||||
}
|
||||
}
|
||||
|
@ -27,13 +27,11 @@ use instant::Instant;
|
||||
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
|
||||
// Register date/time functions
|
||||
lib.combine(exported_module!(time_functions));
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
lib.set_getter_fn("elapsed", time_functions::elapsed);
|
||||
});
|
||||
|
||||
#[export_module]
|
||||
mod time_functions {
|
||||
#[inline(always)]
|
||||
pub fn timestamp() -> Instant {
|
||||
Instant::now()
|
||||
}
|
||||
@ -61,6 +59,12 @@ mod time_functions {
|
||||
}
|
||||
}
|
||||
|
||||
#[rhai_fn(get = "elapsed", return_raw)]
|
||||
#[inline(always)]
|
||||
pub fn elapsed_prop(timestamp: &mut Instant) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
elapsed(timestamp)
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, name = "-")]
|
||||
fn time_diff(ts1: Instant, ts2: Instant) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
@ -104,26 +108,32 @@ mod time_functions {
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "==")]
|
||||
#[inline(always)]
|
||||
pub fn eq(x: Instant, y: Instant) -> bool {
|
||||
x == y
|
||||
}
|
||||
#[rhai_fn(name = "!=")]
|
||||
#[inline(always)]
|
||||
pub fn ne(x: Instant, y: Instant) -> bool {
|
||||
x != y
|
||||
}
|
||||
#[rhai_fn(name = "<")]
|
||||
#[inline(always)]
|
||||
pub fn lt(x: Instant, y: Instant) -> bool {
|
||||
x < y
|
||||
}
|
||||
#[rhai_fn(name = "<=")]
|
||||
#[inline(always)]
|
||||
pub fn lte(x: Instant, y: Instant) -> bool {
|
||||
x <= y
|
||||
}
|
||||
#[rhai_fn(name = ">")]
|
||||
#[inline(always)]
|
||||
pub fn gt(x: Instant, y: Instant) -> bool {
|
||||
x > y
|
||||
}
|
||||
#[rhai_fn(name = ">=")]
|
||||
#[inline(always)]
|
||||
pub fn gte(x: Instant, y: Instant) -> bool {
|
||||
x >= y
|
||||
}
|
||||
|
@ -3,21 +3,28 @@
|
||||
use rhai::plugin::*;
|
||||
use rhai::{Engine, EvalAltResult, INT};
|
||||
|
||||
#[export_module]
|
||||
mod special_array_package {
|
||||
use rhai::{Array, INT};
|
||||
mod test {
|
||||
use rhai::plugin::*;
|
||||
|
||||
#[rhai_fn(get = "foo", return_raw)]
|
||||
pub fn foo(array: &mut Array) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
Ok(array[0].clone())
|
||||
}
|
||||
#[rhai_fn(name = "test")]
|
||||
pub fn len(array: &mut Array, mul: INT) -> INT {
|
||||
(array.len() as INT) * mul
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn funky_add(x: INT, y: INT) -> INT {
|
||||
x / 2 + y * 2
|
||||
#[export_module]
|
||||
pub mod special_array_package {
|
||||
use rhai::{Array, INT};
|
||||
|
||||
#[rhai_fn(get = "foo", return_raw)]
|
||||
#[inline(always)]
|
||||
pub fn foo(array: &mut Array) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
Ok(array[0].clone())
|
||||
}
|
||||
#[rhai_fn(name = "test")]
|
||||
#[inline(always)]
|
||||
pub fn len(array: &mut Array, mul: INT) -> INT {
|
||||
(array.len() as INT) * mul
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
#[inline(always)]
|
||||
pub fn funky_add(x: INT, y: INT) -> INT {
|
||||
x / 2 + y * 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,7 +59,7 @@ gen_unary_functions!(greet = make_greeting(INT, bool, char) -> String);
|
||||
fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
let m = exported_module!(special_array_package);
|
||||
let m = exported_module!(test::special_array_package);
|
||||
engine.load_package(m);
|
||||
|
||||
reg_functions!(engine += greet::single(INT, bool, char));
|
||||
|
Loading…
Reference in New Issue
Block a user