2020-05-06 13:45:17 +02:00
|
|
|
#![cfg(not(feature = "no_object"))]
|
|
|
|
|
2020-07-31 16:30:23 +02:00
|
|
|
use crate::any::Dynamic;
|
2020-04-21 17:25:12 +02:00
|
|
|
use crate::def_package;
|
2020-08-14 18:04:10 +02:00
|
|
|
use crate::engine::{make_getter, Map};
|
2020-05-25 07:44:28 +02:00
|
|
|
use crate::parser::{ImmutableString, INT};
|
2020-08-14 18:04:10 +02:00
|
|
|
use crate::plugin::*;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-06-21 10:37:05 +02:00
|
|
|
use crate::stdlib::vec::Vec;
|
2020-04-24 06:39:24 +02:00
|
|
|
|
2020-04-22 08:55:40 +02:00
|
|
|
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
2020-08-14 18:04:10 +02:00
|
|
|
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
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
lib.combine(exported_module!(index_functions));
|
|
|
|
});
|
|
|
|
|
|
|
|
#[export_module]
|
|
|
|
mod map_functions {
|
|
|
|
pub fn has(map: &mut Map, prop: ImmutableString) -> bool {
|
|
|
|
map.contains_key(&prop)
|
|
|
|
}
|
|
|
|
pub fn len(map: &mut Map) -> INT {
|
|
|
|
map_funcs::len(map)
|
|
|
|
}
|
|
|
|
pub fn clear(map: &mut Map) {
|
2020-05-13 13:21:42 +02:00
|
|
|
map.clear();
|
2020-08-14 18:04:10 +02:00
|
|
|
}
|
|
|
|
pub fn remove(x: &mut Map, name: ImmutableString) -> Dynamic {
|
|
|
|
x.remove(&name).unwrap_or_else(|| ().into())
|
|
|
|
}
|
|
|
|
pub fn mixin(map1: &mut Map, map2: Map) {
|
|
|
|
map_funcs::mixin(map1, map2);
|
|
|
|
}
|
|
|
|
pub fn fill_with(map1: &mut Map, map2: Map) {
|
|
|
|
map2.into_iter().for_each(|(key, value)| {
|
|
|
|
if !map1.contains_key(&key) {
|
2020-04-21 17:01:10 +02:00
|
|
|
map1.insert(key, value);
|
2020-08-14 18:04:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
|
2020-08-14 18:04:10 +02:00
|
|
|
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
|
2020-07-31 16:30:23 +02:00
|
|
|
}
|
2020-08-14 18:04:10 +02:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
|
2020-08-14 18:04:10 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
#[export_module]
|
|
|
|
mod index_functions {
|
|
|
|
pub fn keys(map: &mut Map) -> Vec<Dynamic> {
|
|
|
|
map.iter().map(|(k, _)| k.clone().into()).collect()
|
2020-07-31 16:30:23 +02:00
|
|
|
}
|
2020-08-14 18:04:10 +02:00
|
|
|
pub fn values(map: &mut Map) -> Vec<Dynamic> {
|
|
|
|
map.iter().map(|(_, v)| v.clone()).collect()
|
|
|
|
}
|
|
|
|
}
|