Skip to content

Commit

Permalink
Merge b23bb5b into 26b6223
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored May 8, 2021
2 parents 26b6223 + b23bb5b commit b667a76
Showing 1 changed file with 52 additions and 0 deletions.
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 {
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())
}
}

0 comments on commit b667a76

Please sign in to comment.