Check for constant values passed to methods.

This commit is contained in:
Stephen Chung
2021-02-19 23:13:53 +08:00
parent ac1b7debe9
commit 182fc2c3d1
13 changed files with 360 additions and 300 deletions

View File

@@ -13,52 +13,54 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
#[export_module]
mod map_functions {
#[rhai_fn(pure)]
pub fn has(map: &mut Map, prop: ImmutableString) -> bool {
map.contains_key(&prop)
}
#[rhai_fn(pure)]
pub fn len(map: &mut Map) -> INT {
map.len() as INT
}
pub fn clear(map: &mut Map) {
map.clear();
}
pub fn remove(x: &mut Map, name: ImmutableString) -> Dynamic {
x.remove(&name).unwrap_or_else(|| ().into())
pub fn remove(map: &mut Map, name: ImmutableString) -> Dynamic {
map.remove(&name).unwrap_or_else(|| ().into())
}
#[rhai_fn(name = "mixin", name = "+=")]
pub fn mixin(map1: &mut Map, map2: Map) {
pub fn mixin(map: &mut Map, map2: Map) {
map2.into_iter().for_each(|(key, value)| {
map1.insert(key, value);
map.insert(key, value);
});
}
#[rhai_fn(name = "+")]
pub fn merge(mut map1: Map, map2: Map) -> Map {
pub fn merge(mut map: Map, map2: Map) -> Map {
map2.into_iter().for_each(|(key, value)| {
map1.insert(key, value);
map.insert(key, value);
});
map1
map
}
pub fn fill_with(map1: &mut Map, map2: Map) {
pub fn fill_with(map: &mut Map, map2: Map) {
map2.into_iter().for_each(|(key, value)| {
map1.entry(key).or_insert(value);
map.entry(key).or_insert(value);
});
}
#[rhai_fn(name = "==", return_raw)]
#[rhai_fn(name = "==", return_raw, pure)]
pub fn equals(
ctx: NativeCallContext,
map1: &mut Map,
map: &mut Map,
mut map2: Map,
) -> Result<Dynamic, Box<EvalAltResult>> {
if map1.len() != map2.len() {
if map.len() != map2.len() {
return Ok(false.into());
}
if map1.is_empty() {
if map.is_empty() {
return Ok(true.into());
}
let def_value = Some(false.into());
for (m1, v1) in map1.iter_mut() {
for (m1, v1) in map.iter_mut() {
if let Some(v2) = map2.get_mut(m1) {
let equals = ctx
.call_fn_dynamic_raw(OP_EQUALS, true, false, &mut [v1, v2], def_value.as_ref())
@@ -74,20 +76,22 @@ mod map_functions {
Ok(true.into())
}
#[rhai_fn(name = "!=", return_raw)]
#[rhai_fn(name = "!=", return_raw, pure)]
pub fn not_equals(
ctx: NativeCallContext,
map1: &mut Map,
map: &mut Map,
map2: Map,
) -> Result<Dynamic, Box<EvalAltResult>> {
equals(ctx, map1, map2).map(|r| (!r.as_bool().unwrap()).into())
equals(ctx, map, map2).map(|r| (!r.as_bool().unwrap()).into())
}
#[cfg(not(feature = "no_index"))]
pub mod indexing {
#[rhai_fn(pure)]
pub fn keys(map: &mut Map) -> Array {
map.iter().map(|(k, _)| k.clone().into()).collect()
}
#[rhai_fn(pure)]
pub fn values(map: &mut Map) -> Array {
map.iter().map(|(_, v)| v.clone()).collect()
}