mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
LibJS: Disallow numerical separators in octal numbers and after '.'
This commit is contained in:
parent
32016d3924
commit
afde1821b5
Notes:
sideshowbarker
2024-07-17 23:18:56 +09:00
Author: https://github.com/davidot Commit: https://github.com/SerenityOS/serenity/commit/afde1821b50 Pull-request: https://github.com/SerenityOS/serenity/pull/11088 Issue: https://github.com/SerenityOS/serenity/issues/11078 Reviewed-by: https://github.com/IdanHo Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/linusg ✅
@ -654,7 +654,7 @@ Token Lexer::next()
|
|||||||
if (m_current_char == '.') {
|
if (m_current_char == '.') {
|
||||||
// decimal
|
// decimal
|
||||||
consume();
|
consume();
|
||||||
while (is_ascii_digit(m_current_char) || match_numeric_literal_separator_followed_by(is_ascii_digit))
|
while (is_ascii_digit(m_current_char))
|
||||||
consume();
|
consume();
|
||||||
if (m_current_char == 'e' || m_current_char == 'E')
|
if (m_current_char == 'e' || m_current_char == 'E')
|
||||||
is_invalid_numeric_literal = !consume_exponent();
|
is_invalid_numeric_literal = !consume_exponent();
|
||||||
@ -688,7 +688,7 @@ Token Lexer::next()
|
|||||||
// octal without '0o' prefix. Forbidden in 'strict mode'
|
// octal without '0o' prefix. Forbidden in 'strict mode'
|
||||||
do {
|
do {
|
||||||
consume();
|
consume();
|
||||||
} while (is_ascii_digit(m_current_char) || match_numeric_literal_separator_followed_by(is_ascii_digit));
|
} while (is_ascii_digit(m_current_char));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 1...9 or period
|
// 1...9 or period
|
||||||
@ -700,11 +700,15 @@ Token Lexer::next()
|
|||||||
} else {
|
} else {
|
||||||
if (m_current_char == '.') {
|
if (m_current_char == '.') {
|
||||||
consume();
|
consume();
|
||||||
while (is_ascii_digit(m_current_char) || match_numeric_literal_separator_followed_by(is_ascii_digit))
|
if (m_current_char == '_')
|
||||||
|
is_invalid_numeric_literal = true;
|
||||||
|
|
||||||
|
while (is_ascii_digit(m_current_char) || match_numeric_literal_separator_followed_by(is_ascii_digit)) {
|
||||||
consume();
|
consume();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (m_current_char == 'e' || m_current_char == 'E')
|
if (m_current_char == 'e' || m_current_char == 'E')
|
||||||
is_invalid_numeric_literal = !consume_exponent();
|
is_invalid_numeric_literal = is_invalid_numeric_literal || !consume_exponent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (is_invalid_numeric_literal) {
|
if (is_invalid_numeric_literal) {
|
||||||
|
26
Userland/Libraries/LibJS/Tests/syntax/numeric-separator.js
Normal file
26
Userland/Libraries/LibJS/Tests/syntax/numeric-separator.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
describe("numeric separators", () => {
|
||||||
|
test("numeric separator works for 'normal' number", () => {
|
||||||
|
expect("1_2").toEvalTo(12);
|
||||||
|
expect("4_2.4_2").toEvalTo(42.42);
|
||||||
|
expect("1_2e0_2").toEvalTo(1200);
|
||||||
|
|
||||||
|
expect("1_2E+_1").not.toEval();
|
||||||
|
expect("1_2E+0_1").toEvalTo(120);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("cannot use numeric separator after .", () => {
|
||||||
|
expect("4._3").not.toEval();
|
||||||
|
expect("0._3").not.toEval();
|
||||||
|
expect("1_.3._3").not.toEval();
|
||||||
|
|
||||||
|
// Actually a valid attempt to get property '_3' on 1.3 which fails but does parse.
|
||||||
|
expect("1.3._3").toEval();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("cannot use numeric separator in octal escaped number", () => {
|
||||||
|
expect("00_1").not.toEval();
|
||||||
|
expect("01_1").not.toEval();
|
||||||
|
expect("07_3").not.toEval();
|
||||||
|
expect("00_1").not.toEval();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user