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 lossless TryFromJs for integers from f64 #3907

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Changes from 1 commit
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
126 changes: 126 additions & 0 deletions core/engine/src/value/conversions/try_from_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,47 @@ impl TryFromJs for f64 {
}
}

trait ToF64: Sized {
jedel1043 marked this conversation as resolved.
Show resolved Hide resolved
fn cast_to_f64(self) -> f64;
fn cast_from_f64(v: f64) -> Self;
}

macro_rules! impl_to_f64 {
($type_:ident) => {
impl ToF64 for $type_ {
#[inline]
// NOTE: Lint only applies to types that are lossless conversion to f64.
#[allow(clippy::cast_lossless)]
fn cast_to_f64(self) -> f64 {
self as f64
}
#[inline]
fn cast_from_f64(v: f64) -> Self {
v as Self
}
}
};
}

impl_to_f64!(i8);
impl_to_f64!(u8);
impl_to_f64!(i16);
impl_to_f64!(u16);
impl_to_f64!(i32);
impl_to_f64!(u32);
impl_to_f64!(i64);
impl_to_f64!(u64);
impl_to_f64!(usize);
impl_to_f64!(i128);
impl_to_f64!(u128);

fn from_f64<T: ToF64>(v: f64) -> Option<T> {
if T::cast_from_f64(v).cast_to_f64().to_bits() == v.to_bits() {
return Some(T::cast_from_f64(v));
}
None
}

impl TryFromJs for i8 {
fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
match value {
Expand All @@ -164,6 +205,11 @@ impl TryFromJs for i8 {
.with_message(format!("cannot convert value to a i8: {e}"))
.into()
}),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a i8")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a i8")
.into()),
Expand All @@ -179,6 +225,11 @@ impl TryFromJs for u8 {
.with_message(format!("cannot convert value to a u8: {e}"))
.into()
}),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a u8")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a u8")
.into()),
Expand All @@ -194,6 +245,11 @@ impl TryFromJs for i16 {
.with_message(format!("cannot convert value to a i16: {e}"))
.into()
}),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a i16")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a i16")
.into()),
Expand All @@ -209,6 +265,11 @@ impl TryFromJs for u16 {
.with_message(format!("cannot convert value to a iu16: {e}"))
.into()
}),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a u16")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a u16")
.into()),
Expand All @@ -220,6 +281,11 @@ impl TryFromJs for i32 {
fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
match value {
JsValue::Integer(i) => Ok(*i),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a i32")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a i32")
.into()),
Expand All @@ -235,6 +301,11 @@ impl TryFromJs for u32 {
.with_message(format!("cannot convert value to a u32: {e}"))
.into()
}),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a u32")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a u32")
.into()),
Expand All @@ -246,6 +317,11 @@ impl TryFromJs for i64 {
fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
match value {
JsValue::Integer(i) => Ok((*i).into()),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a i64")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a i64")
.into()),
Expand All @@ -261,6 +337,11 @@ impl TryFromJs for u64 {
.with_message(format!("cannot convert value to a u64: {e}"))
.into()
}),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a u64")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a u64")
.into()),
Expand All @@ -276,6 +357,11 @@ impl TryFromJs for usize {
.with_message(format!("cannot convert value to a usize: {e}"))
.into()
}),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a usize")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a usize")
.into()),
Expand All @@ -287,6 +373,11 @@ impl TryFromJs for i128 {
fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
match value {
JsValue::Integer(i) => Ok((*i).into()),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a i128")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a i128")
.into()),
Expand All @@ -302,13 +393,48 @@ impl TryFromJs for u128 {
.with_message(format!("cannot convert value to a u128: {e}"))
.into()
}),
JsValue::Rational(f) => from_f64(*f).ok_or_else(|| {
JsNativeError::typ()
.with_message("cannot convert value to a u128")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("cannot convert value to a u128")
.into()),
}
}
}

#[test]
fn integer_floating_js_value_to_integer() {
let context = &mut Context::default();

assert_eq!(i8::try_from_js(&JsValue::from(4.0), context), Ok(4));
assert_eq!(u8::try_from_js(&JsValue::from(4.0), context), Ok(4));
assert_eq!(i16::try_from_js(&JsValue::from(4.0), context), Ok(4));
assert_eq!(u16::try_from_js(&JsValue::from(4.0), context), Ok(4));
assert_eq!(i32::try_from_js(&JsValue::from(4.0), context), Ok(4));
assert_eq!(u32::try_from_js(&JsValue::from(4.0), context), Ok(4));
assert_eq!(i64::try_from_js(&JsValue::from(4.0), context), Ok(4));
assert_eq!(u64::try_from_js(&JsValue::from(4.0), context), Ok(4));

// Floating with fractional part
let result = i32::try_from_js(&JsValue::from(4.000_000_000_000_001), context);
assert!(result.is_err());

// NaN
let result = i32::try_from_js(&JsValue::nan(), context);
assert!(result.is_err());

// +Infinity
let result = i32::try_from_js(&JsValue::positive_infinity(), context);
assert!(result.is_err());

// -Infinity
let result = i32::try_from_js(&JsValue::negative_infinity(), context);
assert!(result.is_err());
}

#[test]
fn value_into_vec() {
use boa_engine::{run_test_actions, TestAction};
Expand Down
Loading