From 4f2191ae1f82b95dd5ffe6a5cfc6d2dcaaada6c5 Mon Sep 17 00:00:00 2001 From: n14little Date: Tue, 2 Jun 2020 06:45:10 -0500 Subject: [PATCH] Has own property should call get own property (#444) * object.hasOwnProperty should call getOwnProperty * should work for properties with undefined and null values * cargo fmt --- boa/src/builtins/object/mod.rs | 19 +++++++++++++------ boa/src/builtins/object/tests.rs | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 boa/src/builtins/object/tests.rs diff --git a/boa/src/builtins/object/mod.rs b/boa/src/builtins/object/mod.rs index 57763a4ab7..9586509b2c 100644 --- a/boa/src/builtins/object/mod.rs +++ b/boa/src/builtins/object/mod.rs @@ -36,6 +36,9 @@ pub use internal_state::{InternalState, InternalStateCell}; pub mod internal_methods_trait; mod internal_state; +#[cfg(test)] +mod tests; + /// Static `prototype`, usually set on constructors as a key to point to their respective prototype object. pub static PROTOTYPE: &str = "prototype"; @@ -605,12 +608,16 @@ pub fn has_own_property(this: &mut Value, args: &[Value], ctx: &mut Interpreter) } else { Some(ctx.to_string(args.get(0).expect("Cannot get object"))?) }; - Ok(Value::from( - prop.is_some() - && this - .get_property(&prop.expect("Cannot get object")) - .is_some(), - )) + let own_property = this + .as_object() + .as_deref() + .expect("Cannot get THIS object") + .get_own_property(&Value::string(&prop.expect("cannot get prop"))); + if own_property.is_none() { + Ok(Value::from(false)) + } else { + Ok(Value::from(true)) + } } /// Create a new `Object` object. diff --git a/boa/src/builtins/object/tests.rs b/boa/src/builtins/object/tests.rs new file mode 100644 index 0000000000..a699e7b290 --- /dev/null +++ b/boa/src/builtins/object/tests.rs @@ -0,0 +1,22 @@ +use crate::{exec::Interpreter, forward, realm::Realm}; + +#[test] +fn object_has_own_property() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + let init = r#" + let x = { someProp: 1, undefinedProp: undefined, nullProp: null }; + "#; + + eprintln!("{}", forward(&mut engine, init)); + assert_eq!(forward(&mut engine, "x.hasOwnProperty('someProp')"), "true"); + assert_eq!( + forward(&mut engine, "x.hasOwnProperty('undefinedProp')"), + "true" + ); + assert_eq!(forward(&mut engine, "x.hasOwnProperty('nullProp')"), "true"); + assert_eq!( + forward(&mut engine, "x.hasOwnProperty('hasOwnProperty')"), + "false" + ); +}