diff options
author | DexesTTP <dexes.ttp@gmail.com> | 2020-04-06 22:17:05 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-07 09:05:16 +0200 |
commit | e586dc285a499370be10f7a0292756f18d7aef4b (patch) | |
tree | ffa0cf583d37715a6f2b9a2da050d1ce53816294 /Libraries | |
parent | 154dcd1ed61243a7e8b0b54a75c27034298e4d1d (diff) | |
download | serenity-e586dc285a499370be10f7a0292756f18d7aef4b.zip |
LibJS: Allow parsing numeric and string literals in object expressions
Also updated the object-basic.js test to include this change
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Parser.cpp | 23 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/object-basic.js | 14 |
2 files changed, 32 insertions, 5 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 5787bc94aa..65916686cd 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -389,13 +389,30 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression() consume(TokenType::CurlyOpen); while (!match(TokenType::CurlyClose)) { - auto identifier = create_ast_node<Identifier>(consume(TokenType::Identifier).value()); + FlyString property_name; + if (match(TokenType::Identifier)) { + property_name = consume(TokenType::Identifier).value(); + } else if (match(TokenType::StringLiteral)) { + property_name = consume(TokenType::StringLiteral).string_value(); + } else if (match(TokenType::NumericLiteral)) { + property_name = consume(TokenType::NumericLiteral).value(); + } else { + m_parser_state.m_has_errors = true; + auto& current_token = m_parser_state.m_current_token; + fprintf(stderr, "Error: Unexpected token %s as member in object initialization. Expected a numeric literal, string literal or identifier (line: %zu, column: %zu))\n", + current_token.name(), + current_token.line_number(), + current_token.line_column()); + consume(); + continue; + } + if (match(TokenType::Colon)) { consume(TokenType::Colon); - properties.set(identifier->string(), parse_expression(0)); + properties.set(property_name, parse_expression(0)); } else { - properties.set(identifier->string(), identifier); + properties.set(property_name, create_ast_node<Identifier>(property_name)); } if (!match(TokenType::Comma)) diff --git a/Libraries/LibJS/Tests/object-basic.js b/Libraries/LibJS/Tests/object-basic.js index 9be713008c..1334ee6e1b 100644 --- a/Libraries/LibJS/Tests/object-basic.js +++ b/Libraries/LibJS/Tests/object-basic.js @@ -1,7 +1,11 @@ try { - var o = { foo: "bar" }; + var o = { 1: 23, foo: "bar", "hello": "friends" }; + assert(o[1] === 23); + assert(o["1"] === 23); assert(o.foo === "bar"); assert(o["foo"] === "bar"); + assert(o.hello === "friends"); + assert(o["hello"] === "friends"); o.baz = "test"; assert(o.baz === "test"); assert(o["baz"] === "test"); @@ -11,7 +15,13 @@ try { o[-1] = "hello friends"; assert(o[-1] === "hello friends"); assert(o["-1"] === "hello friends"); + + var math = { 3.14: "pi" }; + assert(math["3.14"] === "pi"); + // Note : this test doesn't pass yet due to floating-point literals being coerced to i32 on access + // assert(math[3.14] === "pi"); + console.log("PASS"); } catch (e) { console.log("FAIL: " + e); -}
\ No newline at end of file +} |