Register property versions of some methods.
This commit is contained in:
@@ -105,6 +105,10 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
},
|
||||
);
|
||||
lib.set_fn_1_mut("len", |list: &mut Array| Ok(list.len() as INT));
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
lib.set_getter_fn("len", |list: &mut Array| Ok(list.len() as INT));
|
||||
|
||||
lib.set_fn_1_mut("clear", |list: &mut Array| {
|
||||
list.clear();
|
||||
Ok(())
|
||||
|
@@ -43,6 +43,18 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
||||
lib.set_fn_1("is_finite", |x: FLOAT| Ok(x.is_finite()));
|
||||
lib.set_fn_1("is_infinite", |x: FLOAT| Ok(x.is_infinite()));
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
{
|
||||
lib.set_getter_fn("floor", |x: &mut FLOAT| Ok(x.floor()));
|
||||
lib.set_getter_fn("ceiling", |x: &mut FLOAT| Ok(x.ceil()));
|
||||
lib.set_getter_fn("round", |x: &mut FLOAT| Ok(x.ceil()));
|
||||
lib.set_getter_fn("int", |x: &mut FLOAT| Ok(x.trunc()));
|
||||
lib.set_getter_fn("fraction", |x: &mut FLOAT| Ok(x.fract()));
|
||||
lib.set_getter_fn("is_nan", |x: &mut FLOAT| Ok(x.is_nan()));
|
||||
lib.set_getter_fn("is_finite", |x: &mut FLOAT| Ok(x.is_finite()));
|
||||
lib.set_getter_fn("is_infinite", |x: &mut FLOAT| Ok(x.is_infinite()));
|
||||
}
|
||||
|
||||
// Register conversion functions
|
||||
lib.set_fn_1("to_float", |x: INT| Ok(x as FLOAT));
|
||||
lib.set_fn_1("to_float", |x: f32| Ok(x as FLOAT));
|
||||
|
@@ -106,6 +106,10 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
}
|
||||
|
||||
lib.set_fn_1_mut("len", |s: &mut ImmutableString| Ok(s.chars().count() as INT));
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
lib.set_getter_fn("len", |s: &mut ImmutableString| Ok(s.chars().count() as INT));
|
||||
|
||||
lib.set_fn_2_mut(
|
||||
"contains",
|
||||
|s: &mut ImmutableString, ch: char| Ok(s.contains(ch)),
|
||||
|
@@ -10,6 +10,9 @@ use crate::token::Position;
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
use crate::stdlib::time::Instant;
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
use crate::parser::FLOAT;
|
||||
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
|
||||
// Register date/time functions
|
||||
@@ -70,27 +73,29 @@ def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
|
||||
lib.set_fn_2("==", eq::<Instant>);
|
||||
lib.set_fn_2("!=", ne::<Instant>);
|
||||
|
||||
lib.set_fn_1(
|
||||
"elapsed",
|
||||
|timestamp: Instant| {
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
return Ok(timestamp.elapsed().as_secs_f64());
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
fn elapsed (timestamp: &mut Instant) -> Result<FLOAT, Box<EvalAltResult>> {
|
||||
Ok(timestamp.elapsed().as_secs_f64())
|
||||
}
|
||||
|
||||
#[cfg(feature = "no_float")]
|
||||
{
|
||||
let seconds = timestamp.elapsed().as_secs();
|
||||
#[cfg(feature = "no_float")]
|
||||
fn elapsed (timestamp: &mut Instant) -> Result<INT, Box<EvalAltResult>> {
|
||||
let seconds = timestamp.elapsed().as_secs();
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
{
|
||||
if seconds > (MAX_INT as u64) {
|
||||
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
||||
format!("Integer overflow for timestamp.elapsed(): {}", seconds),
|
||||
Position::none(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
return Ok(seconds as INT);
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
{
|
||||
if seconds > (MAX_INT as u64) {
|
||||
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
||||
format!("Integer overflow for timestamp.elapsed: {}", seconds),
|
||||
Position::none(),
|
||||
)));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
Ok(seconds as INT)
|
||||
}
|
||||
|
||||
lib.set_fn_1_mut("elapsed", elapsed);
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
lib.set_getter_fn("elapsed", elapsed);
|
||||
});
|
||||
|
Reference in New Issue
Block a user