Skip to content

Commit

Permalink
Modify JsValue to be more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed May 28, 2022
1 parent 6e1ef58 commit 9e63a28
Show file tree
Hide file tree
Showing 15 changed files with 496 additions and 421 deletions.
6 changes: 3 additions & 3 deletions boa_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "../README.md"
[features]
profiler = ["boa_profiler/profiler"]
deser = ["boa_interner/serde"]
nan_boxing = []
nan_boxing = ["dep:sptr", "dep:num_enum"]

# Enable Boa's WHATWG console object implementation.
console = []
Expand Down Expand Up @@ -43,9 +43,9 @@ dyn-clone = "1.0.5"
once_cell = "1.12.0"
tap = "1.0.1"
icu = "0.6.0"
sptr = "0.3.1"
cfg-if = "1.0.0"
num_enum = "0.5.7"
sptr = { version = "0.3.1", optional = true }
num_enum = { version = "0.5.7", optional = true }

[dev-dependencies]
criterion = "0.3.5"
Expand Down
6 changes: 3 additions & 3 deletions boa_engine/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl Map {
// 3. Let entries be the List that is M.[[MapData]].
if let Some(map) = object.borrow_mut().as_map_mut() {
let key = match key.variant() {
JsVariant::Rational(r) => {
JsVariant::Float64(r) => {
// 5. If key is -0𝔽, set key to +0𝔽.
if r.is_zero() {
JsValue::new(0f64)
Expand Down Expand Up @@ -329,7 +329,7 @@ impl Map {
) -> JsResult<JsValue> {
let key = args.get_or_undefined(0);
let key = match key.variant() {
JsVariant::Rational(r) if r.is_zero() => JsValue::new(0f64),
JsVariant::Float64(r) if r.is_zero() => JsValue::new(0f64),
_ => key.clone(),
};

Expand Down Expand Up @@ -393,7 +393,7 @@ impl Map {
) -> JsResult<JsValue> {
let key = args.get_or_undefined(0);
let key = match key.variant() {
JsVariant::Rational(r) if r.is_zero() => JsValue::new(0f64),
JsVariant::Float64(r) if r.is_zero() => JsValue::new(0f64),
_ => key.clone(),
};

Expand Down
12 changes: 6 additions & 6 deletions boa_engine/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,8 @@ impl Number {
) -> JsResult<JsValue> {
Ok(JsValue::new(if let Some(val) = args.get(0) {
match val.variant() {
JsVariant::Rational(number) => number.is_finite(),
JsVariant::Integer(_) => true,
JsVariant::Float64(number) => number.is_finite(),
JsVariant::Integer32(_) => true,
_ => false,
}
} else {
Expand Down Expand Up @@ -1066,8 +1066,8 @@ impl Number {
_ctx: &mut Context,
) -> JsResult<JsValue> {
Ok(JsValue::new(match args.get(0).map(JsValue::variant) {
Some(JsVariant::Integer(_)) => true,
Some(JsVariant::Rational(number)) if Self::is_float_integer(number) => {
Some(JsVariant::Integer32(_)) => true,
Some(JsVariant::Float64(number)) if Self::is_float_integer(number) => {
number.abs() <= Self::MAX_SAFE_INTEGER
}
_ => false,
Expand All @@ -1083,8 +1083,8 @@ impl Number {
#[inline]
pub(crate) fn is_integer(val: &JsValue) -> bool {
match val.variant() {
JsVariant::Integer(_) => true,
JsVariant::Rational(number) => Self::is_float_integer(number),
JsVariant::Integer32(_) => true,
JsVariant::Float64(number) => Self::is_float_integer(number),
_ => false,
}
}
Expand Down
5 changes: 4 additions & 1 deletion boa_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! - **serde** - Enables serialization and deserialization of the AST (Abstract Syntax Tree).
//! - **console** - Enables `boa`s WHATWG `console` object implementation.
//! - **profiler** - Enables profiling with measureme (this is mostly internal).
//! - **nan_boxing** - Enables `boa`'s nan-boxed [`JsValue`] implementation
//! (Only available on x86_64 platforms, does nothing on incompatible platforms).
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",
Expand Down Expand Up @@ -68,6 +70,8 @@
rustdoc::missing_doc_code_examples
)]

pub mod value; // In the top to give priority to `JsValue` docs

pub mod bigint;
pub mod builtins;
pub mod bytecompiler;
Expand All @@ -80,7 +84,6 @@ pub mod realm;
pub mod string;
pub mod symbol;
pub mod syntax;
pub mod value;
pub mod vm;

#[cfg(test)]
Expand Down
30 changes: 15 additions & 15 deletions boa_engine/src/value/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl From<f32> for JsValue {
// if value as i32 as f64 == value {
// Self::Integer(value as i32)
// } else {
Self::rational(value.into())
Self::float64(value.into())
// }
}
}
Expand All @@ -56,54 +56,54 @@ impl From<f64> for JsValue {
// if value as i32 as f64 == value {
// Self::Integer(value as i32)
// } else {
Self::rational(value)
Self::float64(value)
// }
}
}

impl From<u8> for JsValue {
#[inline]
fn from(value: u8) -> Self {
Self::integer(value.into())
Self::integer32(value.into())
}
}

impl From<i8> for JsValue {
#[inline]
fn from(value: i8) -> Self {
Self::integer(value.into())
Self::integer32(value.into())
}
}

impl From<u16> for JsValue {
#[inline]
fn from(value: u16) -> Self {
Self::integer(value.into())
Self::integer32(value.into())
}
}

impl From<i16> for JsValue {
#[inline]
fn from(value: i16) -> Self {
Self::integer(value.into())
Self::integer32(value.into())
}
}

impl From<u32> for JsValue {
#[inline]
fn from(value: u32) -> Self {
if let Ok(integer) = i32::try_from(value) {
Self::integer(integer)
Self::integer32(integer)
} else {
Self::rational(value.into())
Self::float64(value.into())
}
}
}

impl From<i32> for JsValue {
#[inline]
fn from(value: i32) -> Self {
Self::integer(value)
Self::integer32(value)
}
}

Expand All @@ -118,9 +118,9 @@ impl From<usize> for JsValue {
#[inline]
fn from(value: usize) -> Self {
if let Ok(value) = i32::try_from(value) {
Self::integer(value)
Self::integer32(value)
} else {
Self::rational(value as f64)
Self::float64(value as f64)
}
}
}
Expand All @@ -129,9 +129,9 @@ impl From<u64> for JsValue {
#[inline]
fn from(value: u64) -> Self {
if let Ok(value) = i32::try_from(value) {
Self::integer(value)
Self::integer32(value)
} else {
Self::rational(value as f64)
Self::float64(value as f64)
}
}
}
Expand All @@ -140,9 +140,9 @@ impl From<i64> for JsValue {
#[inline]
fn from(value: i64) -> Self {
if let Ok(value) = i32::try_from(value) {
Self::integer(value)
Self::integer32(value)
} else {
Self::rational(value as f64)
Self::float64(value as f64)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/value/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,11 @@ impl Display for ValueDisplay<'_> {
None => write!(f, "Symbol()"),
},
JsVariant::String(v) => write!(f, "\"{}\"", *v),
JsVariant::Rational(v) => format_rational(v, f),
JsVariant::Float64(v) => format_rational(v, f),
JsVariant::Object(_) => {
write!(f, "{}", log_string_from(self.value, self.internals, true))
}
JsVariant::Integer(v) => write!(f, "{v}"),
JsVariant::Integer32(v) => write!(f, "{v}"),
JsVariant::BigInt(num) => write!(f, "{}n", *num),
}
}
Expand Down
44 changes: 22 additions & 22 deletions boa_engine/src/value/equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ impl JsValue {
// 2. If Type(x) is Number or BigInt, then
// a. Return ! Type(x)::equal(x, y).
(JsVariant::BigInt(ref x), JsVariant::BigInt(ref y)) => JsBigInt::equal(x, y),
(JsVariant::Rational(x), JsVariant::Rational(y)) => Number::equal(x, y),
(JsVariant::Rational(x), JsVariant::Integer(y)) => Number::equal(x, f64::from(y)),
(JsVariant::Integer(x), JsVariant::Rational(y)) => Number::equal(f64::from(x), y),
(JsVariant::Integer(x), JsVariant::Integer(y)) => x == y,
(JsVariant::Float64(x), JsVariant::Float64(y)) => Number::equal(x, y),
(JsVariant::Float64(x), JsVariant::Integer32(y)) => Number::equal(x, f64::from(y)),
(JsVariant::Integer32(x), JsVariant::Float64(y)) => Number::equal(f64::from(x), y),
(JsVariant::Integer32(x), JsVariant::Integer32(y)) => x == y,

//Null has to be handled specially because "typeof null" returns object and if we managed
//this without a special case we would compare self and other as if they were actually
Expand Down Expand Up @@ -56,10 +56,10 @@ impl JsValue {
//
// https://github.com/rust-lang/rust/issues/54883
(
JsVariant::Integer(_) | JsVariant::Rational(_),
JsVariant::Integer32(_) | JsVariant::Float64(_),
JsVariant::String(_) | JsVariant::Boolean(_),
)
| (JsVariant::String(_), JsVariant::Integer(_) | JsVariant::Rational(_)) => {
| (JsVariant::String(_), JsVariant::Integer32(_) | JsVariant::Float64(_)) => {
let x = self.to_number(context)?;
let y = other.to_number(context)?;
Number::equal(x, y)
Expand Down Expand Up @@ -91,8 +91,8 @@ impl JsValue {
(
JsVariant::Object(_),
JsVariant::String(_)
| JsVariant::Rational(_)
| JsVariant::Integer(_)
| JsVariant::Float64(_)
| JsVariant::Integer32(_)
| JsVariant::BigInt(_)
| JsVariant::Symbol(_),
) => {
Expand All @@ -106,8 +106,8 @@ impl JsValue {
// of the comparison ? ToPrimitive(x) == y.
(
JsVariant::String(_)
| JsVariant::Rational(_)
| JsVariant::Integer(_)
| JsVariant::Float64(_)
| JsVariant::Integer32(_)
| JsVariant::BigInt(_)
| JsVariant::Symbol(_),
JsVariant::Object(_),
Expand All @@ -121,10 +121,10 @@ impl JsValue {
// 12. If Type(x) is BigInt and Type(y) is Number, or if Type(x) is Number and Type(y) is BigInt, then
// a. If x or y are any of NaN, +∞, or -∞, return false.
// b. If the mathematical value of x is equal to the mathematical value of y, return true; otherwise return false.
(JsVariant::BigInt(a), JsVariant::Rational(b)) => *a == b,
(JsVariant::Rational(a), JsVariant::BigInt(b)) => a == *b,
(JsVariant::BigInt(a), JsVariant::Integer(b)) => *a == b,
(JsVariant::Integer(a), JsVariant::BigInt(b)) => a == *b,
(JsVariant::BigInt(a), JsVariant::Float64(b)) => *a == b,
(JsVariant::Float64(a), JsVariant::BigInt(b)) => a == *b,
(JsVariant::BigInt(a), JsVariant::Integer32(b)) => *a == b,
(JsVariant::Integer32(a), JsVariant::BigInt(b)) => a == *b,

// 13. Return false.
_ => false,
Expand All @@ -148,10 +148,10 @@ impl JsValue {
// 2. If Type(x) is Number or BigInt, then
// a. Return ! Type(x)::SameValue(x, y).
(JsVariant::BigInt(ref x), JsVariant::BigInt(ref y)) => JsBigInt::same_value(x, y),
(JsVariant::Rational(x), JsVariant::Rational(y)) => Number::same_value(x, y),
(JsVariant::Rational(x), JsVariant::Integer(y)) => Number::same_value(x, f64::from(y)),
(JsVariant::Integer(x), JsVariant::Rational(y)) => Number::same_value(f64::from(x), y),
(JsVariant::Integer(x), JsVariant::Integer(y)) => x == y,
(JsVariant::Float64(x), JsVariant::Float64(y)) => Number::same_value(x, y),
(JsVariant::Float64(x), JsVariant::Integer32(y)) => Number::same_value(x, f64::from(y)),
(JsVariant::Integer32(x), JsVariant::Float64(y)) => Number::same_value(f64::from(x), y),
(JsVariant::Integer32(x), JsVariant::Integer32(y)) => x == y,

// 3. Return ! SameValueNonNumeric(x, y).
(_, _) => Self::same_value_non_numeric(x, y),
Expand All @@ -177,14 +177,14 @@ impl JsValue {
// a. Return ! Type(x)::SameValueZero(x, y).
(JsVariant::BigInt(ref x), JsVariant::BigInt(ref y)) => JsBigInt::same_value_zero(x, y),

(JsVariant::Rational(x), JsVariant::Rational(y)) => Number::same_value_zero(x, y),
(JsVariant::Rational(x), JsVariant::Integer(y)) => {
(JsVariant::Float64(x), JsVariant::Float64(y)) => Number::same_value_zero(x, y),
(JsVariant::Float64(x), JsVariant::Integer32(y)) => {
Number::same_value_zero(x, f64::from(y))
}
(JsVariant::Integer(x), JsVariant::Rational(y)) => {
(JsVariant::Integer32(x), JsVariant::Float64(y)) => {
Number::same_value_zero(f64::from(x), y)
}
(JsVariant::Integer(x), JsVariant::Integer(y)) => x == y,
(JsVariant::Integer32(x), JsVariant::Integer32(y)) => x == y,

// 3. Return ! SameValueNonNumeric(x, y).
(_, _) => Self::same_value_non_numeric(x, y),
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/value/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl Hash for JsValue {
JsVariant::Null => NullHashable.hash(state),
JsVariant::String(string) => string.hash(state),
JsVariant::Boolean(boolean) => boolean.hash(state),
JsVariant::Integer(integer) => RationalHashable(f64::from(integer)).hash(state),
JsVariant::Integer32(integer) => RationalHashable(f64::from(integer)).hash(state),
JsVariant::BigInt(bigint) => bigint.hash(state),
JsVariant::Rational(rational) => RationalHashable(rational).hash(state),
JsVariant::Float64(rational) => RationalHashable(rational).hash(state),
JsVariant::Symbol(symbol) => Hash::hash(&*symbol, state),
JsVariant::Object(object) => std::ptr::hash(object.as_ref(), state),
}
Expand Down
Loading

0 comments on commit 9e63a28

Please sign in to comment.