diff --git a/boa/src/syntax/lexer/mod.rs b/boa/src/syntax/lexer/mod.rs index bd7675c2cc6..f74a0866f70 100644 --- a/boa/src/syntax/lexer/mod.rs +++ b/boa/src/syntax/lexer/mod.rs @@ -383,17 +383,17 @@ impl<'a> Lexer<'a> { return Ok(()); } Some('x') | Some('X') => { - self.read_integer_in_base(16, buf)? + self.read_integer_in_base(16, buf)? as f64 } Some('o') | Some('O') => { - self.read_integer_in_base(8, buf)? + self.read_integer_in_base(8, buf)? as f64 } Some('b') | Some('B') => { - self.read_integer_in_base(2, buf)? + self.read_integer_in_base(2, buf)? as f64 } - Some(ch) if ch.is_ascii_digit() => { + Some(ch) if (ch.is_ascii_digit() || ch == '.') => { // LEGACY OCTAL (ONLY FOR NON-STRICT MODE) - let mut gone_decimal = false; + let mut gone_decimal = ch == '.'; while let Some(next_ch) = self.preview_next() { match next_ch { c if next_ch.is_digit(8) => { @@ -411,19 +411,19 @@ impl<'a> Lexer<'a> { } } if gone_decimal { - u64::from_str(&buf).map_err(|_e| LexerError::new("Could not convert value to u64"))? + f64::from_str(&buf).map_err(|_e| LexerError::new("Could not convert value to f64"))? } else if buf.is_empty() { - 0 + 0.0 } else { - u64::from_str_radix(&buf, 8).map_err(|_e| LexerError::new("Could not convert value to u64"))? + (u64::from_str_radix(&buf, 8).map_err(|_e| LexerError::new("Could not convert value to u64"))?) as f64 } } Some(_) => { - 0 + 0.0 } }; - self.push_token(TokenData::NumericLiteral(num as f64)); + self.push_token(TokenData::NumericLiteral(num)); //11.8.3 if let Err(e) = self.check_after_numeric_literal() { diff --git a/boa/src/syntax/lexer/tests.rs b/boa/src/syntax/lexer/tests.rs index c79652b0005..e41bff985c8 100644 --- a/boa/src/syntax/lexer/tests.rs +++ b/boa/src/syntax/lexer/tests.rs @@ -383,8 +383,10 @@ fn check_decrement_advances_lexer_2_places() { #[test] fn numbers() { - let mut lexer = - Lexer::new("1 2 0x34 056 7.89 42. 5e3 5e+3 5e-3 0b10 0O123 0999 1.0e1 1.0e-1 1.0E1 1E1"); + let mut lexer = Lexer::new( + "1 2 0x34 056 7.89 42. 5e3 5e+3 5e-3 0b10 0O123 0999 1.0e1 1.0e-1 1.0E1 1E1 0.0 0.12", + ); + lexer.lex().expect("failed to lex"); assert_eq!(lexer.tokens[0].data, TokenData::NumericLiteral(1.0)); assert_eq!(lexer.tokens[1].data, TokenData::NumericLiteral(2.0)); @@ -401,7 +403,9 @@ fn numbers() { assert_eq!(lexer.tokens[12].data, TokenData::NumericLiteral(10.0)); assert_eq!(lexer.tokens[13].data, TokenData::NumericLiteral(0.1)); assert_eq!(lexer.tokens[14].data, TokenData::NumericLiteral(10.0)); - assert_eq!(lexer.tokens[14].data, TokenData::NumericLiteral(10.0)); + assert_eq!(lexer.tokens[15].data, TokenData::NumericLiteral(10.0)); + assert_eq!(lexer.tokens[16].data, TokenData::NumericLiteral(0.0)); + assert_eq!(lexer.tokens[17].data, TokenData::NumericLiteral(0.12)); } #[test]