Skip to content

Commit

Permalink
LibJS: Fix parsing of numeric object keys
Browse files Browse the repository at this point in the history
Numeric keys were interpreted as their source text, leading to
something like {0x10:true} to end up as {"0x10":true}
instead of {16:true}
  • Loading branch information
sunverwerth authored and awesomekling committed Dec 27, 2020
1 parent fdacfef commit be9c2fe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 2 additions & 4 deletions Libraries/LibJS/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,9 @@ NonnullRefPtr<Expression> Parser::parse_property_key()
if (match(TokenType::StringLiteral)) {
return parse_string_literal(consume());
} else if (match(TokenType::NumericLiteral)) {
// FIXME: "evaluate" key to double value, see https://github.com/SerenityOS/serenity/issues/3717
return create_ast_node<StringLiteral>(consume_and_validate_numeric_literal().value());
return create_ast_node<NumericLiteral>(consume().double_value());
} else if (match(TokenType::BigIntLiteral)) {
auto value = consume(TokenType::BigIntLiteral).value();
return create_ast_node<StringLiteral>(value.substring_view(0, value.length() - 1));
return create_ast_node<BigIntLiteral>(consume().value());
} else if (match(TokenType::BracketOpen)) {
consume(TokenType::BracketOpen);
auto result = parse_expression(0);
Expand Down
7 changes: 7 additions & 0 deletions Libraries/LibJS/Tests/object-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ describe("correct behavior", () => {
expect(object[symbol]).toBe(2);
});

test("numeric keys", () => {
expect({0x10:true}).toBe({16:true});
expect({0b10:true}).toBe({2:true});
expect({0o10:true}).toBe({8:true});
expect({.5:true}).toBe({"0.5":true});
});

test("computed properties", () => {
const foo = "bar";
const computed = "computed";
Expand Down

0 comments on commit be9c2fe

Please sign in to comment.