Browse Source

Make `same_value` and `same_value_zero` static methods. (#1271)

pull/1278/head
Halid Odat 4 years ago committed by GitHub
parent
commit
0dcf182ad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      boa/src/builtins/array/mod.rs
  2. 4
      boa/src/builtins/boolean/tests.rs
  3. 9
      boa/src/builtins/json/tests.rs
  4. 6
      boa/src/builtins/object/mod.rs
  5. 8
      boa/src/object/internal_methods.rs
  6. 4
      boa/src/object/mod.rs
  7. 82
      boa/src/value/equality.rs
  8. 2
      boa/src/value/hash.rs

4
boa/src/builtins/array/mod.rs

@ -20,7 +20,7 @@ use crate::{
object::{ConstructorBuilder, FunctionBuilder, GcObject, ObjectData, PROTOTYPE},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
value::{same_value_zero, IntegerOrInfinity, Value},
value::{IntegerOrInfinity, Value},
BoaProfiler, Context, Result,
};
use num_traits::*;
@ -1154,7 +1154,7 @@ impl Array {
for idx in 0..length {
let check_element = this.get_field(idx, context)?.clone();
if same_value_zero(&check_element, &search_element) {
if Value::same_value_zero(&check_element, &search_element) {
return Ok(Value::from(true));
}
}

4
boa/src/builtins/boolean/tests.rs

@ -1,4 +1,4 @@
use crate::{forward, forward_val, value::same_value, Context};
use crate::{forward, forward_val, Context, Value};
/// Test the correct type is returned from call and construct
#[allow(clippy::unwrap_used)]
@ -58,7 +58,7 @@ fn instances_have_correct_proto_set() {
let bool_instance = forward_val(&mut context, "boolInstance").expect("value expected");
let bool_prototype = forward_val(&mut context, "boolProto").expect("value expected");
assert!(same_value(
assert!(Value::same_value(
&bool_instance.as_object().unwrap().prototype_instance(),
&bool_prototype
));

9
boa/src/builtins/json/tests.rs

@ -1,4 +1,4 @@
use crate::{forward, forward_val, value::same_value, Context, Value};
use crate::{forward, forward_val, Context, Value};
#[test]
fn json_sanity() {
@ -426,10 +426,13 @@ fn json_parse_sets_prototypes() {
let global_array_prototype: Value =
context.standard_objects().array_object().prototype().into();
assert_eq!(
same_value(&object_prototype, &global_object_prototype),
Value::same_value(&object_prototype, &global_object_prototype),
true
);
assert_eq!(
Value::same_value(&array_prototype, &global_array_prototype),
true
);
assert_eq!(same_value(&array_prototype, &global_array_prototype), true);
}
#[test]

6
boa/src/builtins/object/mod.rs

@ -22,7 +22,7 @@ use crate::{
property::DataDescriptor,
property::PropertyDescriptor,
symbol::WellKnownSymbols,
value::{same_value, Type, Value},
value::{Type, Value},
BoaProfiler, Context, Result,
};
@ -261,7 +261,7 @@ impl Object {
let x = args.get(0).cloned().unwrap_or_else(Value::undefined);
let y = args.get(1).cloned().unwrap_or_else(Value::undefined);
Ok(same_value(&x, &y).into())
Ok(Value::same_value(&x, &y).into())
}
/// Get the `prototype` of an object.
@ -351,7 +351,7 @@ impl Object {
if v.is_null() {
return Ok(Value::Boolean(false));
}
if same_value(&o, &v) {
if Value::same_value(&o, &v) {
return Ok(Value::Boolean(true));
}
}

8
boa/src/object/internal_methods.rs

@ -8,7 +8,7 @@
use crate::{
object::{GcObject, Object, ObjectData},
property::{AccessorDescriptor, Attribute, DataDescriptor, PropertyDescriptor, PropertyKey},
value::{same_value, Type, Value},
value::{Type, Value},
BoaProfiler, Context, Result,
};
@ -245,7 +245,7 @@ impl GcObject {
return false;
}
if !same_value(&desc.value(), &current.value()) {
if !Value::same_value(&desc.value(), &current.value()) {
return false;
}
}
@ -468,7 +468,7 @@ impl GcObject {
pub fn set_prototype_of(&mut self, val: Value) -> bool {
debug_assert!(val.is_object() || val.is_null());
let current = self.get_prototype_of();
if same_value(&current, &val) {
if Value::same_value(&current, &val) {
return true;
}
if !self.is_extensible() {
@ -479,7 +479,7 @@ impl GcObject {
while !done {
if p.is_null() {
done = true
} else if same_value(&Value::from(self.clone()), &p) {
} else if Value::same_value(&Value::from(self.clone()), &p) {
return false;
} else {
let prototype = p

4
boa/src/object/mod.rs

@ -15,7 +15,7 @@ use crate::{
gc::{Finalize, Trace},
property::{AccessorDescriptor, Attribute, DataDescriptor, PropertyDescriptor, PropertyKey},
symbol::RcSymbol,
value::{same_value, RcBigInt, RcString, Value},
value::{RcBigInt, RcString, Value},
BoaProfiler, Context,
};
use rustc_hash::FxHashMap;
@ -534,7 +534,7 @@ impl Object {
} else {
// If target is non-extensible, [[SetPrototypeOf]] must return false
// unless V is the SameValue as the target's observed [[GetPrototypeOf]] value.
same_value(&prototype, &self.prototype)
Value::same_value(&prototype, &self.prototype)
}
}

82
boa/src/value/equality.rs

@ -28,7 +28,7 @@ impl Value {
(Self::Null, Self::Null) => true,
// 3. Return ! SameValueNonNumeric(x, y).
(_, _) => same_value_non_numeric(self, other),
(_, _) => Self::same_value_non_numeric(self, other),
}
}
@ -111,32 +111,15 @@ impl Value {
_ => false,
})
}
}
/// This function takes a string and conversts it to BigInt type.
///
/// If the result is `NaN` than `None` is returned.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-stringtobigint
pub fn string_to_bigint(string: &str) -> Option<BigInt> {
if string.is_empty() {
return Some(BigInt::from(0));
}
BigInt::from_str(string)
}
/// The internal comparison abstract operation SameValue(x, y),
/// where x and y are ECMAScript language values, produces true or false.
///
/// More information:
/// - [ECMAScript][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-samevalue
pub fn same_value(x: &Value, y: &Value) -> bool {
/// The internal comparison abstract operation SameValue(x, y),
/// where x and y are ECMAScript language values, produces true or false.
///
/// More information:
/// - [ECMAScript][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-samevalue
pub fn same_value(x: &Value, y: &Value) -> bool {
// 1. If Type(x) is different from Type(y), return false.
if x.get_type() != y.get_type() {
return false;
@ -152,20 +135,20 @@ pub fn same_value(x: &Value, y: &Value) -> bool {
(Value::Integer(x), Value::Integer(y)) => x == y,
// 3. Return ! SameValueNonNumeric(x, y).
(_, _) => same_value_non_numeric(x, y),
(_, _) => Self::same_value_non_numeric(x, y),
}
}
}
/// The internal comparison abstract operation `SameValueZero(x, y)`,
/// where `x` and `y` are ECMAScript language values, produces `true` or `false`.
///
/// `SameValueZero` differs from SameValue only in its treatment of `+0` and `-0`.
///
/// More information:
/// - [ECMAScript][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-samevaluezero
pub fn same_value_zero(x: &Value, y: &Value) -> bool {
/// The internal comparison abstract operation `SameValueZero(x, y)`,
/// where `x` and `y` are ECMAScript language values, produces `true` or `false`.
///
/// `SameValueZero` differs from SameValue only in its treatment of `+0` and `-0`.
///
/// More information:
/// - [ECMAScript][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-samevaluezero
pub fn same_value_zero(x: &Value, y: &Value) -> bool {
if x.get_type() != y.get_type() {
return false;
}
@ -181,11 +164,11 @@ pub fn same_value_zero(x: &Value, y: &Value) -> bool {
(Value::Integer(x), Value::Integer(y)) => x == y,
// 3. Return ! SameValueNonNumeric(x, y).
(_, _) => same_value_non_numeric(x, y),
(_, _) => Self::same_value_non_numeric(x, y),
}
}
}
fn same_value_non_numeric(x: &Value, y: &Value) -> bool {
fn same_value_non_numeric(x: &Value, y: &Value) -> bool {
debug_assert!(x.get_type() == y.get_type());
match (x, y) {
(Value::Null, Value::Null) | (Value::Undefined, Value::Undefined) => true,
@ -195,4 +178,21 @@ fn same_value_non_numeric(x: &Value, y: &Value) -> bool {
(Value::Symbol(ref x), Value::Symbol(ref y)) => x == y,
_ => false,
}
}
}
/// This function takes a string and conversts it to BigInt type.
///
/// If the result is `NaN` than `None` is returned.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-stringtobigint
pub fn string_to_bigint(string: &str) -> Option<BigInt> {
if string.is_empty() {
return Some(BigInt::from(0));
}
BigInt::from_str(string)
}

2
boa/src/value/hash.rs

@ -5,7 +5,7 @@ use std::hash::{Hash, Hasher};
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
same_value_zero(self, other)
Self::same_value_zero(self, other)
}
}

Loading…
Cancel
Save