Skip to content

Commit

Permalink
fix(es/lexer): Fix lexing of numbers with large exponent (#3061)
Browse files Browse the repository at this point in the history
swc_ecma_parser:
 - Fix lexing of numbers where exponents are large enough to be parsed as `Infinity`. (Closes #3060)
  • Loading branch information
asterite3 authored Dec 18, 2021
1 parent 8c16c2b commit 0c813ae
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions crates/swc_ecma_parser/src/lexer/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,22 @@ impl<'a, I: Input> Lexer<'a, I> {
};

let exp = self.read_number_no_dot(10)?;
let flag = if positive { '+' } else { '-' };

raw_val.push(flag);
write!(raw_val, "{}", exp).unwrap();
val = if exp == f64::INFINITY {
if positive && val != 0.0 {
f64::INFINITY
} else {
0.0
}
} else {
let flag = if positive { '+' } else { '-' };

raw_val.push(flag);
write!(raw_val, "{}", exp).unwrap();

// TODO:
val = raw_val.parse().expect("failed to parse float literal");
// TODO:
raw_val.parse().expect("failed to parse float literal")
}
}

self.ensure_not_ident()?;
Expand Down Expand Up @@ -478,6 +487,51 @@ mod tests {
assert_eq!(1e30, num("1e30"));
}

#[test]
fn num_very_big_exp() {
const LARGE_POSITIVE_EXP: &str =
"1e100000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000";
const LARGE_NEGATIVE_EXP: &str =
"1e-10000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000";
const ZERO_WITH_LARGE_POSITIVE_EXP: &str =
"0e100000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000";
const ZERO_WITH_LARGE_NEGATIVE_EXP: &str =
"0e-10000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000";
const LARGE_MANTISSA_WITH_LARGE_NEGATIVE_EXP: &str =
"10000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000\
e-100000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000";

assert_eq!(num(LARGE_POSITIVE_EXP), INFINITY);
assert_eq!(num(LARGE_NEGATIVE_EXP), 0.0);
assert_eq!(num(ZERO_WITH_LARGE_POSITIVE_EXP), 0.0);
assert_eq!(num(ZERO_WITH_LARGE_NEGATIVE_EXP), 0.0);
assert_eq!(num(LARGE_MANTISSA_WITH_LARGE_NEGATIVE_EXP), 0.0);
}

#[test]
fn num_big_many_zero() {
assert_eq!(
Expand Down

0 comments on commit 0c813ae

Please sign in to comment.