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

Implement Object.assign( target, ...sources ) #1235

Merged
merged 2 commits into from
May 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl BuiltIn for Object {
.static_method(Self::get_prototype_of, "getPrototypeOf", 1)
.static_method(Self::define_property, "defineProperty", 3)
.static_method(Self::define_properties, "defineProperties", 2)
.static_method(Self::assign, "assign", 2)
.static_method(Self::is, "is", 2)
.static_method(
Self::get_own_property_descriptor,
Expand Down Expand Up @@ -464,6 +465,17 @@ impl Object {
Ok(object.has_own_property(key).into())
}

/// `Object.prototype.propertyIsEnumerable( property )`
///
/// This method returns a Boolean indicating whether the specified property is
/// enumerable and is the object's own property.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.prototype.propertyisenumerable
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
pub fn property_is_enumerable(
this: &Value,
args: &[Value],
Expand All @@ -481,4 +493,44 @@ impl Object {
Value::from(own_prop.enumerable())
}))
}

/// `Object.assign( target, ...sources )`
///
/// This method copies all enumerable own properties from one or more
/// source objects to a target object. It returns the target object.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.assign
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
pub fn assign(_: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
let mut to = args
.get(0)
.cloned()
.unwrap_or_default()
.to_object(context)?;

if args.len() == 1 {
Razican marked this conversation as resolved.
Show resolved Hide resolved
return Ok(to.into());
}

for source in &args[1..] {
if !source.is_null_or_undefined() {
let from = source.to_object(context).unwrap();
let keys = from.own_property_keys();
for key in keys {
if let Some(desc) = from.get_own_property(&key) {
if desc.enumerable() {
let property = from.get(&key, from.clone().into(), context)?;
to.set(key, property, to.clone().into(), context)?;
}
}
}
}
}

Ok(to.into())
}
}