Refine packages.

This commit is contained in:
Stephen Chung 2020-08-20 22:11:41 +08:00
parent 38a6c15da1
commit 729ab99ec3
9 changed files with 210 additions and 191 deletions

View File

@ -25,11 +25,13 @@ macro_rules! gen_array_functions {
use super::super::*; use super::super::*;
#[export_fn] #[export_fn]
#[inline(always)]
pub fn push_func(list: &mut Array, item: $arg_type) { pub fn push_func(list: &mut Array, item: $arg_type) {
super::super::push(list, item); super::super::push(list, item);
} }
#[export_fn] #[export_fn]
#[inline(always)]
pub fn insert_func(list: &mut Array, len: INT, item: $arg_type) { pub fn insert_func(list: &mut Array, len: INT, item: $arg_type) {
super::super::insert(list, len, item); super::super::insert(list, len, item);
} }
@ -59,9 +61,6 @@ macro_rules! reg_pad {
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
lib.combine(exported_module!(array_functions)); 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_functions!(lib += basic; INT, bool, char, ImmutableString, FnPtr, Array, Unit);
reg_pad!(lib, 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] #[export_module]
mod array_functions { mod array_functions {
#[inline(always)]
pub fn len(list: &mut Array) -> INT { pub fn len(list: &mut Array) -> INT {
list.len() as 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) { pub fn append(x: &mut Array, y: Array) {
x.extend(y); x.extend(y);
} }
#[rhai_fn(name = "+=")] #[rhai_fn(name = "+=")]
#[inline(always)]
pub fn append_operator(x: &mut Array, y: Array) { pub fn append_operator(x: &mut Array, y: Array) {
append(x, y) append(x, y)
} }
#[rhai_fn(name = "+")] #[rhai_fn(name = "+")]
#[inline]
pub fn concat(mut x: Array, y: Array) -> Array { pub fn concat(mut x: Array, y: Array) -> Array {
x.extend(y); x.extend(y);
x x
} }
#[inline]
pub fn pop(list: &mut Array) -> Dynamic { pub fn pop(list: &mut Array) -> Dynamic {
list.pop().unwrap_or_else(|| ().into()) list.pop().unwrap_or_else(|| ().into())
} }
@ -125,6 +134,7 @@ mod array_functions {
list.remove(len as usize) list.remove(len as usize)
} }
} }
#[inline(always)]
pub fn clear(list: &mut Array) { pub fn clear(list: &mut Array) {
list.clear(); 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 // Register array utility functions
#[inline(always)]
fn push<T: Variant + Clone>(list: &mut Array, item: T) { fn push<T: Variant + Clone>(list: &mut Array, item: T) {
list.push(Dynamic::from(item)); list.push(Dynamic::from(item));
} }

View File

@ -2,17 +2,20 @@ use crate::def_package;
use crate::fn_native::FnPtr; use crate::fn_native::FnPtr;
use crate::plugin::*; use crate::plugin::*;
#[cfg(not(feature = "no_object"))]
use crate::engine::make_getter;
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, { def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
set_exported_fn!(lib, "name", get_fn_name); lib.combine(exported_module!(fn_ptr_functions));
#[cfg(not(feature = "no_object"))]
set_exported_fn!(lib, make_getter("name"), get_fn_name);
}); });
#[export_fn] #[export_module]
fn get_fn_name(f: &mut FnPtr) -> ImmutableString { mod fn_ptr_functions {
#[inline(always)]
fn name(f: &mut FnPtr) -> ImmutableString {
f.get_fn_name().clone() f.get_fn_name().clone()
}
#[rhai_fn(get = "name")]
#[inline(always)]
fn name_prop(f: &mut FnPtr) -> ImmutableString {
name(f)
}
} }

View File

@ -2,25 +2,52 @@ use crate::def_package;
use crate::plugin::*; use crate::plugin::*;
macro_rules! gen_cmp_functions { macro_rules! gen_cmp_functions {
($op_name:literal = $op_fn:ident ( $($arg_type:ident),+ ) -> $return_type:ident) => { ($root:ident => $($arg_type:ident),+) => {
pub mod $op_fn { $( mod $root { $(
pub mod $arg_type { pub mod $arg_type {
use crate::plugin::*; use crate::plugin::*;
pub const OP_NAME: &'static str = $op_name; #[export_module]
pub mod functions {
#[export_fn] #[rhai_fn(name = "<")]
pub fn cmp_func(x: $arg_type, y: $arg_type) -> $return_type { #[inline(always)]
super::super::super::$op_fn(x, y) 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 { macro_rules! reg_functions {
($mod_name:ident += $root:ident :: $op_name:ident ( $($arg_type:ident),+ )) => { ($mod_name:ident += $root: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.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_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
{ {
reg_functions!(lib += cmp::lt(i8, u8, i16, u16, i32, u32, u64)); reg_functions!(lib += numbers; 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));
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
{ reg_functions!(lib += num_128; i128, u128);
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));
}
} }
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
{ reg_functions!(lib += float; f32);
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));
}
set_exported_fn!(lib, "!", not); 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 // Logic operators
#[export_fn] #[export_fn]
#[inline(always)] #[inline(always)]
@ -94,33 +76,12 @@ fn not(x: bool) -> bool {
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
mod cmp { gen_cmp_functions!(numbers => i8, u8, i16, u16, i32, u32, u64);
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);
}
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
mod cmp_128 { gen_cmp_functions!(num_128 => i128, u128);
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);
}
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
mod cmp_float { gen_cmp_functions!(float => f32);
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);
}

View File

@ -18,19 +18,24 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
#[export_module] #[export_module]
mod map_functions { mod map_functions {
#[inline(always)]
pub fn has(map: &mut Map, prop: ImmutableString) -> bool { pub fn has(map: &mut Map, prop: ImmutableString) -> bool {
map.contains_key(&prop) map.contains_key(&prop)
} }
#[inline(always)]
pub fn len(map: &mut Map) -> INT { pub fn len(map: &mut Map) -> INT {
map.len() as INT map.len() as INT
} }
#[rhai_fn(get = "len")] #[rhai_fn(get = "len")]
#[inline(always)]
pub fn len_prop(map: &mut Map) -> INT { pub fn len_prop(map: &mut Map) -> INT {
len(map) len(map)
} }
#[inline(always)]
pub fn clear(map: &mut Map) { pub fn clear(map: &mut Map) {
map.clear(); map.clear();
} }
#[inline]
pub fn remove(x: &mut Map, name: ImmutableString) -> Dynamic { pub fn remove(x: &mut Map, name: ImmutableString) -> Dynamic {
x.remove(&name).unwrap_or_else(|| ().into()) x.remove(&name).unwrap_or_else(|| ().into())
} }
@ -40,6 +45,7 @@ mod map_functions {
}); });
} }
#[rhai_fn(name = "+=")] #[rhai_fn(name = "+=")]
#[inline(always)]
pub fn mixin_operator(map1: &mut Map, map2: Map) { pub fn mixin_operator(map1: &mut Map, map2: Map) {
mixin(map1, map2) mixin(map1, map2)
} }

View File

@ -8,9 +8,6 @@ use crate::parser::FLOAT;
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
use crate::{result::EvalAltResult, token::Position}; use crate::{result::EvalAltResult, token::Position};
#[cfg(not(feature = "no_object"))]
use crate::engine::make_getter;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
use num_traits::float::Float; use num_traits::float::Float;
@ -31,19 +28,6 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
// Floating point functions // Floating point functions
lib.combine(exported_module!(float_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 // Trig functions
lib.combine(exported_module!(trig_functions)); lib.combine(exported_module!(trig_functions));
@ -125,39 +109,51 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
mod trig_functions { mod trig_functions {
use crate::parser::FLOAT; use crate::parser::FLOAT;
#[inline(always)]
pub fn sin(x: FLOAT) -> FLOAT { pub fn sin(x: FLOAT) -> FLOAT {
x.to_radians().sin() x.to_radians().sin()
} }
#[inline(always)]
pub fn cos(x: FLOAT) -> FLOAT { pub fn cos(x: FLOAT) -> FLOAT {
x.to_radians().cos() x.to_radians().cos()
} }
#[inline(always)]
pub fn tan(x: FLOAT) -> FLOAT { pub fn tan(x: FLOAT) -> FLOAT {
x.to_radians().tan() x.to_radians().tan()
} }
#[inline(always)]
pub fn sinh(x: FLOAT) -> FLOAT { pub fn sinh(x: FLOAT) -> FLOAT {
x.to_radians().sinh() x.to_radians().sinh()
} }
#[inline(always)]
pub fn cosh(x: FLOAT) -> FLOAT { pub fn cosh(x: FLOAT) -> FLOAT {
x.to_radians().cosh() x.to_radians().cosh()
} }
#[inline(always)]
pub fn tanh(x: FLOAT) -> FLOAT { pub fn tanh(x: FLOAT) -> FLOAT {
x.to_radians().tanh() x.to_radians().tanh()
} }
#[inline(always)]
pub fn asin(x: FLOAT) -> FLOAT { pub fn asin(x: FLOAT) -> FLOAT {
x.asin().to_degrees() x.asin().to_degrees()
} }
#[inline(always)]
pub fn acos(x: FLOAT) -> FLOAT { pub fn acos(x: FLOAT) -> FLOAT {
x.acos().to_degrees() x.acos().to_degrees()
} }
#[inline(always)]
pub fn atan(x: FLOAT) -> FLOAT { pub fn atan(x: FLOAT) -> FLOAT {
x.atan().to_degrees() x.atan().to_degrees()
} }
#[inline(always)]
pub fn asinh(x: FLOAT) -> FLOAT { pub fn asinh(x: FLOAT) -> FLOAT {
x.asinh().to_degrees() x.asinh().to_degrees()
} }
#[inline(always)]
pub fn acosh(x: FLOAT) -> FLOAT { pub fn acosh(x: FLOAT) -> FLOAT {
x.acosh().to_degrees() x.acosh().to_degrees()
} }
#[inline(always)]
pub fn atanh(x: FLOAT) -> FLOAT { pub fn atanh(x: FLOAT) -> FLOAT {
x.atanh().to_degrees() x.atanh().to_degrees()
} }
@ -168,84 +164,96 @@ mod trig_functions {
mod float_functions { mod float_functions {
use crate::parser::FLOAT; use crate::parser::FLOAT;
#[inline(always)]
pub fn sqrt(x: FLOAT) -> FLOAT { pub fn sqrt(x: FLOAT) -> FLOAT {
x.sqrt() x.sqrt()
} }
#[inline(always)]
pub fn exp(x: FLOAT) -> FLOAT { pub fn exp(x: FLOAT) -> FLOAT {
x.exp() x.exp()
} }
#[inline(always)]
pub fn ln(x: FLOAT) -> FLOAT { pub fn ln(x: FLOAT) -> FLOAT {
x.ln() x.ln()
} }
#[inline(always)]
pub fn log(x: FLOAT, base: FLOAT) -> FLOAT { pub fn log(x: FLOAT, base: FLOAT) -> FLOAT {
x.log(base) x.log(base)
} }
#[inline(always)]
pub fn log10(x: FLOAT) -> FLOAT { pub fn log10(x: FLOAT) -> FLOAT {
x.log10() x.log10()
} }
pub fn floor(x: FLOAT) -> FLOAT { #[inline(always)]
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]
pub fn floor(x: FLOAT) -> FLOAT { pub fn floor(x: FLOAT) -> FLOAT {
x.floor() 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 { pub fn ceiling(x: FLOAT) -> FLOAT {
x.ceil() 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 { pub fn round(x: FLOAT) -> FLOAT {
x.ceil() 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 { pub fn int(x: FLOAT) -> FLOAT {
x.trunc() 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 { pub fn fraction(x: FLOAT) -> FLOAT {
x.fract() 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 { pub fn is_nan(x: FLOAT) -> bool {
x.is_nan() 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 { pub fn is_finite(x: FLOAT) -> bool {
x.is_finite() 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 { pub fn is_infinite(x: FLOAT) -> bool {
x.is_infinite() x.is_infinite()
} }
#[rhai_fn(get = "is_infinite")]
#[inline(always)]
pub fn is_infinite_prop(x: FLOAT) -> bool {
is_infinite(x)
}
} }

View File

@ -27,6 +27,7 @@ macro_rules! gen_functions {
use super::super::*; use super::super::*;
#[export_fn] #[export_fn]
#[inline(always)]
pub fn to_string_func(x: &mut $arg_type) -> ImmutableString { pub fn to_string_func(x: &mut $arg_type) -> ImmutableString {
super::super::$fn_name(x) super::super::$fn_name(x)
} }
@ -125,29 +126,36 @@ gen_functions!(print_array => to_debug(Array));
// Register print and debug // Register print and debug
#[export_fn] #[export_fn]
#[inline(always)]
fn print_empty_string() -> ImmutableString { fn print_empty_string() -> ImmutableString {
"".to_string().into() "".to_string().into()
} }
#[export_fn] #[export_fn]
#[inline(always)]
fn print_unit(_x: ()) -> ImmutableString { fn print_unit(_x: ()) -> ImmutableString {
"".to_string().into() "".to_string().into()
} }
#[export_fn] #[export_fn]
#[inline(always)]
fn print_string(s: ImmutableString) -> ImmutableString { fn print_string(s: ImmutableString) -> ImmutableString {
s s
} }
#[export_fn] #[export_fn]
#[inline(always)]
fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString { fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
f.to_string().into() f.to_string().into()
} }
#[inline(always)]
fn to_string<T: Display>(x: &mut T) -> ImmutableString { fn to_string<T: Display>(x: &mut T) -> ImmutableString {
x.to_string().into() x.to_string().into()
} }
#[inline]
fn to_debug<T: Debug>(x: &mut T) -> ImmutableString { fn to_debug<T: Debug>(x: &mut T) -> ImmutableString {
format!("{:?}", x).into() format!("{:?}", x).into()
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[export_fn] #[export_fn]
#[inline]
fn format_map(x: &mut Map) -> ImmutableString { fn format_map(x: &mut Map) -> ImmutableString {
format!("#{:?}", x).into() format!("#{:?}", x).into()
} }

View File

@ -22,11 +22,13 @@ macro_rules! gen_concat_functions {
use super::super::*; use super::super::*;
#[export_fn] #[export_fn]
#[inline(always)]
pub fn append_func(x: &mut ImmutableString, y: $arg_type) -> String { pub fn append_func(x: &mut ImmutableString, y: $arg_type) -> String {
super::super::add_append(x, y) super::super::add_append(x, y)
} }
#[export_fn] #[export_fn]
#[inline(always)]
pub fn prepend_func(x: &mut $arg_type, y: ImmutableString) -> String { pub fn prepend_func(x: &mut $arg_type, y: ImmutableString) -> String {
super::super::add_prepend(x, y) super::super::add_prepend(x, y)
} }
@ -60,9 +62,6 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
lib.combine(exported_module!(index_functions)); lib.combine(exported_module!(index_functions));
#[cfg(not(feature = "no_object"))]
lib.combine(exported_module!(object_functions));
lib.combine(exported_module!(string_functions)); lib.combine(exported_module!(string_functions));
lib.set_raw_fn( 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 { fn add_prepend<T: Display>(x: &mut T, y: ImmutableString) -> String {
format!("{}{}", x, y) format!("{}{}", x, y)
} }
#[inline]
fn add_append<T: Display>(x: &mut ImmutableString, y: T) -> String { fn add_append<T: Display>(x: &mut ImmutableString, y: T) -> String {
format!("{}{}", x, y) format!("{}{}", x, y)
} }
@ -144,26 +145,38 @@ gen_concat_functions!(float => f32, f64);
#[export_module] #[export_module]
mod string_functions { mod string_functions {
#[rhai_fn(name = "+")] #[rhai_fn(name = "+")]
#[inline(always)]
pub fn add_append_unit(s: ImmutableString, _x: ()) -> ImmutableString { pub fn add_append_unit(s: ImmutableString, _x: ()) -> ImmutableString {
s s
} }
#[rhai_fn(name = "+")] #[rhai_fn(name = "+")]
#[inline(always)]
pub fn add_prepend_unit(_x: (), s: ImmutableString) -> ImmutableString { pub fn add_prepend_unit(_x: (), s: ImmutableString) -> ImmutableString {
s s
} }
#[rhai_fn(name = "+=")] #[rhai_fn(name = "+=")]
#[inline(always)]
pub fn append_char(s: &mut ImmutableString, ch: char) { pub fn append_char(s: &mut ImmutableString, ch: char) {
*s += ch; *s += ch;
} }
#[rhai_fn(name = "+=")] #[rhai_fn(name = "+=")]
#[inline(always)]
pub fn append_string(s: &mut ImmutableString, add: ImmutableString) { pub fn append_string(s: &mut ImmutableString, add: ImmutableString) {
*s += &add; *s += &add;
} }
#[inline(always)]
pub fn len(s: &mut ImmutableString) -> INT { pub fn len(s: &mut ImmutableString) -> INT {
s.chars().count() as 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) { pub fn clear(s: &mut ImmutableString) {
s.make_mut().clear(); s.make_mut().clear();
} }
@ -186,10 +199,12 @@ mod string_functions {
} }
#[rhai_fn(name = "contains")] #[rhai_fn(name = "contains")]
#[inline(always)]
pub fn contains_char(s: &mut ImmutableString, ch: char) -> bool { pub fn contains_char(s: &mut ImmutableString, ch: char) -> bool {
s.contains(ch) s.contains(ch)
} }
#[rhai_fn(name = "contains")] #[rhai_fn(name = "contains")]
#[inline(always)]
pub fn contains_string(s: &mut ImmutableString, find: ImmutableString) -> bool { pub fn contains_string(s: &mut ImmutableString, find: ImmutableString) -> bool {
s.contains(find.as_str()) s.contains(find.as_str())
} }
@ -270,6 +285,7 @@ mod string_functions {
.into() .into()
} }
#[rhai_fn(name = "sub_string")] #[rhai_fn(name = "sub_string")]
#[inline(always)]
pub fn sub_string_starting_from(s: ImmutableString, start: INT) -> ImmutableString { pub fn sub_string_starting_from(s: ImmutableString, start: INT) -> ImmutableString {
let len = s.len() as INT; let len = s.len() as INT;
sub_string(s, start, len) sub_string(s, start, len)
@ -302,23 +318,28 @@ mod string_functions {
copy.extend(chars.iter().skip(offset).take(len)); copy.extend(chars.iter().skip(offset).take(len));
} }
#[rhai_fn(name = "crop")] #[rhai_fn(name = "crop")]
#[inline(always)]
pub fn crop_string_starting_from(s: &mut ImmutableString, start: INT) { pub fn crop_string_starting_from(s: &mut ImmutableString, start: INT) {
crop_string(s, start, s.len() as INT); crop_string(s, start, s.len() as INT);
} }
#[rhai_fn(name = "replace")] #[rhai_fn(name = "replace")]
#[inline(always)]
pub fn replace_string(s: &mut ImmutableString, find: ImmutableString, sub: ImmutableString) { pub fn replace_string(s: &mut ImmutableString, find: ImmutableString, sub: ImmutableString) {
*s = s.replace(find.as_str(), sub.as_str()).into(); *s = s.replace(find.as_str(), sub.as_str()).into();
} }
#[rhai_fn(name = "replace")] #[rhai_fn(name = "replace")]
#[inline(always)]
pub fn replace_string_with_char(s: &mut ImmutableString, find: ImmutableString, sub: char) { pub fn replace_string_with_char(s: &mut ImmutableString, find: ImmutableString, sub: char) {
*s = s.replace(find.as_str(), &sub.to_string()).into(); *s = s.replace(find.as_str(), &sub.to_string()).into();
} }
#[rhai_fn(name = "replace")] #[rhai_fn(name = "replace")]
#[inline(always)]
pub fn replace_char_with_string(s: &mut ImmutableString, find: char, sub: ImmutableString) { pub fn replace_char_with_string(s: &mut ImmutableString, find: char, sub: ImmutableString) {
*s = s.replace(&find.to_string(), sub.as_str()).into(); *s = s.replace(&find.to_string(), sub.as_str()).into();
} }
#[rhai_fn(name = "replace")] #[rhai_fn(name = "replace")]
#[inline(always)]
pub fn replace_char(s: &mut ImmutableString, find: char, sub: char) { pub fn replace_char(s: &mut ImmutableString, find: char, sub: char) {
*s = s.replace(&find.to_string(), &sub.to_string()).into(); *s = s.replace(&find.to_string(), &sub.to_string()).into();
} }
@ -330,20 +351,13 @@ mod index_functions {
use crate::engine::Array; use crate::engine::Array;
#[rhai_fn(name = "+")] #[rhai_fn(name = "+")]
#[inline]
pub fn append(x: &mut ImmutableString, y: Array) -> String { pub fn append(x: &mut ImmutableString, y: Array) -> String {
format!("{}{:?}", x, y) format!("{}{:?}", x, y)
} }
#[rhai_fn(name = "+")] #[rhai_fn(name = "+")]
#[inline]
pub fn prepend(x: &mut Array, y: ImmutableString) -> String { pub fn prepend(x: &mut Array, y: ImmutableString) -> String {
format!("{:?}{}", x, y) 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)
}
}

View File

@ -27,13 +27,11 @@ use instant::Instant;
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
// Register date/time functions // Register date/time functions
lib.combine(exported_module!(time_functions)); lib.combine(exported_module!(time_functions));
#[cfg(not(feature = "no_object"))]
lib.set_getter_fn("elapsed", time_functions::elapsed);
}); });
#[export_module] #[export_module]
mod time_functions { mod time_functions {
#[inline(always)]
pub fn timestamp() -> Instant { pub fn timestamp() -> Instant {
Instant::now() 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 = "-")] #[rhai_fn(return_raw, name = "-")]
fn time_diff(ts1: Instant, ts2: Instant) -> Result<Dynamic, Box<EvalAltResult>> { fn time_diff(ts1: Instant, ts2: Instant) -> Result<Dynamic, Box<EvalAltResult>> {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
@ -104,26 +108,32 @@ mod time_functions {
} }
#[rhai_fn(name = "==")] #[rhai_fn(name = "==")]
#[inline(always)]
pub fn eq(x: Instant, y: Instant) -> bool { pub fn eq(x: Instant, y: Instant) -> bool {
x == y x == y
} }
#[rhai_fn(name = "!=")] #[rhai_fn(name = "!=")]
#[inline(always)]
pub fn ne(x: Instant, y: Instant) -> bool { pub fn ne(x: Instant, y: Instant) -> bool {
x != y x != y
} }
#[rhai_fn(name = "<")] #[rhai_fn(name = "<")]
#[inline(always)]
pub fn lt(x: Instant, y: Instant) -> bool { pub fn lt(x: Instant, y: Instant) -> bool {
x < y x < y
} }
#[rhai_fn(name = "<=")] #[rhai_fn(name = "<=")]
#[inline(always)]
pub fn lte(x: Instant, y: Instant) -> bool { pub fn lte(x: Instant, y: Instant) -> bool {
x <= y x <= y
} }
#[rhai_fn(name = ">")] #[rhai_fn(name = ">")]
#[inline(always)]
pub fn gt(x: Instant, y: Instant) -> bool { pub fn gt(x: Instant, y: Instant) -> bool {
x > y x > y
} }
#[rhai_fn(name = ">=")] #[rhai_fn(name = ">=")]
#[inline(always)]
pub fn gte(x: Instant, y: Instant) -> bool { pub fn gte(x: Instant, y: Instant) -> bool {
x >= y x >= y
} }

View File

@ -3,22 +3,29 @@
use rhai::plugin::*; use rhai::plugin::*;
use rhai::{Engine, EvalAltResult, INT}; use rhai::{Engine, EvalAltResult, INT};
#[export_module] mod test {
mod special_array_package { use rhai::plugin::*;
#[export_module]
pub mod special_array_package {
use rhai::{Array, INT}; use rhai::{Array, INT};
#[rhai_fn(get = "foo", return_raw)] #[rhai_fn(get = "foo", return_raw)]
#[inline(always)]
pub fn foo(array: &mut Array) -> Result<Dynamic, Box<EvalAltResult>> { pub fn foo(array: &mut Array) -> Result<Dynamic, Box<EvalAltResult>> {
Ok(array[0].clone()) Ok(array[0].clone())
} }
#[rhai_fn(name = "test")] #[rhai_fn(name = "test")]
#[inline(always)]
pub fn len(array: &mut Array, mul: INT) -> INT { pub fn len(array: &mut Array, mul: INT) -> INT {
(array.len() as INT) * mul (array.len() as INT) * mul
} }
#[rhai_fn(name = "+")] #[rhai_fn(name = "+")]
#[inline(always)]
pub fn funky_add(x: INT, y: INT) -> INT { pub fn funky_add(x: INT, y: INT) -> INT {
x / 2 + y * 2 x / 2 + y * 2
} }
}
} }
macro_rules! gen_unary_functions { macro_rules! gen_unary_functions {
@ -52,7 +59,7 @@ gen_unary_functions!(greet = make_greeting(INT, bool, char) -> String);
fn test_plugins_package() -> Result<(), Box<EvalAltResult>> { fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
let m = exported_module!(special_array_package); let m = exported_module!(test::special_array_package);
engine.load_package(m); engine.load_package(m);
reg_functions!(engine += greet::single(INT, bool, char)); reg_functions!(engine += greet::single(INT, bool, char));