Bring more functions into modules in plugins via rhai_fn(name) attribute.
This commit is contained in:
parent
31a05f8c48
commit
e75d91e9bf
@ -93,7 +93,7 @@
|
|||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
|
||||||
use quote::{quote, ToTokens};
|
use quote::quote;
|
||||||
use syn::parse_macro_input;
|
use syn::parse_macro_input;
|
||||||
|
|
||||||
mod function;
|
mod function;
|
||||||
|
@ -11,9 +11,6 @@ use crate::plugin::*;
|
|||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
use crate::{result::EvalAltResult, token::Position};
|
use crate::{result::EvalAltResult, token::Position};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
use crate::engine::make_getter;
|
|
||||||
|
|
||||||
use crate::stdlib::{any::TypeId, boxed::Box};
|
use crate::stdlib::{any::TypeId, boxed::Box};
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
@ -61,8 +58,9 @@ 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));
|
||||||
set_exported_fn!(lib, "+", append);
|
|
||||||
set_exported_fn!(lib, "+", concat);
|
#[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);
|
||||||
@ -86,9 +84,6 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
|||||||
reg_pad!(lib, f32, f64);
|
reg_pad!(lib, f32, f64);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
set_exported_fn!(lib, make_getter("len"), array_funcs::len);
|
|
||||||
|
|
||||||
// Register array iterator
|
// Register array iterator
|
||||||
lib.set_iter(
|
lib.set_iter(
|
||||||
TypeId::of::<Array>(),
|
TypeId::of::<Array>(),
|
||||||
@ -96,24 +91,23 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
#[export_fn]
|
|
||||||
fn append(x: &mut Array, y: Array) {
|
|
||||||
x.extend(y);
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
fn concat(mut x: Array, y: Array) -> Array {
|
|
||||||
x.extend(y);
|
|
||||||
x
|
|
||||||
}
|
|
||||||
|
|
||||||
#[export_module]
|
#[export_module]
|
||||||
mod array_functions {
|
mod array_functions {
|
||||||
pub fn len(list: &mut Array) -> INT {
|
pub fn len(list: &mut Array) -> INT {
|
||||||
array_funcs::len(list)
|
list.len() as INT
|
||||||
}
|
}
|
||||||
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 = "+=")]
|
||||||
|
pub fn append_operator(x: &mut Array, y: Array) {
|
||||||
|
append(x, y)
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "+")]
|
||||||
|
pub fn concat(mut x: Array, y: Array) -> Array {
|
||||||
|
x.extend(y);
|
||||||
|
x
|
||||||
|
}
|
||||||
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())
|
||||||
}
|
}
|
||||||
@ -143,14 +137,12 @@ mod array_functions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod array_funcs {
|
#[cfg(not(feature = "no_object"))]
|
||||||
use crate::engine::Array;
|
#[export_module]
|
||||||
use crate::parser::INT;
|
mod object_functions {
|
||||||
use crate::plugin::*;
|
#[rhai_fn(name = "get$len")]
|
||||||
|
|
||||||
#[export_fn]
|
|
||||||
pub fn len(list: &mut Array) -> INT {
|
pub fn len(list: &mut Array) -> INT {
|
||||||
list.len() as INT
|
array_functions::len(list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,11 +4,11 @@ use crate::parser::ImmutableString;
|
|||||||
use crate::plugin::*;
|
use crate::plugin::*;
|
||||||
use crate::result::EvalAltResult;
|
use crate::result::EvalAltResult;
|
||||||
|
|
||||||
|
def_package!(crate:EvalPackage:"Disable 'eval'.", lib, {
|
||||||
|
set_exported_fn!(lib, "eval", eval_override);
|
||||||
|
});
|
||||||
|
|
||||||
#[export_fn(return_raw)]
|
#[export_fn(return_raw)]
|
||||||
fn eval_override(_script: ImmutableString) -> Result<Dynamic, Box<EvalAltResult>> {
|
fn eval_override(_script: ImmutableString) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
Err("eval is evil!".into())
|
Err("eval is evil!".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
def_package!(crate:EvalPackage:"Disable 'eval'.", lib, {
|
|
||||||
set_exported_fn!(lib, "eval", eval_override);
|
|
||||||
});
|
|
||||||
|
@ -5,14 +5,14 @@ use crate::plugin::*;
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
use crate::engine::make_getter;
|
use crate::engine::make_getter;
|
||||||
|
|
||||||
#[export_fn]
|
|
||||||
fn get_fn_name(f: &mut FnPtr) -> ImmutableString {
|
|
||||||
f.get_fn_name().clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
|
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
|
||||||
set_exported_fn!(lib, "name", get_fn_name);
|
set_exported_fn!(lib, "name", get_fn_name);
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
set_exported_fn!(lib, make_getter("name"), get_fn_name);
|
set_exported_fn!(lib, make_getter("name"), get_fn_name);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
#[export_fn]
|
||||||
|
fn get_fn_name(f: &mut FnPtr) -> ImmutableString {
|
||||||
|
f.get_fn_name().clone()
|
||||||
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use crate::any::Dynamic;
|
use crate::any::Dynamic;
|
||||||
use crate::def_package;
|
use crate::def_package;
|
||||||
use crate::engine::{make_getter, Map};
|
use crate::engine::Map;
|
||||||
use crate::parser::{ImmutableString, INT};
|
use crate::parser::{ImmutableString, INT};
|
||||||
use crate::plugin::*;
|
use crate::plugin::*;
|
||||||
|
|
||||||
@ -10,9 +10,6 @@ use crate::stdlib::vec::Vec;
|
|||||||
|
|
||||||
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
||||||
lib.combine(exported_module!(map_functions));
|
lib.combine(exported_module!(map_functions));
|
||||||
set_exported_fn!(lib, make_getter("len"), map_funcs::len);
|
|
||||||
set_exported_fn!(lib, "+=", map_funcs::mixin);
|
|
||||||
set_exported_fn!(lib, "+", map_funcs::merge);
|
|
||||||
|
|
||||||
// Register map access functions
|
// Register map access functions
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -25,7 +22,11 @@ mod map_functions {
|
|||||||
map.contains_key(&prop)
|
map.contains_key(&prop)
|
||||||
}
|
}
|
||||||
pub fn len(map: &mut Map) -> INT {
|
pub fn len(map: &mut Map) -> INT {
|
||||||
map_funcs::len(map)
|
map.len() as INT
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "get$len")]
|
||||||
|
pub fn len_prop(map: &mut Map) -> INT {
|
||||||
|
len(map)
|
||||||
}
|
}
|
||||||
pub fn clear(map: &mut Map) {
|
pub fn clear(map: &mut Map) {
|
||||||
map.clear();
|
map.clear();
|
||||||
@ -34,7 +35,20 @@ mod map_functions {
|
|||||||
x.remove(&name).unwrap_or_else(|| ().into())
|
x.remove(&name).unwrap_or_else(|| ().into())
|
||||||
}
|
}
|
||||||
pub fn mixin(map1: &mut Map, map2: Map) {
|
pub fn mixin(map1: &mut Map, map2: Map) {
|
||||||
map_funcs::mixin(map1, map2);
|
map2.into_iter().for_each(|(key, value)| {
|
||||||
|
map1.insert(key, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "+=")]
|
||||||
|
pub fn mixin_operator(map1: &mut Map, map2: Map) {
|
||||||
|
mixin(map1, map2)
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "+")]
|
||||||
|
pub fn merge(mut map1: Map, map2: Map) -> Map {
|
||||||
|
map2.into_iter().for_each(|(key, value)| {
|
||||||
|
map1.insert(key, value);
|
||||||
|
});
|
||||||
|
map1
|
||||||
}
|
}
|
||||||
pub fn fill_with(map1: &mut Map, map2: Map) {
|
pub fn fill_with(map1: &mut Map, map2: Map) {
|
||||||
map2.into_iter().for_each(|(key, value)| {
|
map2.into_iter().for_each(|(key, value)| {
|
||||||
@ -45,30 +59,6 @@ mod map_functions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod map_funcs {
|
|
||||||
use crate::engine::Map;
|
|
||||||
use crate::parser::INT;
|
|
||||||
use crate::plugin::*;
|
|
||||||
|
|
||||||
#[export_fn]
|
|
||||||
pub fn len(map: &mut Map) -> INT {
|
|
||||||
map.len() as INT
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
pub fn mixin(map1: &mut Map, map2: Map) {
|
|
||||||
map2.into_iter().for_each(|(key, value)| {
|
|
||||||
map1.insert(key, value);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
pub fn merge(mut map1: Map, map2: Map) -> Map {
|
|
||||||
map2.into_iter().for_each(|(key, value)| {
|
|
||||||
map1.insert(key, value);
|
|
||||||
});
|
|
||||||
map1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
#[export_module]
|
#[export_module]
|
||||||
mod index_functions {
|
mod index_functions {
|
||||||
|
@ -11,9 +11,6 @@ use crate::utils::StaticVec;
|
|||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
use crate::{result::EvalAltResult, token::Position};
|
use crate::{result::EvalAltResult, token::Position};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
use crate::engine::make_getter;
|
|
||||||
|
|
||||||
use crate::stdlib::{
|
use crate::stdlib::{
|
||||||
any::TypeId, boxed::Box, fmt::Display, format, mem, string::String, string::ToString, vec::Vec,
|
any::TypeId, boxed::Box, fmt::Display, format, mem, string::String, string::ToString, vec::Vec,
|
||||||
};
|
};
|
||||||
@ -61,29 +58,12 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
|||||||
reg_functions!(lib += float; f32, f64);
|
reg_functions!(lib += float; f32, f64);
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
{
|
lib.combine(exported_module!(index_functions));
|
||||||
set_exported_fn!(lib, "+", string_funcs_array::append_array);
|
|
||||||
set_exported_fn!(lib, "+", string_funcs_array::prepend_array);
|
#[cfg(not(feature = "no_object"))]
|
||||||
}
|
lib.combine(exported_module!(object_functions));
|
||||||
|
|
||||||
lib.combine(exported_module!(string_functions));
|
lib.combine(exported_module!(string_functions));
|
||||||
set_exported_fn!(lib, "contains", string_funcs::contains_char);
|
|
||||||
set_exported_fn!(lib, "contains", string_funcs::contains_string);
|
|
||||||
set_exported_fn!(lib, "index_of", string_funcs::index_of_char);
|
|
||||||
set_exported_fn!(lib, "index_of", string_funcs::index_of_char_starting_from);
|
|
||||||
set_exported_fn!(lib, "index_of", string_funcs::index_of_string);
|
|
||||||
set_exported_fn!(lib, "index_of", string_funcs::index_of_string_starting_from);
|
|
||||||
set_exported_fn!(lib, "append", string_funcs::append_char);
|
|
||||||
set_exported_fn!(lib, "+=", string_funcs::append_char);
|
|
||||||
set_exported_fn!(lib, "append", string_funcs::append_string);
|
|
||||||
set_exported_fn!(lib, "sub_string", string_funcs::sub_string);
|
|
||||||
set_exported_fn!(lib, "sub_string", string_funcs::sub_string_starting_from);
|
|
||||||
set_exported_fn!(lib, "crop", string_funcs::crop_string);
|
|
||||||
set_exported_fn!(lib, "crop", string_funcs::crop_string_starting_from);
|
|
||||||
set_exported_fn!(lib, "replace", string_funcs::replace_string);
|
|
||||||
set_exported_fn!(lib, "replace", string_funcs::replace_char);
|
|
||||||
set_exported_fn!(lib, "replace", string_funcs::replace_string_with_char);
|
|
||||||
set_exported_fn!(lib, "replace", string_funcs::replace_char_with_string);
|
|
||||||
|
|
||||||
lib.set_raw_fn(
|
lib.set_raw_fn(
|
||||||
"pad",
|
"pad",
|
||||||
@ -131,9 +111,6 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
set_exported_fn!(lib, make_getter("len"), string_funcs::len);
|
|
||||||
|
|
||||||
// Register string iterator
|
// Register string iterator
|
||||||
lib.set_iter(
|
lib.set_iter(
|
||||||
TypeId::of::<ImmutableString>(),
|
TypeId::of::<ImmutableString>(),
|
||||||
@ -166,9 +143,27 @@ gen_concat_functions!(float => f32, f64);
|
|||||||
|
|
||||||
#[export_module]
|
#[export_module]
|
||||||
mod string_functions {
|
mod string_functions {
|
||||||
pub fn len(s: &mut ImmutableString) -> INT {
|
#[rhai_fn(name = "+")]
|
||||||
string_funcs::len(s)
|
pub fn append_unit(s: ImmutableString, _x: ()) -> ImmutableString {
|
||||||
|
s
|
||||||
}
|
}
|
||||||
|
#[rhai_fn(name = "+")]
|
||||||
|
pub fn prepend_unit(_x: (), s: ImmutableString) -> ImmutableString {
|
||||||
|
s
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "+")]
|
||||||
|
pub fn append_char(s: &mut ImmutableString, ch: char) {
|
||||||
|
*s += ch;
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "+")]
|
||||||
|
pub fn append_string(s: &mut ImmutableString, add: ImmutableString) {
|
||||||
|
*s += &add;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn len(s: &mut ImmutableString) -> INT {
|
||||||
|
s.chars().count() as INT
|
||||||
|
}
|
||||||
|
|
||||||
pub fn clear(s: &mut ImmutableString) {
|
pub fn clear(s: &mut ImmutableString) {
|
||||||
s.make_mut().clear();
|
s.make_mut().clear();
|
||||||
}
|
}
|
||||||
@ -189,52 +184,17 @@ mod string_functions {
|
|||||||
*s = trimmed.to_string().into();
|
*s = trimmed.to_string().into();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[rhai_fn(name = "contains")]
|
||||||
mod string_funcs_array {
|
|
||||||
use crate::engine::Array;
|
|
||||||
use crate::plugin::*;
|
|
||||||
use crate::stdlib::string::String;
|
|
||||||
use crate::utils::ImmutableString;
|
|
||||||
|
|
||||||
#[export_fn]
|
|
||||||
pub fn append_array(x: &mut ImmutableString, y: Array) -> String {
|
|
||||||
format!("{}{:?}", x, y)
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
pub fn prepend_array(x: &mut Array, y: ImmutableString) -> String {
|
|
||||||
format!("{:?}{}", x, y)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod string_funcs {
|
|
||||||
use crate::parser::INT;
|
|
||||||
use crate::plugin::*;
|
|
||||||
use crate::stdlib::string::{String, ToString};
|
|
||||||
use crate::utils::{ImmutableString, StaticVec};
|
|
||||||
|
|
||||||
#[export_fn]
|
|
||||||
pub fn append_unit(s: ImmutableString, _x: ()) -> ImmutableString {
|
|
||||||
s
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
pub fn prepend_unit(_x: (), s: ImmutableString) -> ImmutableString {
|
|
||||||
s
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
pub fn len(s: &mut ImmutableString) -> INT {
|
|
||||||
s.chars().count() as INT
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "contains")]
|
||||||
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())
|
||||||
}
|
}
|
||||||
#[export_fn]
|
|
||||||
|
#[rhai_fn(name = "index_of")]
|
||||||
pub fn index_of_char_starting_from(s: &mut ImmutableString, ch: char, start: INT) -> INT {
|
pub fn index_of_char_starting_from(s: &mut ImmutableString, ch: char, start: INT) -> INT {
|
||||||
let start = if start < 0 {
|
let start = if start < 0 {
|
||||||
0
|
0
|
||||||
@ -249,13 +209,13 @@ mod string_funcs {
|
|||||||
.map(|index| s[0..start + index].chars().count() as INT)
|
.map(|index| s[0..start + index].chars().count() as INT)
|
||||||
.unwrap_or(-1 as INT)
|
.unwrap_or(-1 as INT)
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "index_of")]
|
||||||
pub fn index_of_char(s: &mut ImmutableString, ch: char) -> INT {
|
pub fn index_of_char(s: &mut ImmutableString, ch: char) -> INT {
|
||||||
s.find(ch)
|
s.find(ch)
|
||||||
.map(|index| s[0..index].chars().count() as INT)
|
.map(|index| s[0..index].chars().count() as INT)
|
||||||
.unwrap_or(-1 as INT)
|
.unwrap_or(-1 as INT)
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "index_of")]
|
||||||
pub fn index_of_string_starting_from(
|
pub fn index_of_string_starting_from(
|
||||||
s: &mut ImmutableString,
|
s: &mut ImmutableString,
|
||||||
find: ImmutableString,
|
find: ImmutableString,
|
||||||
@ -274,21 +234,14 @@ mod string_funcs {
|
|||||||
.map(|index| s[0..start + index].chars().count() as INT)
|
.map(|index| s[0..start + index].chars().count() as INT)
|
||||||
.unwrap_or(-1 as INT)
|
.unwrap_or(-1 as INT)
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "index_of")]
|
||||||
pub fn index_of_string(s: &mut ImmutableString, find: ImmutableString) -> INT {
|
pub fn index_of_string(s: &mut ImmutableString, find: ImmutableString) -> INT {
|
||||||
s.find(find.as_str())
|
s.find(find.as_str())
|
||||||
.map(|index| s[0..index].chars().count() as INT)
|
.map(|index| s[0..index].chars().count() as INT)
|
||||||
.unwrap_or(-1 as INT)
|
.unwrap_or(-1 as INT)
|
||||||
}
|
}
|
||||||
#[export_fn]
|
|
||||||
pub fn append_char(s: &mut ImmutableString, ch: char) {
|
#[rhai_fn(name = "sub_string")]
|
||||||
*s += ch;
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
pub fn append_string(s: &mut ImmutableString, add: ImmutableString) {
|
|
||||||
*s += &add;
|
|
||||||
}
|
|
||||||
#[export_fn]
|
|
||||||
pub fn sub_string(s: ImmutableString, start: INT, len: INT) -> ImmutableString {
|
pub fn sub_string(s: ImmutableString, start: INT, len: INT) -> ImmutableString {
|
||||||
let offset = if s.is_empty() || len <= 0 {
|
let offset = if s.is_empty() || len <= 0 {
|
||||||
return "".to_string().into();
|
return "".to_string().into();
|
||||||
@ -316,12 +269,13 @@ mod string_funcs {
|
|||||||
.collect::<String>()
|
.collect::<String>()
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "sub_string")]
|
||||||
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)
|
||||||
}
|
}
|
||||||
#[export_fn]
|
|
||||||
|
#[rhai_fn(name = "crop")]
|
||||||
fn crop_string(s: &mut ImmutableString, start: INT, len: INT) {
|
fn crop_string(s: &mut ImmutableString, start: INT, len: INT) {
|
||||||
let offset = if s.is_empty() || len <= 0 {
|
let offset = if s.is_empty() || len <= 0 {
|
||||||
s.make_mut().clear();
|
s.make_mut().clear();
|
||||||
@ -347,24 +301,49 @@ mod string_funcs {
|
|||||||
copy.clear();
|
copy.clear();
|
||||||
copy.extend(chars.iter().skip(offset).take(len));
|
copy.extend(chars.iter().skip(offset).take(len));
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "crop")]
|
||||||
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);
|
||||||
}
|
}
|
||||||
#[export_fn]
|
|
||||||
|
#[rhai_fn(name = "replace")]
|
||||||
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();
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "replace")]
|
||||||
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();
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "replace")]
|
||||||
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();
|
||||||
}
|
}
|
||||||
#[export_fn]
|
#[rhai_fn(name = "replace")]
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
#[export_module]
|
||||||
|
mod index_functions {
|
||||||
|
use crate::engine::Array;
|
||||||
|
|
||||||
|
#[rhai_fn(name = "+")]
|
||||||
|
pub fn append(x: &mut ImmutableString, y: Array) -> String {
|
||||||
|
format!("{}{:?}", x, y)
|
||||||
|
}
|
||||||
|
#[rhai_fn(name = "+")]
|
||||||
|
pub fn prepend(x: &mut Array, y: ImmutableString) -> String {
|
||||||
|
format!("{:?}{}", x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
#[export_module]
|
||||||
|
mod object_functions {
|
||||||
|
#[rhai_fn(name = "get$len")]
|
||||||
|
pub fn len(s: &mut ImmutableString) -> INT {
|
||||||
|
string_functions::len(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -18,9 +18,6 @@ use crate::parser::INT;
|
|||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
use crate::token::Position;
|
use crate::token::Position;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
use crate::engine::make_getter;
|
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use crate::stdlib::time::Instant;
|
use crate::stdlib::time::Instant;
|
||||||
|
|
||||||
@ -29,111 +26,104 @@ 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
|
||||||
set_exported_fn!(lib, "timestamp", create_timestamp);
|
lib.combine(exported_module!(time_functions));
|
||||||
set_exported_fn!(lib, "elapsed", elapsed);
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
set_exported_fn!(lib, make_getter("elapsed"), elapsed);
|
lib.set_getter_fn("elapsed", time_functions::elapsed);
|
||||||
|
|
||||||
set_exported_fn!(lib, "-", time_diff);
|
|
||||||
|
|
||||||
//lib.combine(exported_module!(time_compare));
|
|
||||||
|
|
||||||
lib.set_fn_2("<", |x:Instant, y:Instant| Ok(x < y));
|
|
||||||
lib.set_fn_2("<=", |x:Instant, y:Instant| Ok(x <= y));
|
|
||||||
lib.set_fn_2(">", |x:Instant, y:Instant| Ok(x > y));
|
|
||||||
lib.set_fn_2(">=", |x:Instant, y:Instant| Ok(x >= y));
|
|
||||||
lib.set_fn_2("==", |x:Instant, y:Instant| Ok(x == y));
|
|
||||||
lib.set_fn_2("!=", |x:Instant, y:Instant| Ok(x != y));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
#[export_fn]
|
|
||||||
fn create_timestamp() -> Instant {
|
|
||||||
Instant::now()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
|
||||||
#[export_fn]
|
|
||||||
fn elapsed(timestamp: &mut Instant) -> FLOAT {
|
|
||||||
timestamp.elapsed().as_secs_f64() as FLOAT
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "no_float")]
|
|
||||||
#[export_fn(return_raw)]
|
|
||||||
fn elapsed(timestamp: &mut Instant) -> Result<Dynamic, Box<EvalAltResult>> {
|
|
||||||
let seconds = timestamp.elapsed().as_secs();
|
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
if seconds > (MAX_INT as u64) {
|
|
||||||
return EvalAltResult::ErrorArithmetic(
|
|
||||||
format!("Integer overflow for timestamp.elapsed: {}", seconds),
|
|
||||||
Position::none(),
|
|
||||||
)
|
|
||||||
.into();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((seconds as INT).into())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
|
||||||
#[export_fn]
|
|
||||||
fn time_diff(ts1: Instant, ts2: Instant) -> FLOAT {
|
|
||||||
if ts2 > ts1 {
|
|
||||||
-(ts2 - ts1).as_secs_f64() as FLOAT
|
|
||||||
} else {
|
|
||||||
(ts1 - ts2).as_secs_f64() as FLOAT
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "no_float")]
|
|
||||||
#[export_fn(return_raw)]
|
|
||||||
fn time_diff(ts1: Instant, ts2: Instant) -> Result<Dynamic, Box<EvalAltResult>> {
|
|
||||||
if ts2 > ts1 {
|
|
||||||
let seconds = (ts2 - ts1).as_secs();
|
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
if seconds > (MAX_INT as u64) {
|
|
||||||
return EvalAltResult::ErrorArithmetic(
|
|
||||||
format!("Integer overflow for timestamp duration: -{}", seconds),
|
|
||||||
Position::none(),
|
|
||||||
)
|
|
||||||
.into();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Dynamic::from(-(seconds as INT)))
|
|
||||||
} else {
|
|
||||||
let seconds = (ts1 - ts2).as_secs();
|
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
if seconds > (MAX_INT as u64) {
|
|
||||||
return EvalAltResult::ErrorArithmetic(
|
|
||||||
format!("Integer overflow for timestamp duration: {}", seconds),
|
|
||||||
Position::none(),
|
|
||||||
)
|
|
||||||
.into();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((seconds as INT).into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[export_module]
|
#[export_module]
|
||||||
mod time_compare {
|
mod time_functions {
|
||||||
|
pub fn timestamp() -> Instant {
|
||||||
|
Instant::now()
|
||||||
|
}
|
||||||
|
#[rhai_fn(return_raw)]
|
||||||
|
pub fn elapsed(timestamp: &mut Instant) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
{
|
||||||
|
Ok((timestamp.elapsed().as_secs_f64() as FLOAT).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "no_float")]
|
||||||
|
{
|
||||||
|
let seconds = timestamp.elapsed().as_secs();
|
||||||
|
|
||||||
|
#[cfg(not(feature = "unchecked"))]
|
||||||
|
if seconds > (MAX_INT as u64) {
|
||||||
|
return EvalAltResult::ErrorArithmetic(
|
||||||
|
format!("Integer overflow for timestamp.elapsed: {}", seconds),
|
||||||
|
Position::none(),
|
||||||
|
)
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((seconds as INT).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rhai_fn(return_raw, name = "-")]
|
||||||
|
fn time_diff(ts1: Instant, ts2: Instant) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
{
|
||||||
|
Ok(if ts2 > ts1 {
|
||||||
|
-(ts2 - ts1).as_secs_f64() as FLOAT
|
||||||
|
} else {
|
||||||
|
(ts1 - ts2).as_secs_f64() as FLOAT
|
||||||
|
}
|
||||||
|
.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "no_float")]
|
||||||
|
if ts2 > ts1 {
|
||||||
|
let seconds = (ts2 - ts1).as_secs();
|
||||||
|
|
||||||
|
#[cfg(not(feature = "unchecked"))]
|
||||||
|
if seconds > (MAX_INT as u64) {
|
||||||
|
return EvalAltResult::ErrorArithmetic(
|
||||||
|
format!("Integer overflow for timestamp duration: -{}", seconds),
|
||||||
|
Position::none(),
|
||||||
|
)
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Dynamic::from(-(seconds as INT)))
|
||||||
|
} else {
|
||||||
|
let seconds = (ts1 - ts2).as_secs();
|
||||||
|
|
||||||
|
#[cfg(not(feature = "unchecked"))]
|
||||||
|
if seconds > (MAX_INT as u64) {
|
||||||
|
return EvalAltResult::ErrorArithmetic(
|
||||||
|
format!("Integer overflow for timestamp duration: {}", seconds),
|
||||||
|
Position::none(),
|
||||||
|
)
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((seconds as INT).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rhai_fn(name = "==")]
|
||||||
pub fn eq(x: Instant, y: Instant) -> bool {
|
pub fn eq(x: Instant, y: Instant) -> bool {
|
||||||
x == y
|
x == y
|
||||||
}
|
}
|
||||||
|
#[rhai_fn(name = "!=")]
|
||||||
pub fn ne(x: Instant, y: Instant) -> bool {
|
pub fn ne(x: Instant, y: Instant) -> bool {
|
||||||
x != y
|
x != y
|
||||||
}
|
}
|
||||||
|
#[rhai_fn(name = "<")]
|
||||||
pub fn lt(x: Instant, y: Instant) -> bool {
|
pub fn lt(x: Instant, y: Instant) -> bool {
|
||||||
x < y
|
x < y
|
||||||
}
|
}
|
||||||
|
#[rhai_fn(name = "<=")]
|
||||||
pub fn lte(x: Instant, y: Instant) -> bool {
|
pub fn lte(x: Instant, y: Instant) -> bool {
|
||||||
x <= y
|
x <= y
|
||||||
}
|
}
|
||||||
|
#[rhai_fn(name = ">")]
|
||||||
pub fn gt(x: Instant, y: Instant) -> bool {
|
pub fn gt(x: Instant, y: Instant) -> bool {
|
||||||
x > y
|
x > y
|
||||||
}
|
}
|
||||||
|
#[rhai_fn(name = ">=")]
|
||||||
pub fn gte(x: Instant, y: Instant) -> bool {
|
pub fn gte(x: Instant, y: Instant) -> bool {
|
||||||
x >= y
|
x >= y
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,14 @@ use rhai::{Engine, EvalAltResult, INT};
|
|||||||
mod special_array_package {
|
mod special_array_package {
|
||||||
use rhai::{Array, INT};
|
use rhai::{Array, INT};
|
||||||
|
|
||||||
|
#[rhai_fn(name = "test")]
|
||||||
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 = "+")]
|
||||||
|
pub fn funky_add(x: INT, y: INT) -> INT {
|
||||||
|
x / 2 + y * 2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! gen_unary_functions {
|
macro_rules! gen_unary_functions {
|
||||||
@ -18,7 +23,7 @@ macro_rules! gen_unary_functions {
|
|||||||
pub mod $arg_type {
|
pub mod $arg_type {
|
||||||
use super::super::*;
|
use super::super::*;
|
||||||
|
|
||||||
#[export_fn]
|
#[export_fn(name="test")]
|
||||||
pub fn single(x: $arg_type) -> $return_type {
|
pub fn single(x: $arg_type) -> $return_type {
|
||||||
super::super::$op_fn(x)
|
super::super::$op_fn(x)
|
||||||
}
|
}
|
||||||
@ -48,9 +53,10 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
|
|||||||
|
|
||||||
reg_functions!(engine += greet::single(INT, bool, char));
|
reg_functions!(engine += greet::single(INT, bool, char));
|
||||||
|
|
||||||
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; len(a, 2)")?, 6);
|
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6);
|
||||||
|
assert_eq!(engine.eval::<INT>("2 + 2")?, 5);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<String>("let a = [1, 2, 3]; greet(len(a, 2))")?,
|
engine.eval::<String>("let a = [1, 2, 3]; greet(test(a, 2))")?,
|
||||||
"6 kitties"
|
"6 kitties"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -39,5 +39,14 @@ fn test_timestamp() -> Result<(), Box<EvalAltResult>> {
|
|||||||
)? < 10
|
)? < 10
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert!(engine.eval::<bool>(
|
||||||
|
r"
|
||||||
|
let time1 = timestamp();
|
||||||
|
for x in range(0, 10000) {}
|
||||||
|
let time2 = timestamp();
|
||||||
|
time1 <= time2
|
||||||
|
"
|
||||||
|
)?);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user