Browse Source

Support for symbols as property keys for `Object.defineProperty` (#770)

pull/787/head
George Roman 4 years ago committed by GitHub
parent
commit
31e3174ca9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      boa/src/builtins/object/mod.rs
  2. 14
      boa/src/builtins/object/tests.rs

5
boa/src/builtins/object/mod.rs

@ -131,7 +131,10 @@ impl Object {
/// Define a property in an object
pub fn define_property(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> {
let obj = args.get(0).expect("Cannot get object");
let prop = args.get(1).expect("Cannot get object").to_string(ctx)?;
let prop = args
.get(1)
.expect("Cannot get object")
.to_property_key(ctx)?;
let desc = Property::from(args.get(2).expect("Cannot get object"));
obj.set_property(prop, desc);
Ok(Value::undefined())

14
boa/src/builtins/object/tests.rs

@ -177,3 +177,17 @@ fn object_to_string() {
assert_eq!(forward(&mut ctx, "re.toString()"), "\"[object RegExp]\"");
assert_eq!(forward(&mut ctx, "o.toString()"), "\"[object Object]\"");
}
#[test]
fn define_symbol_property() {
let mut ctx = Context::new();
let init = r#"
let obj = {};
let sym = Symbol("key");
Object.defineProperty(obj, sym, { value: "val" });
"#;
eprintln!("{}", forward(&mut ctx, init));
assert_eq!(forward(&mut ctx, "obj[sym]"), "\"val\"");
}

Loading…
Cancel
Save