Change Map keys to Identifier.

This commit is contained in:
Stephen Chung 2021-03-29 17:14:22 +08:00
parent 41b48d591f
commit e306a92ea0
7 changed files with 21 additions and 14 deletions

View File

@ -1548,7 +1548,7 @@ pub enum Expr {
Array(Box<StaticVec<Expr>>, Position), Array(Box<StaticVec<Expr>>, Position),
/// #{ name:expr, ... } /// #{ name:expr, ... }
Map( Map(
Box<(StaticVec<(Ident, Expr)>, BTreeMap<ImmutableString, Dynamic>)>, Box<(StaticVec<(Ident, Expr)>, BTreeMap<Identifier, Dynamic>)>,
Position, Position,
), ),
/// () /// ()

View File

@ -1712,7 +1712,7 @@ impl<T: Variant + Clone> crate::stdlib::iter::FromIterator<T> for Dynamic {
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
impl<K: Into<ImmutableString>, T: Variant + Clone> From<crate::stdlib::collections::HashMap<K, T>> impl<K: Into<crate::Identifier>, T: Variant + Clone> From<crate::stdlib::collections::HashMap<K, T>>
for Dynamic for Dynamic
{ {
#[inline(always)] #[inline(always)]
@ -1729,8 +1729,8 @@ impl<K: Into<ImmutableString>, T: Variant + Clone> From<crate::stdlib::collectio
} }
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
impl<K: Into<ImmutableString>, T: Variant + Clone> From<crate::stdlib::collections::BTreeMap<K, T>> impl<K: Into<crate::Identifier>, T: Variant + Clone>
for Dynamic From<crate::stdlib::collections::BTreeMap<K, T>> for Dynamic
{ {
#[inline(always)] #[inline(always)]
fn from(value: crate::stdlib::collections::BTreeMap<K, T>) -> Self { fn from(value: crate::stdlib::collections::BTreeMap<K, T>) -> Self {

View File

@ -1573,12 +1573,12 @@ impl Engine {
self.make_type_mismatch_err::<ImmutableString>(idx.type_name(), idx_pos) self.make_type_mismatch_err::<ImmutableString>(idx.type_name(), idx_pos)
})?; })?;
if _create && !map.contains_key(index) { if _create && !map.contains_key(index.as_str()) {
map.insert(index.clone(), Default::default()); map.insert(index.clone().into(), Default::default());
} }
Ok(map Ok(map
.get_mut(index) .get_mut(index.as_str())
.map(Target::from) .map(Target::from)
.unwrap_or_else(|| Target::from(()))) .unwrap_or_else(|| Target::from(())))
} }

View File

@ -85,6 +85,13 @@ pub fn get_builtin_binary_op_fn(
Ok(x.$func(y).into()) Ok(x.$func(y).into())
}) })
}; };
($xx:ident . $func:ident ( $yy:ident . $yyy:ident () )) => {
return Some(|_, args| {
let x = &*args[0].read_lock::<$xx>().unwrap();
let y = &*args[1].read_lock::<$yy>().unwrap();
Ok(x.$func(y.$yyy()).into())
})
};
($func:ident ( $op:tt )) => { ($func:ident ( $op:tt )) => {
return Some(|_, args| { return Some(|_, args| {
let (x, y) = $func(args); let (x, y) = $func(args);
@ -284,7 +291,7 @@ pub fn get_builtin_binary_op_fn(
use crate::Map; use crate::Map;
match op { match op {
OP_CONTAINS => impl_op!(Map.contains_key(ImmutableString)), OP_CONTAINS => impl_op!(Map.contains_key(ImmutableString.as_str())),
_ => return None, _ => return None,
} }
} }

View File

@ -189,7 +189,7 @@ pub type Array = stdlib::vec::Vec<Dynamic>;
/// ///
/// Not available under `no_object`. /// Not available under `no_object`.
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
pub type Map = stdlib::collections::BTreeMap<ImmutableString, Dynamic>; pub type Map = stdlib::collections::BTreeMap<Identifier, Dynamic>;
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub use module::ModuleResolver; pub use module::ModuleResolver;

View File

@ -15,7 +15,7 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
mod map_functions { mod map_functions {
#[rhai_fn(name = "has", pure)] #[rhai_fn(name = "has", pure)]
pub fn contains(map: &mut Map, prop: ImmutableString) -> bool { pub fn contains(map: &mut Map, prop: ImmutableString) -> bool {
map.contains_key(&prop) map.contains_key(prop.as_str())
} }
#[rhai_fn(pure)] #[rhai_fn(pure)]
pub fn len(map: &mut Map) -> INT { pub fn len(map: &mut Map) -> INT {
@ -25,7 +25,7 @@ mod map_functions {
map.clear(); map.clear();
} }
pub fn remove(map: &mut Map, name: ImmutableString) -> Dynamic { pub fn remove(map: &mut Map, name: ImmutableString) -> Dynamic {
map.remove(&name).unwrap_or_else(|| ().into()) map.remove(name.as_str()).unwrap_or_else(|| ().into())
} }
#[rhai_fn(name = "mixin", name = "+=")] #[rhai_fn(name = "mixin", name = "+=")]
pub fn mixin(map: &mut Map, map2: Map) { pub fn mixin(map: &mut Map, map2: Map) {

View File

@ -24,8 +24,8 @@ use crate::syntax::{CustomSyntax, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT};
use crate::token::{is_keyword_function, is_valid_identifier, Token, TokenStream}; use crate::token::{is_keyword_function, is_valid_identifier, Token, TokenStream};
use crate::utils::{get_hasher, IdentifierBuilder}; use crate::utils::{get_hasher, IdentifierBuilder};
use crate::{ use crate::{
calc_fn_hash, Dynamic, Engine, Identifier, ImmutableString, LexError, ParseError, calc_fn_hash, Dynamic, Engine, Identifier, LexError, ParseError, ParseErrorType, Position,
ParseErrorType, Position, Scope, Shared, StaticVec, AST, Scope, Shared, StaticVec, AST,
}; };
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
@ -685,7 +685,7 @@ fn parse_map_literal(
settings.pos = eat_token(input, Token::MapStart); settings.pos = eat_token(input, Token::MapStart);
let mut map: StaticVec<(Ident, Expr)> = Default::default(); let mut map: StaticVec<(Ident, Expr)> = Default::default();
let mut template: BTreeMap<ImmutableString, Dynamic> = Default::default(); let mut template: BTreeMap<Identifier, Dynamic> = Default::default();
loop { loop {
const MISSING_RBRACE: &str = "to end this object map literal"; const MISSING_RBRACE: &str = "to end this object map literal";