Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Object.is() method issue #513 #515

Merged
merged 4 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use std::{
};

use super::function::{make_builtin_fn, make_constructor_fn};
use crate::builtins::value::same_value;
use crate::builtins::Number;
pub use internal_state::{InternalState, InternalStateCell};

pub mod internal_methods;
Expand Down Expand Up @@ -406,6 +408,28 @@ pub fn make_object(_: &mut Value, args: &[Value], ctx: &mut Interpreter) -> Resu
Ok(object)
}

/// Uses the SameValue algorithm to check equality of objects
pub fn is(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
let x = args.get(0).expect("Cannot get object").clone();
let y = args.get(1).expect("Cannot get object").clone();
HalidOdat marked this conversation as resolved.
Show resolved Hide resolved

if x.get_type() != y.get_type() {
return Ok(Value::boolean(false));
}
HalidOdat marked this conversation as resolved.
Show resolved Hide resolved

let result = match (x.data(), y.data()) {
(ValueData::BigInt(x), ValueData::BigInt(y)) => BigInt::same_value(x, y),
(ValueData::Rational(x), ValueData::Rational(y)) => Number::same_value(*x, *y),
(ValueData::Rational(x), ValueData::Integer(y)) => Number::same_value(*x, f64::from(*y)),
(ValueData::Integer(x), ValueData::Rational(y)) => Number::same_value(f64::from(*x), *y),
(ValueData::Integer(x), ValueData::Integer(y)) => x == y,

(_, _) => same_value(&x, &y, false),
};

Ok(Value::boolean(result))
HalidOdat marked this conversation as resolved.
Show resolved Hide resolved
}

/// Get the `prototype` of an object.
pub fn get_prototype_of(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
let obj = args.get(0).expect("Cannot get object");
Expand Down Expand Up @@ -511,6 +535,7 @@ pub fn create(global: &Value) -> Value {
make_builtin_fn(set_prototype_of, "setPrototypeOf", &object, 2);
make_builtin_fn(get_prototype_of, "getPrototypeOf", &object, 1);
make_builtin_fn(define_property, "defineProperty", &object, 3);
make_builtin_fn(is, "is", &object, 2);

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
@@ -1,5 +1,27 @@
use crate::{exec::Interpreter, forward, realm::Realm};

#[test]
fn object_is() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let init = r#"
var foo = { a: 1};
var bar = { a: 1};
"#;

forward(&mut engine, init);

assert_eq!(forward(&mut engine, "Object.is('foo', 'foo')"), "true");
assert_eq!(forward(&mut engine, "Object.is('foo', 'bar')"), "false");
assert_eq!(forward(&mut engine, "Object.is([], [])"), "false");
assert_eq!(forward(&mut engine, "Object.is(foo, foo)"), "true");
assert_eq!(forward(&mut engine, "Object.is(foo, bar)"), "false");
assert_eq!(forward(&mut engine, "Object.is(null, null)"), "true");
assert_eq!(forward(&mut engine, "Object.is(0, -0)"), "false");
assert_eq!(forward(&mut engine, "Object.is(-0, -0)"), "true");
assert_eq!(forward(&mut engine, "Object.is(NaN, 0/0)"), "true");
}
#[test]
fn object_has_own_property() {
let realm = Realm::create();
Expand Down