Browse Source

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
pull/455/head
n14little 4 years ago committed by GitHub
parent
commit
4f2191ae1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      boa/src/builtins/object/mod.rs
  2. 22
      boa/src/builtins/object/tests.rs

19
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.

22
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"
);
}
Loading…
Cancel
Save