Add object maps.

This commit is contained in:
Stephen Chung
2020-03-29 23:53:35 +08:00
parent ef6c6ea6d2
commit 45ee51874f
9 changed files with 632 additions and 202 deletions

View File

@@ -10,6 +10,9 @@ use crate::result::EvalAltResult;
#[cfg(not(feature = "no_index"))]
use crate::engine::Array;
#[cfg(not(feature = "no_object"))]
use crate::engine::Map;
#[cfg(not(feature = "no_float"))]
use crate::parser::FLOAT;
@@ -596,14 +599,20 @@ impl Engine<'_> {
#[cfg(not(feature = "no_index"))]
{
reg_fn1!(self, "print", debug, String, Array);
reg_fn1!(self, "debug", debug, String, Array);
self.register_fn("print", |x: &mut Array| -> String { format!("{:?}", x) });
self.register_fn("debug", |x: &mut Array| -> String { format!("{:?}", x) });
// Register array iterator
self.register_iterator::<Array, _>(|a| {
Box::new(a.downcast_ref::<Array>().unwrap().clone().into_iter())
});
}
#[cfg(not(feature = "no_object"))]
{
self.register_fn("print", |x: &mut Map| -> String { format!("${:?}", x) });
self.register_fn("debug", |x: &mut Map| -> String { format!("${:?}", x) });
}
}
// Register range function
@@ -822,6 +831,14 @@ impl Engine<'_> {
});
}
// Register map functions
#[cfg(not(feature = "no_object"))]
{
self.register_fn("has", |map: &mut Map, prop: String| map.contains_key(&prop));
self.register_fn("len", |map: &mut Map| map.len() as INT);
self.register_fn("clear", |map: &mut Map| map.clear());
}
// Register string concatenate functions
fn prepend<T: Display>(x: T, y: String) -> String {
format!("{}{}", x, y)