Skip to content

Commit c69d8da

Browse files
committed
Directly pass Lexer for string interning.
1 parent aebe389 commit c69d8da

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, 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
@@ -80,7 +80,7 @@
8080
* Responsible for converting source content into a stream of tokens.
8181
*/
8282
@SuppressWarnings("fallthrough")
83-
public class Lexer extends Scanner {
83+
public class Lexer extends Scanner implements StringPool {
8484
private static final boolean XML_LITERALS = Options.getBooleanProperty("lexer.xmlliterals");
8585

8686
private static final String MSG_EDIT_STRING_MISSING_BRACE = "edit.string.missing.brace";
@@ -2131,6 +2131,7 @@ public TruffleString valueOfRawString(final long token) {
21312131
return stringIntern(sb.toString());
21322132
}
21332133

2134+
@Override
21342135
public TruffleString stringIntern(TruffleString candidate) {
21352136
TruffleString interned = internedStrings.putIfAbsent(candidate.toJavaStringUncached(), candidate);
21362137
return interned == null ? candidate : interned;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ public FunctionNode parseFunctionBody(boolean generator, boolean async) {
649649
addFunctionDeclarations(function);
650650
} finally {
651651
functionDeclarations = null;
652-
function.finishBodyScope(lexer::stringIntern);
652+
function.finishBodyScope(lexer);
653653
restoreBlock(body);
654654
lc.pop(function);
655655
}
@@ -1197,7 +1197,7 @@ private FunctionNode program(final TruffleString scriptName, final int parseFlag
11971197
addFunctionDeclarations(script);
11981198
} finally {
11991199
functionDeclarations = null;
1200-
script.finishBodyScope(lexer::stringIntern);
1200+
script.finishBodyScope(lexer);
12011201
restoreBlock(body);
12021202
lc.pop(script);
12031203
}
@@ -2166,7 +2166,7 @@ private Pair<FunctionNode, Boolean> fieldInitializer(int lineNumber, long fieldT
21662166
try {
21672167
initializer = assignmentExpression(true, false, false);
21682168
} finally {
2169-
function.finishBodyScope(lexer::stringIntern);
2169+
function.finishBodyScope(lexer);
21702170
restoreBlock(body);
21712171
lc.propagateFunctionFlags();
21722172
lc.pop(function);
@@ -5757,7 +5757,7 @@ private Block functionBody(final ParserContextFunctionNode functionNode) {
57575757
expect(RBRACE);
57585758
}
57595759
} finally {
5760-
functionNode.finishBodyScope(lexer::stringIntern);
5760+
functionNode.finishBodyScope(lexer);
57615761
restoreBlock(body);
57625762
lc.propagateFunctionFlags();
57635763
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2023, 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
@@ -44,7 +44,6 @@
4444
import java.util.ArrayList;
4545
import java.util.List;
4646
import java.util.Map;
47-
import java.util.function.Function;
4847

4948
import com.oracle.js.parser.ir.Block;
5049
import com.oracle.js.parser.ir.Expression;
@@ -555,9 +554,9 @@ private void putFunctionSymbolIfAbsent(String bindingName, TruffleString binding
555554
}
556555
}
557556

558-
public void finishBodyScope(Function<TruffleString, TruffleString> stringIntern) {
557+
public void finishBodyScope(StringPool strings) {
559558
if (needsArguments()) {
560-
putFunctionSymbolIfAbsent(Parser.ARGUMENTS_NAME, stringIntern.apply(Parser.ARGUMENTS_NAME_TS), Symbol.IS_ARGUMENTS);
559+
putFunctionSymbolIfAbsent(Parser.ARGUMENTS_NAME, strings.stringIntern(Parser.ARGUMENTS_NAME_TS), Symbol.IS_ARGUMENTS);
561560
}
562561
if (hoistableBlockFunctionDeclarations != null) {
563562
declareHoistedBlockFunctionDeclarations();
@@ -571,13 +570,13 @@ public void finishBodyScope(Function<TruffleString, TruffleString> stringIntern)
571570
if (!isArrow()) {
572571
boolean needsThisForEval = hasEval() || hasArrowEval();
573572
if (usesThis() || usesSuper() || needsThisForEval || getFlag(FunctionNode.HAS_DIRECT_SUPER) != 0) {
574-
putFunctionSymbolIfAbsent(TokenType.THIS.getName(), stringIntern.apply(TokenType.THIS.getNameTS()), Symbol.IS_THIS);
573+
putFunctionSymbolIfAbsent(TokenType.THIS.getName(), strings.stringIntern(TokenType.THIS.getNameTS()), Symbol.IS_THIS);
575574
}
576575
if (usesSuper() || (isMethod() && needsThisForEval)) {
577-
putFunctionSymbolIfAbsent(TokenType.SUPER.getName(), stringIntern.apply(TokenType.SUPER.getNameTS()), Symbol.IS_SUPER);
576+
putFunctionSymbolIfAbsent(TokenType.SUPER.getName(), strings.stringIntern(TokenType.SUPER.getNameTS()), Symbol.IS_SUPER);
578577
}
579578
if (usesNewTarget() || needsThisForEval) {
580-
putFunctionSymbolIfAbsent(Parser.NEW_TARGET_NAME, stringIntern.apply(Parser.NEW_TARGET_NAME_TS), Symbol.IS_NEW_TARGET);
579+
putFunctionSymbolIfAbsent(Parser.NEW_TARGET_NAME, strings.stringIntern(Parser.NEW_TARGET_NAME_TS), Symbol.IS_NEW_TARGET);
581580
}
582581
}
583582
if (hasParameterExpressions()) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.js.parser;
42+
43+
import com.oracle.truffle.api.strings.TruffleString;
44+
45+
public interface StringPool {
46+
TruffleString stringIntern(TruffleString candidate);
47+
}

0 commit comments

Comments
 (0)