-
-
Notifications
You must be signed in to change notification settings - Fork 402
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
Add From<i/u128> for JsBigInt #1971
Conversation
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()
}
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the contribution!! Looks great :)
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
I would say this can be treated in a separate issue, it's a subjective topic :) |
impl From<i128> for Numeric { | ||
#[inline] | ||
fn from(value: i128) -> Self { | ||
Self::BigInt(value.into()) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a number (f64
) instead of BigInt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we are already doing this with i64
, and BigInt
would make sure that it doesn't lose precision:
boa/boa_engine/src/value/mod.rs
Lines 1003 to 1008 in e2630fa
impl From<i64> for Numeric { | |
#[inline] | |
fn from(value: i64) -> Self { | |
Self::BigInt(value.into()) | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we are already doing this with i64
Ummm.... This is wrong, It should be a number, not bigint.
BigInt would make sure that it doesn't lose precision
It's fine if we lose precision (we lose precision if we type a number value which is too big for f64
in JavaScript, same with From<i64>
for JsValue
), If we don't want loss of precision we create a bigint and we can convert it into Numeric
.
The behaviour of From<u/iN>
should be uniform, to do some number and others bigint is confuzing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was some discussion here too: #1961
From my point of view, we should be able to convert an f64
to JsValue
or Numeric
, and a BigInt
too. But nothing else (at most an i32). If the user wants to convert a i128
to a BigInt
, they can first convert it to a BigInt
and then convert the BigInt
to a Numeric
or a JsValue
.
We shouldn't probably force anything, but give some options. Maybe a TryFrom
implementation could be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my point of view, we should be able to convert an
f64
toJsValue
orNumeric
, and aBigInt
too. But nothing else (at most an i32). If the user wants to convert ai128
to aBigInt
, they can first convert it to aBigInt
and then convert theBigInt
to aNumeric
or aJsValue
.We shouldn't probably force anything, but give some options. Maybe a
TryFrom
implementation could be nice.
Yup. agreed
impl From<i128> for Numeric { | ||
#[inline] | ||
fn from(value: i128) -> Self { | ||
Self::BigInt(value.into()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove this, as well as From<u128> for Numeric
. If the user wants to a bigint or number they should explicitly say so.
Besides it's out of the scope of this PR of adding From<i/u128> for JsBigInt
Don't we want to implement the 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 |
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. |
This will be handled as part of #2276. |
This Pull Request fixes 1970.
It changes the following: