summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2020-05-28 15:10:22 -0700
committerAndreas Kling <kling@serenityos.org>2020-05-29 15:56:39 +0200
commit5cd01ed79e816bb0a81c429f7efaa5de24c4b598 (patch)
tree41c31ba76fd9720ab536cf4b52f938a84b0cde33
parent688ca7b196d2567f46125e54fa8463327cc798d4 (diff)
downloadserenity-5cd01ed79e816bb0a81c429f7efaa5de24c4b598.zip
LibJS: New expressions look for expressions with correct precedence
-rw-r--r--Libraries/LibJS/Parser.cpp3
-rw-r--r--Libraries/LibJS/Tests/new-expression.js51
2 files changed, 52 insertions, 2 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp
index df5330af58..c06028f5a9 100644
--- a/Libraries/LibJS/Parser.cpp
+++ b/Libraries/LibJS/Parser.cpp
@@ -893,8 +893,7 @@ NonnullRefPtr<NewExpression> Parser::parse_new_expression()
{
consume(TokenType::New);
- // FIXME: Support full expressions as the callee as well.
- auto callee = create_ast_node<Identifier>(consume(TokenType::Identifier).value());
+ auto callee = parse_expression(g_operator_precedence.get(TokenType::New).value(), Associativity::Right, { TokenType::ParenOpen });
Vector<CallExpression::Argument> arguments;
diff --git a/Libraries/LibJS/Tests/new-expression.js b/Libraries/LibJS/Tests/new-expression.js
new file mode 100644
index 0000000000..7b1f6917fb
--- /dev/null
+++ b/Libraries/LibJS/Tests/new-expression.js
@@ -0,0 +1,51 @@
+load("test-common.js");
+
+try {
+ function Foo() { this.x = 1; }
+
+ let foo = new Foo();
+ assert(foo.x === 1);
+
+ foo = new Foo
+ assert(foo.x === 1);
+
+ foo = new
+ Foo
+ ()
+ assert(foo.x === 1);
+
+ foo = new Foo + 2
+ assert(foo === "[object Object]2");
+
+ let a = {
+ b: function() {
+ this.x = 2;
+ },
+ };
+
+ foo = new a.b();
+ assert(foo.x === 2);
+
+ foo = new a.b;
+ assert(foo.x === 2);
+
+ foo = new
+ a.b();
+ assert(foo.x === 2);
+
+ function funcGetter() {
+ return function(a, b) {
+ this.x = a + b;
+ };
+ };
+
+ foo = new funcGetter()(1, 5);
+ assert(foo === undefined);
+
+ foo = new (funcGetter())(1, 5);
+ assert(foo.x === 6);
+
+ console.log("PASS");
+} catch (e) {
+ console.log("FAIL: " + e);
+}