diff --git a/README.md b/README.md index 30878dce..2a73dfb8 100644 --- a/README.md +++ b/README.md @@ -1047,7 +1047,7 @@ Object maps ----------- Object maps are dictionaries. Properties of any type (`Dynamic`) can be freely added and retrieved. -Object map literals are built within braces '`${`' ... '`}`' (_name_ `:` _value_ syntax similar to Rust) +Object map literals are built within braces '`#{`' ... '`}`' (_name_ `:` _value_ syntax similar to Rust) and separated by commas '`,`'. The property _name_ can be a simple variable name following the same naming rules as [variables], or an arbitrary string literal. @@ -1074,7 +1074,7 @@ The following functions (defined in the standard library but excluded if [`no_st Examples: ```rust -let y = ${ // object map literal with 3 properties +let y = #{ // object map literal with 3 properties a: 1, bar: "hello", "baz!$@": 123.456, // like JS, you can use any string as property names... @@ -1095,11 +1095,11 @@ ts.obj = y; // object maps can be assigned completely (by value copy let foo = ts.list.a; foo == 42; -let foo = ${ a:1, b:2, c:3 }["a"]; +let foo = #{ a:1, b:2, c:3 }["a"]; foo == 1; fn abc() { - ${ a:1, b:2, c:3 } // a function returning an object map + #{ a:1, b:2, c:3 } // a function returning an object map } let foo = abc().b; diff --git a/src/builtin.rs b/src/builtin.rs index 4f297572..754acec0 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -610,8 +610,8 @@ impl Engine<'_> { #[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) }); + self.register_fn("print", |x: &mut Map| -> String { format!("#{:?}", x) }); + self.register_fn("debug", |x: &mut Map| -> String { format!("#{:?}", x) }); } } diff --git a/src/parser.rs b/src/parser.rs index 64fc5341..96ad6000 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -368,7 +368,7 @@ pub enum Expr { /// [ expr, ... ] Array(Vec, Position), #[cfg(not(feature = "no_object"))] - /// ${ name:expr, ... } + /// #{ name:expr, ... } Map(Vec<(String, Expr, Position)>, Position), /// lhs && rhs And(Box, Box), @@ -627,7 +627,7 @@ impl Token { Comma => ",", Period => ".", #[cfg(not(feature = "no_object"))] - MapStart => "${", + MapStart => "#{", Equals => "=", True => "true", False => "false", @@ -1159,7 +1159,7 @@ impl<'a> TokenIterator<'a> { // Map literal #[cfg(not(feature = "no_object"))] - ('$', '{') => { + ('#', '{') => { self.eat_next(); return Some((Token::MapStart, pos)); } diff --git a/tests/maps.rs b/tests/maps.rs index 608c7fad..0da9b589 100644 --- a/tests/maps.rs +++ b/tests/maps.rs @@ -1,31 +1,34 @@ #![cfg(not(feature = "no_object"))] -#![cfg(not(feature = "no_index"))] -use rhai::{AnyExt, Dynamic, Engine, EvalAltResult, Map, RegisterFn, INT}; +use rhai::{AnyExt, Engine, EvalAltResult, Map, INT}; #[test] fn test_map_indexing() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); + #[cfg(not(feature = "no_index"))] assert_eq!( - engine.eval::(r#"let x = ${a: 1, b: 2, c: 3}; x["b"]"#)?, + engine.eval::(r#"let x = #{a: 1, b: 2, c: 3}; x["b"]"#)?, 2 ); + assert_eq!( - engine.eval::("let y = ${a: 1, b: 2, c: 3}; y.a = 5; y.a")?, + engine.eval::("let y = #{a: 1, b: 2, c: 3}; y.a = 5; y.a")?, 5 ); + + #[cfg(not(feature = "no_index"))] assert_eq!( engine.eval::( r#" - let y = ${d: 1, "e": ${a: 42, b: 88, "": "93"}, " 123 xyz": 9}; - y.e[""][1] + let y = #{d: 1, "e": #{a: 42, b: 88, "": "hello"}, " 123 xyz": 9}; + y.e[""][4] "# )?, - '3' + 'o' ); - engine.eval::<()>("let y = ${a: 1, b: 2, c: 3}; y.z")?; + engine.eval::<()>("let y = #{a: 1, b: 2, c: 3}; y.z")?; Ok(()) } @@ -34,14 +37,14 @@ fn test_map_indexing() -> Result<(), EvalAltResult> { fn test_map_assign() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - let mut x = engine.eval::("let x = ${a: 1, b: true, c: \"3\"}; x")?; - let box_a = x.remove("a").unwrap(); - let box_b = x.remove("b").unwrap(); - let box_c = x.remove("c").unwrap(); + let x = engine.eval::(r#"let x = #{a: 1, b: true, "c#": "hello"}; x"#)?; + let a = x.get("a").cloned().unwrap(); + let b = x.get("b").cloned().unwrap(); + let c = x.get("c#").cloned().unwrap(); - assert_eq!(*box_a.downcast::().unwrap(), 1); - assert_eq!(*box_b.downcast::().unwrap(), true); - assert_eq!(*box_c.downcast::().unwrap(), "3"); + assert_eq!(*a.downcast::().unwrap(), 1); + assert_eq!(*b.downcast::().unwrap(), true); + assert_eq!(*c.downcast::().unwrap(), "hello"); Ok(()) } @@ -50,14 +53,14 @@ fn test_map_assign() -> Result<(), EvalAltResult> { fn test_map_return() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - let mut x = engine.eval::("${a: 1, b: true, c: \"3\"}")?; - let box_a = x.remove("a").unwrap(); - let box_b = x.remove("b").unwrap(); - let box_c = x.remove("c").unwrap(); + let x = engine.eval::(r#"#{a: 1, b: true, c: "hello"}"#)?; + let a = x.get("a").cloned().unwrap(); + let b = x.get("b").cloned().unwrap(); + let c = x.get("c").cloned().unwrap(); - assert_eq!(*box_a.downcast::().unwrap(), 1); - assert_eq!(*box_b.downcast::().unwrap(), true); - assert_eq!(*box_c.downcast::().unwrap(), "3".to_string()); + assert_eq!(*a.downcast::().unwrap(), 1); + assert_eq!(*b.downcast::().unwrap(), true); + assert_eq!(*c.downcast::().unwrap(), "hello"); Ok(()) } diff --git a/tests/types.rs b/tests/types.rs index 437c7678..658d8b3f 100644 --- a/tests/types.rs +++ b/tests/types.rs @@ -24,11 +24,11 @@ fn test_type_of() -> Result<(), EvalAltResult> { "array" ); - // #[cfg(not(feature = "no_object"))] - // assert_eq!( - // engine.eval::(r#"type_of(${a:true, "":2, "z":"hello"})"#)?, - // "map" - // ); + #[cfg(not(feature = "no_object"))] + assert_eq!( + engine.eval::(r#"type_of(#{a:true, "":2, "z":"hello"})"#)?, + "map" + ); #[cfg(not(feature = "no_object"))] {