Skip to content

Commit 3f22b0f

Browse files
committed
cleanup
1 parent 07518aa commit 3f22b0f

5 files changed

Lines changed: 142 additions & 69 deletions

File tree

graal-js/src/com.oracle.truffle.js.builtins/src/com/oracle/truffle/js/builtins/ConsolePrototypeBuiltins.java renamed to graal-js/src/com.oracle.truffle.js.builtins/src/com/oracle/truffle/js/builtins/ConsoleBuiltins.java

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,27 @@
4141
package com.oracle.truffle.js.builtins;
4242

4343
import java.io.PrintWriter;
44-
import java.util.HashMap;
44+
import java.util.Map;
4545

46-
import com.oracle.truffle.api.CompilerDirectives;
4746
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4847
import com.oracle.truffle.api.dsl.Specialization;
4948
import com.oracle.truffle.api.object.DynamicObject;
50-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleAssertNodeGen;
51-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleClearNodeGen;
52-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleCountNodeGen;
53-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleCountResetNodeGen;
54-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleGroupEndNodeGen;
55-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleGroupNodeGen;
56-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleTimeEndNodeGen;
57-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleTimeLogNodeGen;
58-
import com.oracle.truffle.js.builtins.ConsolePrototypeBuiltinsFactory.JSConsoleTimeNodeGen;
49+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleAssertNodeGen;
50+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleClearNodeGen;
51+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleCountNodeGen;
52+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleCountResetNodeGen;
53+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleGroupEndNodeGen;
54+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleGroupNodeGen;
55+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleTimeEndNodeGen;
56+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleTimeLogNodeGen;
57+
import com.oracle.truffle.js.builtins.ConsoleBuiltinsFactory.JSConsoleTimeNodeGen;
5958
import com.oracle.truffle.js.builtins.GlobalBuiltins.JSGlobalPrintNode;
6059
import com.oracle.truffle.js.builtins.GlobalBuiltinsFactory.JSGlobalPrintNodeGen;
6160
import com.oracle.truffle.js.nodes.cast.JSToBooleanNode;
6261
import com.oracle.truffle.js.nodes.cast.JSToStringNode;
6362
import com.oracle.truffle.js.nodes.function.JSBuiltin;
6463
import com.oracle.truffle.js.nodes.function.JSBuiltinNode;
64+
import com.oracle.truffle.js.runtime.JSConsoleUtil;
6565
import com.oracle.truffle.js.runtime.JSContext;
6666
import com.oracle.truffle.js.runtime.JSRuntime;
6767
import com.oracle.truffle.js.runtime.builtins.BuiltinEnum;
@@ -70,12 +70,12 @@
7070
/**
7171
* Contains builtins for `console`.
7272
*/
73-
public final class ConsolePrototypeBuiltins extends JSBuiltinsContainer.SwitchEnum<ConsolePrototypeBuiltins.ConsolePrototype> {
74-
protected ConsolePrototypeBuiltins() {
75-
super("Console", ConsolePrototype.class);
73+
public final class ConsoleBuiltins extends JSBuiltinsContainer.SwitchEnum<ConsoleBuiltins.Console> {
74+
protected ConsoleBuiltins() {
75+
super("Console", Console.class);
7676
}
7777

78-
public enum ConsolePrototype implements BuiltinEnum<ConsolePrototype> {
78+
public enum Console implements BuiltinEnum<Console> {
7979
log(0),
8080
info(0),
8181
debug(0),
@@ -95,7 +95,7 @@ public enum ConsolePrototype implements BuiltinEnum<ConsolePrototype> {
9595

9696
private final int length;
9797

98-
ConsolePrototype(int length) {
98+
Console(int length) {
9999
this.length = length;
100100
}
101101

@@ -106,7 +106,7 @@ public int getLength() {
106106
}
107107

108108
@Override
109-
protected Object createNode(JSContext context, JSBuiltin builtin, boolean construct, boolean newTarget, ConsolePrototype builtinEnum) {
109+
protected Object createNode(JSContext context, JSBuiltin builtin, boolean construct, boolean newTarget, Console builtinEnum) {
110110
switch (builtinEnum) {
111111
case log:
112112
case info:
@@ -139,30 +139,16 @@ protected Object createNode(JSContext context, JSBuiltin builtin, boolean constr
139139
return null;
140140
}
141141

142-
private static HashMap<String, Integer> countMap;
143-
private static HashMap<String, Long> timeMap;
144-
145142
public abstract static class JSConsoleOperation extends JSBuiltinNode {
146143

147144
public JSConsoleOperation(JSContext context, JSBuiltin builtin) {
148145
super(context, builtin);
149146
}
150147

151-
protected HashMap<String, Integer> getCountMap() {
152-
if (countMap == null) {
153-
CompilerDirectives.transferToInterpreter();
154-
countMap = new HashMap<>();
155-
}
156-
return countMap;
148+
public JSConsoleUtil getConsoleUtil() {
149+
return getContext().getRealm().getConsoleUtil();
157150
}
158151

159-
protected HashMap<String, Long> getTimeMap() {
160-
if (timeMap == null) {
161-
CompilerDirectives.transferToInterpreter();
162-
timeMap = new HashMap<>();
163-
}
164-
return timeMap;
165-
}
166152
}
167153

168154
public abstract static class JSConsoleAssertNode extends JSConsoleOperation {
@@ -184,7 +170,7 @@ protected DynamicObject assertImpl(Object... data) {
184170
if (data.length > 1) {
185171
System.arraycopy(data, 1, arr, 1, data.length - 1);
186172
}
187-
arr[0] = "Assertion failed:";
173+
arr[0] = data.length > 1 ? "Assertion failed:" : "Assertion failed";
188174
printNode.executeObjectArray(arr);
189175
}
190176
return Undefined.instance;
@@ -221,13 +207,15 @@ public JSConsoleCountNode(JSContext context, JSBuiltin builtin) {
221207
protected DynamicObject count(Object label) {
222208
String key = label == Undefined.instance ? "default" : toStringNode.executeString(label);
223209
int count = 0;
224-
if (getCountMap().containsKey(key)) {
225-
count = getCountMap().get(key);
210+
JSConsoleUtil console = getConsoleUtil();
211+
Map<String, Integer> countMap = console.getCountMap();
212+
if (countMap.containsKey(key)) {
213+
count = countMap.get(key);
226214
}
227-
getCountMap().put(key, ++count);
215+
countMap.put(key, ++count);
228216

229217
PrintWriter writer = getContext().getRealm().getOutputWriter();
230-
writer.append(getContext().getRealm().getConsoleIndentationString());
218+
writer.append(console.getConsoleIndentationString());
231219
writer.append(key);
232220
writer.append(": ");
233221
writer.append(String.valueOf(count));
@@ -250,7 +238,7 @@ public JSConsoleCountResetNode(JSContext context, JSBuiltin builtin) {
250238
@TruffleBoundary
251239
protected DynamicObject count(Object label) {
252240
String key = label == Undefined.instance ? "default" : toStringNode.executeString(label);
253-
getCountMap().remove(key);
241+
getConsoleUtil().getCountMap().remove(key);
254242
return Undefined.instance;
255243
}
256244
}
@@ -272,7 +260,7 @@ protected DynamicObject group(Object[] label) {
272260
if (label.length > 0) {
273261
printNode.executeObjectArray(label);
274262
}
275-
getContext().getRealm().incConsoleIndentation();
263+
getConsoleUtil().incConsoleIndentation();
276264
return Undefined.instance;
277265
}
278266
}
@@ -287,7 +275,7 @@ public JSConsoleGroupEndNode(JSContext context, JSBuiltin builtin) {
287275
@Specialization
288276
@TruffleBoundary
289277
protected DynamicObject groupEnd() {
290-
getContext().getRealm().decConsoleIndentation();
278+
getConsoleUtil().decConsoleIndentation();
291279
return Undefined.instance;
292280
}
293281
}
@@ -304,7 +292,7 @@ public JSConsoleTimeNode(JSContext context, JSBuiltin builtin) {
304292
@TruffleBoundary
305293
protected DynamicObject time(Object label) {
306294
String key = label == Undefined.instance ? "default" : toStringNode.executeString(label);
307-
getTimeMap().put(key, getContext().getRealm().currentTimeMillis());
295+
getConsoleUtil().getTimeMap().put(key, getContext().getRealm().currentTimeMillis());
308296
return Undefined.instance;
309297
}
310298
}
@@ -323,8 +311,9 @@ public JSConsoleTimeEndNode(JSContext context, JSBuiltin builtin) {
323311
@TruffleBoundary
324312
protected DynamicObject timeEnd(Object label) {
325313
String key = label == Undefined.instance ? "default" : toStringNode.executeString(label);
326-
if (getTimeMap().containsKey(key)) {
327-
long start = getTimeMap().remove(key);
314+
Map<String, Long> timeMap = getConsoleUtil().getTimeMap();
315+
if (timeMap.containsKey(key)) {
316+
long start = timeMap.remove(key);
328317
long end = getContext().getRealm().currentTimeMillis();
329318
long delta = end - start;
330319
printNode.executeObjectArray(new Object[]{key + ":", String.valueOf(delta) + "ms"});
@@ -345,10 +334,12 @@ public JSConsoleTimeLogNode(JSContext context, JSBuiltin builtin) {
345334
}
346335

347336
@Specialization
337+
@TruffleBoundary
348338
protected DynamicObject timeLog(Object... data) {
349339
String key = data.length == 0 || data[0] == Undefined.instance ? "default" : toStringNode.executeString(data[0]);
350-
if (getTimeMap().containsKey(key)) {
351-
long start = getTimeMap().get(key);
340+
Map<String, Long> timeMap = getConsoleUtil().getTimeMap();
341+
if (timeMap.containsKey(key)) {
342+
long start = timeMap.get(key);
352343
long end = getContext().getRealm().currentTimeMillis();
353344
long delta = end - start;
354345

graal-js/src/com.oracle.truffle.js.builtins/src/com/oracle/truffle/js/builtins/GlobalBuiltins.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import com.oracle.truffle.js.runtime.Evaluator;
116116
import com.oracle.truffle.js.runtime.ExitException;
117117
import com.oracle.truffle.js.runtime.JSArguments;
118+
import com.oracle.truffle.js.runtime.JSConsoleUtil;
118119
import com.oracle.truffle.js.runtime.JSContext;
119120
import com.oracle.truffle.js.runtime.JSErrorType;
120121
import com.oracle.truffle.js.runtime.JSException;
@@ -990,8 +991,9 @@ public JSGlobalPrintNode(JSContext context, JSBuiltin builtin, boolean useErr) {
990991
protected Object print(Object[] arguments) {
991992
// without a StringBuilder, synchronization fails testnashorn JDK-8041998.js
992993
StringBuilder builder = new StringBuilder();
993-
if (getContext().getRealm().getConsoleIndentation() > 0) {
994-
Boundaries.builderAppend(builder, getContext().getRealm().getConsoleIndentationString());
994+
JSConsoleUtil consoleUtil = getContext().getRealm().getConsoleUtil();
995+
if (consoleUtil.getConsoleIndentation() > 0) {
996+
Boundaries.builderAppend(builder, consoleUtil.getConsoleIndentationString());
995997
}
996998
if (argumentsCount.profile(arguments.length == 1)) {
997999
Boundaries.builderAppend(builder, toString1(arguments[0]));

graal-js/src/com.oracle.truffle.js.builtins/src/com/oracle/truffle/js/builtins/JSDefaultBuiltinLookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public JSDefaultBuiltinLookup() {
107107
defineBuiltins(new DatePrototypeBuiltins());
108108
defineBuiltins(new DateFunctionBuiltins());
109109

110-
defineBuiltins(new ConsolePrototypeBuiltins());
110+
defineBuiltins(new ConsoleBuiltins());
111111

112112
defineBuiltins(new RegExpPrototypeBuiltins());
113113
defineBuiltins(new RegExpPrototypeGetterBuiltins());
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2018, 2018, 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.truffle.js.runtime;
42+
43+
import java.util.HashMap;
44+
import java.util.Map;
45+
46+
import com.oracle.truffle.api.CompilerAsserts;
47+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
48+
49+
public class JSConsoleUtil {
50+
51+
private Map<String, Integer> countMap;
52+
private Map<String, Long> timeMap;
53+
private int consoleIndentation = 0;
54+
55+
public Map<String, Integer> getCountMap() {
56+
CompilerAsserts.neverPartOfCompilation();
57+
if (countMap == null) {
58+
countMap = new HashMap<>();
59+
}
60+
return countMap;
61+
}
62+
63+
public Map<String, Long> getTimeMap() {
64+
CompilerAsserts.neverPartOfCompilation();
65+
if (timeMap == null) {
66+
timeMap = new HashMap<>();
67+
}
68+
return timeMap;
69+
}
70+
71+
public int getConsoleIndentation() {
72+
return consoleIndentation;
73+
}
74+
75+
public void incConsoleIndentation() {
76+
consoleIndentation++;
77+
}
78+
79+
public void decConsoleIndentation() {
80+
if (consoleIndentation > 0) {
81+
consoleIndentation--;
82+
}
83+
}
84+
85+
@TruffleBoundary
86+
public String getConsoleIndentationString() {
87+
StringBuilder sb = new StringBuilder();
88+
for (int i = 0; i < this.consoleIndentation; i++) {
89+
sb.append(" ");
90+
}
91+
return sb.toString();
92+
}
93+
}

graal-js/src/com.oracle.truffle.js.runtime/src/com/oracle/truffle/js/runtime/JSRealm.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.graalvm.options.OptionValues;
5353

5454
import com.oracle.truffle.api.CompilerAsserts;
55+
import com.oracle.truffle.api.CompilerDirectives;
5556
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5657
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5758
import com.oracle.truffle.api.Truffle;
@@ -264,7 +265,7 @@ public class JSRealm {
264265
private PrintWriterWrapper outputWriter;
265266
private PrintWriterWrapper errorWriter;
266267

267-
private int consoleIndentation = 0;
268+
@CompilationFinal private JSConsoleUtil consoleUtil;
268269

269270
public JSRealm(JSContext context, TruffleLanguage.Env env) {
270271
this.context = context;
@@ -1288,26 +1289,12 @@ public Shape getLazyRegexArrayShape() {
12881289
return lazyRegexArrayShape;
12891290
}
12901291

1291-
public int getConsoleIndentation() {
1292-
return consoleIndentation;
1293-
}
1294-
1295-
public void incConsoleIndentation() {
1296-
consoleIndentation++;
1297-
}
1298-
1299-
public void decConsoleIndentation() {
1300-
if (consoleIndentation > 0) {
1301-
consoleIndentation--;
1292+
public JSConsoleUtil getConsoleUtil() {
1293+
if (consoleUtil == null) {
1294+
CompilerDirectives.transferToInterpreterAndInvalidate();
1295+
consoleUtil = new JSConsoleUtil();
13021296
}
1297+
return consoleUtil;
13031298
}
13041299

1305-
@TruffleBoundary
1306-
public String getConsoleIndentationString() {
1307-
StringBuilder sb = new StringBuilder();
1308-
for (int i = 0; i < this.consoleIndentation; i++) {
1309-
sb.append(" ");
1310-
}
1311-
return sb.toString();
1312-
}
13131300
}

0 commit comments

Comments
 (0)