Skip to content

Commit 88adf99

Browse files
authored
Migrated NativeConsole away from IdScriptableObject
* Migrated `NativeConsole` away from `IdScriptableObject` --------- Co-authored-by: andrea.bergia <andrea.bergia@servicenow.com>
1 parent 1afb694 commit 88adf99

1 file changed

Lines changed: 94 additions & 188 deletions

File tree

rhino/src/main/java/org/mozilla/javascript/NativeConsole.java

Lines changed: 94 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
import java.util.regex.Matcher;
1515
import java.util.regex.Pattern;
1616

17-
public class NativeConsole extends IdScriptableObject {
17+
public class NativeConsole extends ScriptableObject {
1818
private static final long serialVersionUID = 5694613212458273057L;
1919

20-
private static final Object CONSOLE_TAG = "Console";
20+
private static final String CLASS_NAME = "Console";
2121

2222
private static final String DEFAULT_LABEL = "default";
2323

@@ -48,9 +48,31 @@ void print(
4848

4949
public static void init(Scriptable scope, boolean sealed, ConsolePrinter printer) {
5050
NativeConsole obj = new NativeConsole(printer);
51-
obj.activatePrototypeMap(MAX_ID);
5251
obj.setPrototype(getObjectPrototype(scope));
5352
obj.setParentScope(scope);
53+
54+
obj.defineProperty(
55+
scope, "toSource", 0, NativeConsole::js_toSource, 0, DONTENUM | READONLY);
56+
obj.defineBuiltinProperty(
57+
scope, "trace", 1, NativeConsole::js_trace, 0, DONTENUM | READONLY);
58+
obj.defineBuiltinProperty(
59+
scope, "debug", 1, NativeConsole::js_debug, 0, DONTENUM | READONLY);
60+
obj.defineBuiltinProperty(scope, "log", 1, NativeConsole::js_log, 0, DONTENUM | READONLY);
61+
obj.defineBuiltinProperty(scope, "info", 1, NativeConsole::js_info, 0, DONTENUM | READONLY);
62+
obj.defineBuiltinProperty(scope, "warn", 1, NativeConsole::js_warn, 0, DONTENUM | READONLY);
63+
obj.defineBuiltinProperty(
64+
scope, "error", 1, NativeConsole::js_error, 0, DONTENUM | READONLY);
65+
obj.defineBuiltinProperty(
66+
scope, "assert", 2, NativeConsole::js_assert, 0, DONTENUM | READONLY);
67+
obj.defineBuiltinProperty(
68+
scope, "count", 1, NativeConsole::js_count, 0, DONTENUM | READONLY);
69+
obj.defineBuiltinProperty(
70+
scope, "countReset", 1, NativeConsole::js_countReset, 0, DONTENUM | READONLY);
71+
obj.defineBuiltinProperty(scope, "time", 1, NativeConsole::js_time, 0, DONTENUM | READONLY);
72+
obj.defineBuiltinProperty(
73+
scope, "timeEnd", 1, NativeConsole::js_timeEnd, 0, DONTENUM | READONLY);
74+
obj.defineBuiltinProperty(
75+
scope, "timeLog", 2, NativeConsole::js_timeLog, 0, DONTENUM | READONLY);
5476
if (sealed) {
5577
obj.sealObject();
5678
}
@@ -63,141 +85,91 @@ private NativeConsole(ConsolePrinter printer) {
6385

6486
@Override
6587
public String getClassName() {
66-
return "Console";
88+
return CLASS_NAME;
6789
}
6890

69-
@Override
70-
protected void initPrototypeId(int id) {
71-
if (id > LAST_METHOD_ID) {
72-
throw new IllegalStateException(String.valueOf(id));
73-
}
74-
75-
String name;
76-
int arity;
77-
switch (id) {
78-
case Id_toSource:
79-
arity = 0;
80-
name = "toSource";
81-
break;
82-
case Id_trace:
83-
arity = 1;
84-
name = "trace";
85-
break;
86-
case Id_debug:
87-
arity = 1;
88-
name = "debug";
89-
break;
90-
case Id_log:
91-
arity = 1;
92-
name = "log";
93-
break;
94-
case Id_info:
95-
arity = 1;
96-
name = "info";
97-
break;
98-
case Id_warn:
99-
arity = 1;
100-
name = "warn";
101-
break;
102-
case Id_error:
103-
arity = 1;
104-
name = "error";
105-
break;
106-
case Id_assert:
107-
arity = 2;
108-
name = "assert";
109-
break;
110-
case Id_count:
111-
arity = 1;
112-
name = "count";
113-
break;
114-
case Id_countReset:
115-
arity = 1;
116-
name = "countReset";
117-
break;
118-
case Id_time:
119-
arity = 1;
120-
name = "time";
121-
break;
122-
case Id_timeEnd:
123-
arity = 1;
124-
name = "timeEnd";
125-
break;
126-
case Id_timeLog:
127-
arity = 2;
128-
name = "timeLog";
129-
break;
130-
default:
131-
throw new IllegalStateException(String.valueOf(id));
132-
}
133-
initPrototypeMethod(CONSOLE_TAG, id, name, arity);
91+
private static NativeConsole realThis(Scriptable thisObj, String methodName) {
92+
return LambdaConstructor.ensureType(thisObj, NativeConsole.class, methodName);
13493
}
13594

136-
@Override
137-
public Object execIdCall(
138-
IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
139-
if (!f.hasTag(CONSOLE_TAG)) {
140-
return super.execIdCall(f, cx, scope, thisObj, args);
141-
}
142-
143-
int methodId = f.methodId();
144-
switch (methodId) {
145-
case Id_toSource:
146-
return "Console";
147-
148-
case Id_trace:
149-
{
150-
ScriptStackElement[] stack =
151-
new EvaluatorException("[object Object]").getScriptStack();
152-
printer.print(cx, scope, Level.TRACE, args, stack);
153-
break;
154-
}
95+
private static Object js_toSource(
96+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
97+
realThis(thisObj, "toSource");
98+
return CLASS_NAME;
99+
}
155100

156-
case Id_debug:
157-
printer.print(cx, scope, Level.DEBUG, args, null);
158-
break;
101+
private static Object js_trace(
102+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
103+
var console = realThis(thisObj, "trace");
104+
ScriptStackElement[] stack = new EvaluatorException("[object Object]").getScriptStack();
105+
console.printer.print(cx, scope, Level.TRACE, args, stack);
106+
return Undefined.instance;
107+
}
159108

160-
case Id_log:
161-
case Id_info:
162-
printer.print(cx, scope, Level.INFO, args, null);
163-
break;
109+
private static Object js_debug(
110+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
111+
var console = realThis(thisObj, "debug");
112+
console.printer.print(cx, scope, Level.DEBUG, args, null);
113+
return Undefined.instance;
114+
}
164115

165-
case Id_warn:
166-
printer.print(cx, scope, Level.WARN, args, null);
167-
break;
116+
private static Object js_log(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
117+
var console = realThis(thisObj, "log");
118+
console.printer.print(cx, scope, Level.INFO, args, null);
119+
return Undefined.instance;
120+
}
168121

169-
case Id_error:
170-
printer.print(cx, scope, Level.ERROR, args, null);
171-
break;
122+
private static Object js_info(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
123+
var console = realThis(thisObj, "info");
124+
console.printer.print(cx, scope, Level.INFO, args, null);
125+
return Undefined.instance;
126+
}
172127

173-
case Id_assert:
174-
jsAssert(cx, scope, args);
175-
break;
128+
private static Object js_warn(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
129+
var console = realThis(thisObj, "warn");
130+
console.printer.print(cx, scope, Level.WARN, args, null);
131+
return Undefined.instance;
132+
}
176133

177-
case Id_count:
178-
count(cx, scope, args);
179-
break;
134+
private static Object js_error(
135+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
136+
var console = realThis(thisObj, "error");
137+
console.printer.print(cx, scope, Level.ERROR, args, null);
138+
return Undefined.instance;
139+
}
180140

181-
case Id_countReset:
182-
countReset(cx, scope, args);
183-
break;
141+
private static Object js_assert(
142+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
143+
realThis(thisObj, "assert").jsAssert(cx, scope, args);
144+
return Undefined.instance;
145+
}
184146

185-
case Id_time:
186-
time(cx, scope, args);
187-
break;
147+
private static Object js_count(
148+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
149+
realThis(thisObj, "assert").count(cx, scope, args);
150+
return Undefined.instance;
151+
}
188152

189-
case Id_timeEnd:
190-
timeEnd(cx, scope, args);
191-
break;
153+
private static Object js_countReset(
154+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
155+
realThis(thisObj, "countReset").countReset(cx, scope, args);
156+
return Undefined.instance;
157+
}
192158

193-
case Id_timeLog:
194-
timeLog(cx, scope, args);
195-
break;
159+
private static Object js_time(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
160+
realThis(thisObj, "time").time(cx, scope, args);
161+
return Undefined.instance;
162+
}
196163

197-
default:
198-
throw new IllegalStateException(String.valueOf(methodId));
199-
}
164+
private static Object js_timeEnd(
165+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
166+
realThis(thisObj, "timeEnd").timeEnd(cx, scope, args);
167+
return Undefined.instance;
168+
}
200169

170+
private static Object js_timeLog(
171+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
172+
realThis(thisObj, "timeLog").timeLog(cx, scope, args);
201173
return Undefined.instance;
202174
}
203175

@@ -461,70 +433,4 @@ private void timeLog(Context cx, Scriptable scope, Object[] args) {
461433
private double nano2Milli(Long nano) {
462434
return nano / 1000000D;
463435
}
464-
465-
@Override
466-
protected int findPrototypeId(String s) {
467-
int id;
468-
switch (s) {
469-
case "log":
470-
id = Id_log;
471-
break;
472-
case "info":
473-
id = Id_info;
474-
break;
475-
case "time":
476-
id = Id_time;
477-
break;
478-
case "warn":
479-
id = Id_warn;
480-
break;
481-
case "count":
482-
id = Id_count;
483-
break;
484-
case "debug":
485-
id = Id_debug;
486-
break;
487-
case "error":
488-
id = Id_error;
489-
break;
490-
case "trace":
491-
id = Id_trace;
492-
break;
493-
case "assert":
494-
id = Id_assert;
495-
break;
496-
case "timeEnd":
497-
id = Id_timeEnd;
498-
break;
499-
case "timeLog":
500-
id = Id_timeLog;
501-
break;
502-
case "toSource":
503-
id = Id_toSource;
504-
break;
505-
case "countReset":
506-
id = Id_countReset;
507-
break;
508-
default:
509-
id = 0;
510-
break;
511-
}
512-
return id;
513-
}
514-
515-
private static final int Id_toSource = 1,
516-
Id_trace = 2,
517-
Id_debug = 3,
518-
Id_log = 4,
519-
Id_info = 5,
520-
Id_warn = 6,
521-
Id_error = 7,
522-
Id_assert = 8,
523-
Id_count = 9,
524-
Id_countReset = 10,
525-
Id_time = 11,
526-
Id_timeEnd = 12,
527-
Id_timeLog = 13,
528-
LAST_METHOD_ID = 13,
529-
MAX_ID = 13;
530436
}

0 commit comments

Comments
 (0)