boa icon indicating copy to clipboard operation
boa copied to clipboard

Add From<i/u128> for JsBigInt

Open dranikpg opened this issue 3 years ago • 5 comments

This Pull Request fixes 1970.

It changes the following:

  • Added From, From for JsBigInt
  • Added From, From for Numeric

dranikpg avatar Mar 23 '22 13:03 dranikpg

Should i128 really be clamped to f64 like i64 is?

impl From<i64> for JsValue {
    #[inline]
    fn from(value: i64) -> Self {
        if let Ok(value) = i32::try_from(value) {
            Self::Integer(value)
        } else {
            Self::Rational(value as f64)
        }
    }
}

I'd rather expect something like this, as accuracy would be very low for big values otherwise

impl From<i128> for JsValue {
    #[inline]
    fn from(value: i128) -> Self {
        if let Ok(value) = i32::try_from(value) {
            Self::Integer(value)
        } else {
            Self::BigInt(value.into()) // i.e. JsBigInt::from()
        }
    }    
}

dranikpg avatar Mar 23 '22 13:03 dranikpg

Codecov Report

Merging #1971 (5d9154e) into main (e2630fa) will decrease coverage by 0.03%. The diff coverage is 0.00%.

@@            Coverage Diff             @@
##             main    #1971      +/-   ##
==========================================
- Coverage   45.90%   45.87%   -0.04%     
==========================================
  Files         206      206              
  Lines       17116    17124       +8     
==========================================
- Hits         7857     7855       -2     
- Misses       9259     9269      +10     
Impacted Files Coverage Δ
boa_engine/src/bigint.rs 35.03% <0.00%> (-4.07%) :arrow_down:
boa_engine/src/value/mod.rs 51.09% <0.00%> (-0.76%) :arrow_down:
boa_engine/src/value/operations.rs 38.85% <0.00%> (-0.32%) :arrow_down:
boa_engine/src/builtins/string/mod.rs 62.74% <0.00%> (+0.21%) :arrow_up:
...ine/src/syntax/parser/expression/assignment/mod.rs 30.84% <0.00%> (+1.86%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update e2630fa...5d9154e. Read the comment docs.

codecov[bot] avatar Mar 23 '22 14:03 codecov[bot]

Should i128 really be clamped to f64 like i64 is?


impl From<i64> for JsValue {

    #[inline]

    fn from(value: i64) -> Self {

        if let Ok(value) = i32::try_from(value) {

            Self::Integer(value)

        } else {

            Self::Rational(value as f64)

        }

    }

}

I'd rather expect something like this, as accuracy would be very low for big values otherwise


impl From<i128> for JsValue {

    #[inline]

    fn from(value: i128) -> Self {

        if let Ok(value) = i32::try_from(value) {

            Self::Integer(value)

        } else {

            Self::BigInt(value.into()) // i.e. JsBigInt::from()

        }

    }    

}

I would say this can be treated in a separate issue, it's a subjective topic :)

Razican avatar Mar 24 '22 06:03 Razican

Don't we want to implement the From<i128> or From<u128> trait for JsValue? Why aren't we adding an implementation in here: https://github.com/boa-dev/boa/blob/main/boa_engine/src/value/conversions.rs?

I would like to be able to do the following:

pub fn into_js_value_i128(number: i128) -> boa_engine::JsValue {
    number.into()
}

pub fn into_js_value_u128(number: u128) -> boa_engine::JsValue {
    number.into()
}

I've noticed that f32, i128, i16, i8, u128, u16, and u8 are all missing implementations for From for JsValue, and if I'm not mistaken that seems like the most ergonomic way to use .into() or JsValue::from().

lastmjs avatar Mar 30 '22 14:03 lastmjs

What can we do to move this forward? I am currently in need of this now and it would be nice to get back on the main branch of Boa.

lastmjs avatar May 20 '22 13:05 lastmjs

This will be handled as part of #2276.

Razican avatar Oct 10 '22 12:10 Razican