Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for symbols as property keys for Object.defineProperty #770

Merged
merged 3 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use crate::{
builtins::function::{make_builtin_fn, make_constructor_fn},
object::{Object as BuiltinObject, ObjectData},
property::Property,
property::{Property, PropertyKey},
value::{same_value, Value},
BoaProfiler, Context, Result,
};
Expand Down Expand Up @@ -96,9 +96,9 @@ impl Object {
}

/// Define a property in an object
pub fn define_property(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> {
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 = PropertyKey::from(args.get(1).expect("Cannot get object"));
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\"");
}
11 changes: 11 additions & 0 deletions boa/src/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ pub enum PropertyKey {
Index(u32),
}

impl From<&Value> for PropertyKey {
#[inline]
fn from(value: &Value) -> PropertyKey {
match value {
Value::String(ref string) => string.clone().into(),
Value::Symbol(ref symbol) => symbol.clone().into(),
_ => panic!("Cannot be used as property"),
}
}
}
HalidOdat marked this conversation as resolved.
Show resolved Hide resolved

impl From<RcString> for PropertyKey {
#[inline]
fn from(string: RcString) -> PropertyKey {
Expand Down