Browse Source

Fix `Object.prototype.hasOwnProperty()` (#1004)

- Fix panic when argument is not supplied.
 - Fix panic when `this` is not a object.
 - Fix Symbol property handling.
pull/1005/head
Halid Odat 4 years ago committed by GitHub
parent
commit
724930dd8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      boa/src/builtins/object/mod.rs
  2. 16
      boa/src/object/gcobject.rs

21
boa/src/builtins/object/mod.rs

@ -379,20 +379,13 @@ impl Object {
/// [spec]: https://tc39.es/ecma262/#sec-object.prototype.hasownproperty /// [spec]: https://tc39.es/ecma262/#sec-object.prototype.hasownproperty
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/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> { pub fn has_own_property(this: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
let prop = if args.is_empty() { let key = args
None .get(0)
} else { .unwrap_or(&Value::undefined())
Some(args.get(0).expect("Cannot get object").to_string(context)?) .to_property_key(context)?;
}; let object = this.to_object(context)?;
let own_property = this
.as_object() Ok(object.has_own_property(key).into())
.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))
}
} }
pub fn property_is_enumerable( pub fn property_is_enumerable(

16
boa/src/object/gcobject.rs

@ -727,7 +727,11 @@ impl GcObject {
/// ///
/// [spec]: https://tc39.es/ecma262/#sec-ordinaryhasinstance /// [spec]: https://tc39.es/ecma262/#sec-ordinaryhasinstance
#[inline] #[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() { if !self.is_callable() {
return Ok(false); return Ok(false);
} }
@ -755,6 +759,16 @@ impl GcObject {
Ok(false) 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 { impl AsRef<GcCell<Object>> for GcObject {

Loading…
Cancel
Save