From 724930dd8eb522c9ce445e328b179507d73e1bcd Mon Sep 17 00:00:00 2001 From: Halid Odat Date: Thu, 31 Dec 2020 23:16:26 +0100 Subject: [PATCH] Fix `Object.prototype.hasOwnProperty()` (#1004) - Fix panic when argument is not supplied. - Fix panic when `this` is not a object. - Fix Symbol property handling. --- boa/src/builtins/object/mod.rs | 21 +++++++-------------- boa/src/object/gcobject.rs | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/boa/src/builtins/object/mod.rs b/boa/src/builtins/object/mod.rs index f150257b21..c13eff66af 100644 --- a/boa/src/builtins/object/mod.rs +++ b/boa/src/builtins/object/mod.rs @@ -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 { - 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( diff --git a/boa/src/object/gcobject.rs b/boa/src/object/gcobject.rs index 7cff8de68c..28fa9e7709 100644 --- a/boa/src/object/gcobject.rs +++ b/boa/src/object/gcobject.rs @@ -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 { + pub(crate) fn ordinary_has_instance( + &self, + context: &mut Context, + value: &Value, + ) -> Result { if !self.is_callable() { return Ok(false); } @@ -755,6 +759,16 @@ impl GcObject { Ok(false) } } + + #[inline] + #[track_caller] + pub fn has_own_property(&self, key: K) -> bool + where + K: Into, + { + let key = key.into(); + self.get_own_property(&key).is_some() + } } impl AsRef> for GcObject {