Skip to content

Commit 0200ec1

Browse files
committed
[GR-30922] Introduce Template Literal AST node as a replacement for RuntimeNode.
PullRequest: js/1985
2 parents 7ef89aa + 5ce4e01 commit 0200ec1

File tree

9 files changed

+310
-309
lines changed

9 files changed

+310
-309
lines changed

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/Parser.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@
157157
import com.oracle.js.parser.ir.PropertyKey;
158158
import com.oracle.js.parser.ir.PropertyNode;
159159
import com.oracle.js.parser.ir.ReturnNode;
160-
import com.oracle.js.parser.ir.RuntimeNode;
161160
import com.oracle.js.parser.ir.Scope;
162161
import com.oracle.js.parser.ir.Statement;
163162
import com.oracle.js.parser.ir.SwitchNode;
164163
import com.oracle.js.parser.ir.Symbol;
164+
import com.oracle.js.parser.ir.TemplateLiteralNode;
165165
import com.oracle.js.parser.ir.TernaryNode;
166166
import com.oracle.js.parser.ir.ThrowNode;
167167
import com.oracle.js.parser.ir.TryNode;
@@ -4412,7 +4412,7 @@ private Expression leftHandSideExpression(boolean yield, boolean await) {
44124412

44134413
final List<Expression> arguments = templateLiteralArgumentList(yield, await);
44144414

4415-
lhs = CallNode.forCall(callLine, callToken, lhs.getStart(), finish, lhs, arguments, false, optionalChain);
4415+
lhs = CallNode.forTaggedTemplateLiteral(callLine, callToken, lhs.getStart(), finish, lhs, arguments);
44164416

44174417
break;
44184418
}
@@ -6190,7 +6190,7 @@ private void endOfLine() {
61906190
private Expression templateLiteral(boolean yield, boolean await) {
61916191
assert type == TEMPLATE || type == TEMPLATE_HEAD;
61926192
final boolean noSubstitutionTemplate = type == TEMPLATE;
6193-
long lastLiteralToken = token;
6193+
final long startToken = token;
61946194
boolean previousPauseOnRightBrace = lexer.pauseOnRightBrace;
61956195
try {
61966196
lexer.pauseOnRightBrace = true;
@@ -6199,18 +6199,17 @@ private Expression templateLiteral(boolean yield, boolean await) {
61996199
return literal;
62006200
}
62016201

6202-
Expression concat = literal;
6202+
List<Expression> expressions = new ArrayList<>();
6203+
expressions.add(literal);
62036204
TokenType lastLiteralType;
62046205
do {
62056206
Expression expression = templateLiteralExpression(yield, await);
6206-
expression = new RuntimeNode(Token.recast(expression.getToken(), VOID), expression.getFinish(), RuntimeNode.Request.TO_STRING, expression);
6207-
concat = new BinaryNode(Token.recast(lastLiteralToken, TokenType.ADD), concat, expression);
6207+
expressions.add(expression);
62086208
lastLiteralType = type;
6209-
lastLiteralToken = token;
62106209
literal = getLiteral();
6211-
concat = new BinaryNode(Token.recast(lastLiteralToken, TokenType.ADD), concat, literal);
6210+
expressions.add(literal);
62126211
} while (lastLiteralType == TEMPLATE_MIDDLE);
6213-
return concat;
6212+
return TemplateLiteralNode.newUntagged(startToken, literal.getFinish(), expressions);
62146213
} finally {
62156214
lexer.pauseOnRightBrace = previousPauseOnRightBrace;
62166215
}
@@ -6261,9 +6260,7 @@ private List<Expression> templateLiteralArgumentList(boolean yield, boolean awai
62616260
} while (lastLiteralType == TEMPLATE_MIDDLE);
62626261
}
62636262

6264-
final LiteralNode<Expression[]> rawStringArray = LiteralNode.newInstance(templateToken, finish, rawStrings);
6265-
final LiteralNode<Expression[]> cookedStringArray = LiteralNode.newInstance(templateToken, finish, cookedStrings);
6266-
final RuntimeNode templateObject = new RuntimeNode(templateToken, finish, RuntimeNode.Request.GET_TEMPLATE_OBJECT, rawStringArray, cookedStringArray);
6263+
final Expression templateObject = TemplateLiteralNode.newTagged(templateToken, rawStrings.get(rawStrings.size() - 1).getFinish(), rawStrings, cookedStrings);
62676264
argumentList.set(0, templateObject);
62686265
return optimizeList(argumentList);
62696266
} finally {

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/ir/CallNode.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -76,6 +76,9 @@ public final class CallNode extends OptionalExpression {
7676
/** Is this call part of an optional chain. */
7777
private static final int IS_OPTIONAL_CHAIN = 1 << 5;
7878

79+
/** Is this a tagged template literal call. */
80+
private static final int IS_TAGGED_TEMPLATE_LITERAL = 1 << 6;
81+
7982
private final int flags;
8083

8184
private final int lineNumber;
@@ -95,8 +98,19 @@ public static Expression forCall(int lineNumber, long token, int start, int fini
9598

9699
public static Expression forCall(int lineNumber, long token, int start, int finish, Expression function, List<Expression> args,
97100
boolean optional, boolean optionalChain, boolean isEval, boolean isApplyArguments) {
101+
return create(lineNumber, token, start, finish, function, args, optional, optionalChain, isEval, isApplyArguments, false);
102+
}
103+
104+
public static Expression forTaggedTemplateLiteral(int lineNumber, long token, int start, int finish, Expression function, List<Expression> args) {
105+
return create(lineNumber, token, start, finish, function, args, false, false, false, false, true);
106+
}
107+
108+
private static Expression create(int lineNumber, long token, int start, int finish, Expression function, List<Expression> args,
109+
boolean optional, boolean optionalChain, boolean isEval, boolean isApplyArguments, boolean isTaggedTemplateLiteral) {
98110
return new CallNode(lineNumber, token, start, finish, setIsFunction(function), args,
99-
(optional ? IS_OPTIONAL : 0) | (optionalChain ? IS_OPTIONAL_CHAIN : 0) | (isEval ? IS_EVAL : 0) | (isApplyArguments ? IS_APPLY_ARGUMENTS : 0));
111+
(optional ? IS_OPTIONAL : 0) | (optionalChain ? IS_OPTIONAL_CHAIN : 0) |
112+
(isEval ? IS_EVAL : 0) | (isApplyArguments ? IS_APPLY_ARGUMENTS : 0) |
113+
(isTaggedTemplateLiteral ? IS_TAGGED_TEMPLATE_LITERAL : 0));
100114
}
101115

102116
public static Expression forImport(int lineNumber, long token, int start, int finish, IdentNode importIdent, List<Expression> args) {
@@ -266,4 +280,11 @@ public boolean isOptional() {
266280
public boolean isOptionalChain() {
267281
return (flags & IS_OPTIONAL_CHAIN) != 0;
268282
}
283+
284+
/**
285+
* Check if this is a tagged template literal call.
286+
*/
287+
public boolean isTaggedTemplateLiteral() {
288+
return (flags & IS_TAGGED_TEMPLATE_LITERAL) != 0;
289+
}
269290
}

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/ir/RuntimeNode.java

Lines changed: 0 additions & 220 deletions
This file was deleted.

0 commit comments

Comments
 (0)