Skip to content

Commit

Permalink
Has own property should call get own property (#444)
Browse files Browse the repository at this point in the history
* object.hasOwnProperty should call getOwnProperty

* should work for properties with undefined and null values

* cargo fmt
  • Loading branch information
n14little authored Jun 2, 2020
1 parent 145f0e3 commit 4f2191a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
19 changes: 13 additions & 6 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions boa/src/builtins/object/tests.rs
Original file line number Diff line number Diff line change
@@ -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"
);
}

0 comments on commit 4f2191a

Please sign in to comment.