Skip to content

Commit

Permalink
Fix Object.prototype.hasOwnProperty()
Browse files Browse the repository at this point in the history
 - Fix panic when argument is not supplied.
 - Fix panic when `this` is not a object.
 - Fix Symbol property handling.
  • Loading branch information
HalidOdat committed Dec 29, 2020
1 parent 6f3641d commit e449546
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
21 changes: 7 additions & 14 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,13 @@ impl Object {
/// [spec]: https://tc39.es/ecma262/#sec-object.prototype.hasownproperty
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
pub fn has_own_property(this: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
let prop = if args.is_empty() {
None
} else {
Some(args.get(0).expect("Cannot get object").to_string(context)?)
};
let own_property = this
.as_object()
.expect("Cannot get THIS object")
.get_own_property(&prop.expect("cannot get prop").into());
if own_property.is_none() {
Ok(Value::from(false))
} else {
Ok(Value::from(true))
}
let key = args
.get(0)
.unwrap_or(&Value::undefined())
.to_property_key(context)?;
let object = this.to_object(context)?;

Ok(object.has_own_property(key).into())
}

pub fn property_is_enumerable(
Expand Down
16 changes: 15 additions & 1 deletion boa/src/object/gcobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,11 @@ impl GcObject {
///
/// [spec]: https://tc39.es/ecma262/#sec-ordinaryhasinstance
#[inline]
pub fn ordinary_has_instance(&self, context: &mut Context, value: &Value) -> Result<bool> {
pub(crate) fn ordinary_has_instance(
&self,
context: &mut Context,
value: &Value,
) -> Result<bool> {
if !self.is_callable() {
return Ok(false);
}
Expand Down Expand Up @@ -755,6 +759,16 @@ impl GcObject {
Ok(false)
}
}

#[inline]
#[track_caller]
pub fn has_own_property<K>(&self, key: K) -> bool
where
K: Into<PropertyKey>,
{
let key = key.into();
self.get_own_property(&key).is_some()
}
}

impl AsRef<GcCell<Object>> for GcObject {
Expand Down

0 comments on commit e449546

Please sign in to comment.