Skip to content

Commit

Permalink
Support for symbols as property keys for Object.defineProperty (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeroman authored Oct 3, 2020
1 parent eb1fd28 commit 31e3174
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
14 changes: 14 additions & 0 deletions boa/src/builtins/object/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\"");
}

0 comments on commit 31e3174

Please sign in to comment.