4141package com .oracle .truffle .js .builtins ;
4242
4343import java .io .PrintWriter ;
44- import java .util .HashMap ;
44+ import java .util .Map ;
4545
46- import com .oracle .truffle .api .CompilerDirectives ;
4746import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
4847import com .oracle .truffle .api .dsl .Specialization ;
4948import 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 ;
5958import com .oracle .truffle .js .builtins .GlobalBuiltins .JSGlobalPrintNode ;
6059import com .oracle .truffle .js .builtins .GlobalBuiltinsFactory .JSGlobalPrintNodeGen ;
6160import com .oracle .truffle .js .nodes .cast .JSToBooleanNode ;
6261import com .oracle .truffle .js .nodes .cast .JSToStringNode ;
6362import com .oracle .truffle .js .nodes .function .JSBuiltin ;
6463import com .oracle .truffle .js .nodes .function .JSBuiltinNode ;
64+ import com .oracle .truffle .js .runtime .JSConsoleUtil ;
6565import com .oracle .truffle .js .runtime .JSContext ;
6666import com .oracle .truffle .js .runtime .JSRuntime ;
6767import com .oracle .truffle .js .runtime .builtins .BuiltinEnum ;
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
0 commit comments