-
-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ecmascript): add
ToBigInt
and StringToBigInt
- Loading branch information
Showing
7 changed files
with
123 additions
and
94 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use num_traits::Zero; | ||
|
||
use num_bigint::BigInt; | ||
|
||
/// `StringToBigInt` | ||
/// | ||
/// <https://tc39.es/ecma262/#sec-stringtobigint> | ||
pub trait StringToBigInt<'a> { | ||
fn string_to_big_int(&self) -> Option<BigInt>; | ||
} | ||
|
||
impl<'a> StringToBigInt<'a> for &str { | ||
fn string_to_big_int(&self) -> Option<BigInt> { | ||
if self.contains('\u{000b}') { | ||
// vertical tab is not always whitespace | ||
return None; | ||
} | ||
|
||
let s = self.trim(); | ||
|
||
if s.is_empty() { | ||
return Some(BigInt::zero()); | ||
} | ||
|
||
if s.len() > 2 && s.starts_with('0') { | ||
let radix: u32 = match s.chars().nth(1) { | ||
Some('x' | 'X') => 16, | ||
Some('o' | 'O') => 8, | ||
Some('b' | 'B') => 2, | ||
_ => 0, | ||
}; | ||
|
||
if radix == 0 { | ||
return None; | ||
} | ||
|
||
return BigInt::parse_bytes(s[2..].as_bytes(), radix); | ||
} | ||
|
||
return BigInt::parse_bytes(s.as_bytes(), 10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use num_bigint::BigInt; | ||
use num_traits::{One, Zero}; | ||
|
||
use oxc_ast::ast::Expression; | ||
use oxc_syntax::operator::UnaryOperator; | ||
|
||
use crate::{StringToBigInt, ToBoolean, ToJsString}; | ||
|
||
/// `ToBigInt` | ||
/// | ||
/// <https://tc39.es/ecma262/#sec-tobigint> | ||
pub trait ToBigInt<'a> { | ||
fn to_big_int(&self) -> Option<BigInt>; | ||
} | ||
|
||
impl<'a> ToBigInt<'a> for Expression<'a> { | ||
#[expect(clippy::cast_possible_truncation)] | ||
fn to_big_int(&self) -> Option<BigInt> { | ||
match self { | ||
Expression::NumericLiteral(number_literal) => { | ||
let value = number_literal.value; | ||
if value.abs() < 2_f64.powi(53) && value.fract() == 0.0 { | ||
Some(BigInt::from(value as i64)) | ||
} else { | ||
None | ||
} | ||
} | ||
Expression::BigIntLiteral(bigint_literal) => { | ||
let value = bigint_literal.raw.as_str().trim_end_matches('n').string_to_big_int(); | ||
debug_assert!(value.is_some(), "Failed to parse {}", bigint_literal.raw); | ||
value | ||
} | ||
Expression::BooleanLiteral(bool_literal) => { | ||
if bool_literal.value { | ||
Some(BigInt::one()) | ||
} else { | ||
Some(BigInt::zero()) | ||
} | ||
} | ||
Expression::UnaryExpression(unary_expr) => match unary_expr.operator { | ||
UnaryOperator::LogicalNot => { | ||
self.to_boolean().map( | ||
|boolean| { | ||
if boolean { | ||
BigInt::one() | ||
} else { | ||
BigInt::zero() | ||
} | ||
}, | ||
) | ||
} | ||
UnaryOperator::UnaryNegation => { | ||
unary_expr.argument.to_big_int().map(std::ops::Neg::neg) | ||
} | ||
UnaryOperator::BitwiseNot => { | ||
unary_expr.argument.to_big_int().map(std::ops::Not::not) | ||
} | ||
UnaryOperator::UnaryPlus => unary_expr.argument.to_big_int(), | ||
_ => None, | ||
}, | ||
Expression::StringLiteral(string_literal) => { | ||
string_literal.value.as_str().string_to_big_int() | ||
} | ||
Expression::TemplateLiteral(_) => { | ||
self.to_js_string().and_then(|value| value.as_ref().string_to_big_int()) | ||
} | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters