Skip to content

Commit 41ac2da

Browse files
committed
super-fast byte, short, int conversion
This update introduces a very fast way to ensure byte, short, and int numerical processing using $b$[0], $i$[0], $s$[0] Also fixes array and switch issues with char and Character, ensuring that those always are integers (or in the case of switch, possibly a string). Substantial cleaning/refactoring of ASTKeywordVisitor removes duplication of code and makes the character business much clearer. Should possibly consider using an int for char rather than single-character string. This would probably immensely simplify the code.
1 parent 5c1a1b4 commit 41ac2da

File tree

13 files changed

+917
-862
lines changed

13 files changed

+917
-862
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTKeywordVisitor.java

Lines changed: 687 additions & 714 deletions
Large diffs are not rendered by default.

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ private void addInnerTypeInstance(ClassInstanceCreation node, String className,
512512
buffer.append(")");
513513
}
514514

515-
@SuppressWarnings("null")
516515
protected void addMethodParameterList(List<?> arguments, IMethodBinding methodDeclaration, boolean isConstructor,
517516
String prefix, String suffix) {
518517
if (methodDeclaration == null)
@@ -533,40 +532,12 @@ protected void addMethodParameterList(List<?> arguments, IMethodBinding methodDe
533532
}
534533
} else {
535534
ITypeBinding[] parameterTypes = methodDeclaration.getParameterTypes();
536-
ITypeBinding declaringClass = methodDeclaration.getDeclaringClass();
537-
String clazzName = (declaringClass == null ? null : declaringClass.getQualifiedName());
538-
String methodName = methodDeclaration.getName();
539-
String post = ", ";
540535
int nparam = parameterTypes.length;
541536
if (prefix != null && (nparam > 0 || methodIsVarArgs)) {
542537
buffer.append(prefix);
543538
prefix = null;
544539
}
545-
for (int i = 0; i < nparam; i++) {
546-
ITypeBinding paramType = parameterTypes[i];
547-
String parameterTypeName = paramType.getName();
548-
Expression arg = (i < argCount ? (Expression) arguments.get(i) : null);
549-
if (i == nparam - 1) {
550-
// BH: can't just check for an array; it has to be an array with the right number of dimensions
551-
if (nparam != argCount || methodIsVarArgs && paramType.isArray()
552-
&& arg.resolveTypeBinding().getDimensions() != paramType.getDimensions()
553-
&& !(arg instanceof NullLiteral)) {
554-
buffer.append("[");
555-
for (int j = i; j < argCount; j++) {
556-
addMethodArgument((Expression) arguments.get(j), clazzName, methodName, parameterTypeName,
557-
i);
558-
if (j != argCount - 1) {
559-
buffer.append(", ");
560-
}
561-
}
562-
buffer.append("]");
563-
break;
564-
}
565-
post = "";
566-
}
567-
addMethodArgument(arg, clazzName, methodName, parameterTypeName, i);
568-
buffer.append(post);
569-
}
540+
addMethodArguments(parameterTypes, methodIsVarArgs, arguments);
570541
}
571542
if (prefix == null && suffix != null)
572543
buffer.append(suffix);
@@ -1056,7 +1027,7 @@ private boolean addThisOrSyntheticReference(Expression node) {
10561027
}
10571028

10581029
public boolean visit(SimpleName node) {
1059-
String constValue = checkConstantValue(node);
1030+
String constValue = getConstantValue(node);
10601031
if (constValue != null) {
10611032
buffer.append(constValue);
10621033
return false;

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTVariableVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected String listFinalVariables(List<ASTFinalVariable> list, String seperato
179179
* @param node
180180
* @return
181181
*/
182-
protected String checkConstantValue(Expression node) {
182+
protected String getConstantValue(Expression node) {
183183
Object constValue = node.resolveConstantExpressionValue();
184184
if (constValue != null && (constValue instanceof Number
185185
|| constValue instanceof Character

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/SWTScriptVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected String[] skipDeclarePackages() {
9393
}
9494

9595
public boolean visit(SimpleName node) {
96-
String constValue = checkConstantValue(node);
96+
String constValue = getConstantValue(node);
9797
if (constValue != null) {
9898
buffer.append(constValue);
9999
return false;
@@ -121,7 +121,7 @@ public boolean visit(SimpleName node) {
121121
@SuppressWarnings("null")
122122
public boolean visit(QualifiedName node) {
123123
if (isSimpleQualified(node)) {
124-
String constValue = checkConstantValue(node);
124+
String constValue = getConstantValue(node);
125125
if (constValue != null) {
126126
buffer.append(constValue);
127127
return false;

sources/net.sf.j2s.core/test/dev/js/j2sSwingJS.js

Lines changed: 73 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,13 +1261,6 @@ Clazz.instanceOf = function (obj, clazz) {
12611261
/* public */
12621262
Clazz._initializingException = false;
12631263

1264-
/**
1265-
* BH: used in Throwable
1266-
*
1267-
*/
1268-
/* public */
1269-
Clazz._callingStackTraces = [];
1270-
12711264
/**
12721265
* MethodException will be used as a signal to notify that the method is
12731266
* not found in the current clazz hierarchy.
@@ -2842,7 +2835,7 @@ updateNode = function(node, ulev, chain, _updateNode) {
28422835
for (var mustLength = node.musts.length, i = mustLength; --i >= 0;) {
28432836
var n = node.musts[i];
28442837
n.requiredBy = node;
2845-
System.out.println(n.name + " required by " + node.name)
2838+
//System.out.println(n.name + " required by " + node.name)
28462839
if (n.status < Node.STATUS_DECLARED && isClassDefined (n.name)) {
28472840
var nns = []; // a stack for onLoaded events
28482841
n.status = Node.STATUS_LOAD_COMPLETE;
@@ -4065,23 +4058,24 @@ Number.getName=Clazz._inF.getName;
40654058
Number.prototype.compareTo = function(x) { var a = this.valueOf(), b = x.valueOf(); return (a < b ? -1 : a == b ? 0 : 1) };
40664059
Number.compare = function(a,b) { return (a < b ? -1 : a == b ? 0 : 1) };
40674060

4061+
var $b$ = new Int8Array(1);
4062+
var $s$ = new Int16Array(1);
4063+
var $i$ = new Int32Array(1);
4064+
40684065
m$(Number,"shortValue",
40694066
function(){
4070-
var x = (this|0)&0xffff;
4071-
return (x >= 0x8000 ? x - 0x10000 : x);
4067+
return ($s$[0] = this, $s$[0]);
40724068
});
40734069

40744070
m$(Number,"byteValue",
40754071
function(){
4076-
var x = (this|0)&0xff;
4077-
return (x >= 0x80 ? x - 0x100 : x);
4072+
return ($b$[0] = this, $b$[0]);
40784073
});
40794074

40804075

40814076
m$(Number,"intValue",
40824077
function(){
4083-
var x = (this|0)&0xffffffff;
4084-
return (x >= 0x80000000 ? x - 0x100000000 : x);
4078+
return ($i$[0] = this, $i$[0]);
40854079
});
40864080

40874081
m$(Number,"longValue",
@@ -4095,7 +4089,7 @@ return this.valueOf();
40954089
});
40964090
m$(Number,"doubleValue",
40974091
function(){
4098-
return parseFloat(this.valueOf());
4092+
return this.valueOf();
40994093
});
41004094

41014095
m$(Number,"hashCode",
@@ -5331,6 +5325,7 @@ String.copyValueOf=sp.copyValueOf=function(){
53315325
};
53325326

53335327
sp.codePointAt || (sp.codePointAt = sp.charCodeAt); // Firefox only
5328+
sp.$c = function(){return this.charCodeAt(0)};
53345329
sp.codePointAt$I = sp.codePointAt;
53355330
// in order to implement CharSequence, we need .length(), but String only has .length
53365331
// so in ALL classes the transpiler changes x.length() to x.length$()
@@ -5490,6 +5485,9 @@ m$(c$,"charCodeAt",
54905485
function(i){
54915486
return(this.value).charCodeAt(i);
54925487
});
5488+
5489+
c$.prototype.$c = function(){return this.value.charCodeAt(0)};
5490+
54935491
m$(c$,"compareTo",
54945492
function(c){
54955493
return(this.value).charCodeAt(0)-(c.value).charCodeAt(0);
@@ -5679,39 +5677,29 @@ Clazz.newInstance$ (this, arguments);
56795677

56805678
m$(C$, 'construct', function () {
56815679
this.fillInStackTrace ();
5682-
this.detailMessage = null;
5680+
this.detailMessage = this.stack;
56835681
this.cause = this;
5684-
this.stackTrace = null;
56855682
}, 1);
56865683

56875684
m$(C$, 'construct$S', function (message) {
56885685
this.fillInStackTrace ();
56895686
this.cause = this;
5690-
this.stackTrace = null;
56915687
this.detailMessage = message;
56925688
}, 1);
56935689

56945690
m$(C$, 'construct$S$Throwable', function (message, cause) {
56955691
this.fillInStackTrace ();
5696-
this.stackTrace = null;
56975692
this.detailMessage = message;
56985693
this.cause = cause;
56995694
}, 1);
57005695

57015696
m$(C$, 'construct$Throwable', function (cause) {
57025697
this.fillInStackTrace ();
5703-
this.stackTrace = null;
5704-
this.detailMessage = (cause == null ? null : cause.toString ());
5698+
this.detailMessage = (cause == null ? this.stack : cause.toString ());
57055699
this.cause = cause;
57065700
}, 1);
57075701

5708-
m$(C$, 'getMessage', function () {
5709-
{
5710-
if (typeof this.message != "undefined") {
5711-
return this.message;
5712-
}
5713-
}return this.detailMessage;
5714-
});
5702+
m$(C$, 'getMessage', function () {return this.message || this.detailMessage});
57155703

57165704
m$(C$, 'getLocalizedMessage', function () {
57175705
return this.getMessage ();
@@ -5734,17 +5722,22 @@ var message = this.getLocalizedMessage ();
57345722
return (message != null) ? (s + ": " + message) : s;
57355723
});
57365724

5725+
m$(C$, 'getStackTrace', function () {
5726+
return this.stackTrace;
5727+
});
5728+
57375729
m$(C$, 'printStackTrace', function () {
57385730
System.err.println$O (this);
57395731
for (var i = 0; i < this.stackTrace.length; i++) {
57405732
var t = this.stackTrace[i];
57415733
var x = t.methodName.indexOf ("(");
5742-
var n = t.methodName.substring (0, x).replace (/\s+/g, "");
5743-
if (n != "construct" || t.nativeClazz == null
5744-
|| Clazz.getInheritedLevel (t.nativeClazz, Throwable) < 0) {
5734+
//var n = (x < 0 ? t.methodName : t.methodName.substring (0, x)).replace (/\s+/g, "");
5735+
if (t.nativeClazz == null || Clazz.getInheritedLevel (t.nativeClazz, Throwable) < 0) {
57455736
System.err.println (t);
57465737
}
57475738
}
5739+
// from a JavaScript error
5740+
this.stack && System.err.println(this.stack);
57485741
});
57495742

57505743
Clazz.newMethod$(C$, 'printStackTrace$java_io_PrintStream', function (s) {
@@ -5756,54 +5749,40 @@ this.printStackTrace ();
57565749
});
57575750

57585751
Clazz.newMethod$(C$, 'fillInStackTrace', function () {
5759-
this.stackTrace = new Array();
5752+
this.stackTrace = Clazz.newArray$(StackTraceElement);
57605753
var caller = arguments.callee.caller;
57615754
var superCaller = null;
5762-
var callerList = new Array();
5763-
var index = Clazz._callingStackTraces.length - 1;
5764-
var noLooping = true;
5765-
while (index > -1 || caller != null) {
5766-
var clazzName = null;
5767-
var nativeClazz = null;
5768-
if (!noLooping || caller == Clazz.tryToSearchAndExecute || caller == Clazz.superCall || caller == null) {
5769-
if (index < 0) {
5770-
break;
5771-
}
5772-
noLooping = true;
5773-
superCaller = Clazz.callingStackTraces[index].caller;
5774-
nativeClazz = Clazz.callingStackTraces[index].owner;
5775-
index--;
5776-
} else {
5777-
superCaller = caller;
5778-
if (superCaller.claxxOwner != null) {
5779-
nativeClazz = superCaller.claxxOwner;
5780-
} else if (superCaller.exClazz != null) {
5781-
nativeClazz = superCaller.exClazz;
5782-
}
5783-
}
5784-
var st =Clazz.$new(StackTraceElement.construct, [
5785-
((nativeClazz != null && nativeClazz.__CLASS_NAME__.length != 0) ?
5786-
nativeClazz.__CLASS_NAME__ : "anonymous"),
5787-
((superCaller.exName == null) ? "anonymous" : superCaller.exName),
5788-
null, -1]);
5789-
st.nativeClazz = nativeClazz;
5790-
this.stackTrace[this.stackTrace.length] = st;
5791-
for (var i = 0; i < callerList.length; i++) {
5792-
if (callerList[i] == superCaller) {
5793-
// ... stack information lost as recursive invocation existed ...
5794-
var st =Clazz.$new(StackTraceElement.construct, ["lost", "missing", null, -3]);
5795-
st.nativeClazz = null;
5796-
this.stackTrace[this.stackTrace.length] = st;
5797-
noLooping = false;
5798-
//break;
5799-
}
5800-
}
5801-
if (superCaller != null) {
5802-
callerList[callerList.length] = superCaller;
5803-
}
5804-
//caller = superCaller.arguments.callee.caller;
5805-
// Udo
5806-
caller = (superCaller && superCaller.arguments && superCaller.arguments.callee) ? superCaller.arguments.callee.caller : null;
5755+
var callerList = [];
5756+
var index = 0;
5757+
while (index < 20 && caller != null) {
5758+
index++;
5759+
var clazzName = null;
5760+
var nativeClazz = null;
5761+
superCaller = caller;
5762+
if (superCaller.exClazz != null) {
5763+
nativeClazz = superCaller.exClazz;
5764+
}
5765+
var st =Clazz.$new(StackTraceElement.construct, [
5766+
((nativeClazz != null && nativeClazz.__CLASS_NAME__.length != 0) ?
5767+
nativeClazz.__CLASS_NAME__ : "anonymous"),
5768+
((superCaller.exName == null) ? "anonymous" : superCaller.exName),
5769+
null, -1]);
5770+
st.nativeClazz = nativeClazz;
5771+
this.stackTrace.push(st);
5772+
for (var i = 0; i < callerList.length; i++) {
5773+
if (callerList[i] == superCaller) {
5774+
// ... stack information lost as recursive invocation existed ...
5775+
var st =Clazz.$new(StackTraceElement.construct, ["lost", "missing", null, -3]);
5776+
st.nativeClazz = null;
5777+
this.stackTrace.push(st);
5778+
index = 100;
5779+
break;
5780+
}
5781+
}
5782+
if (superCaller != null) {
5783+
callerList.push(superCaller);
5784+
}
5785+
caller = (superCaller && superCaller.arguments && superCaller.arguments.callee) ? superCaller.arguments.callee.caller : null;
58075786
}
58085787
Clazz.initializingException = false;
58095788
return this;
@@ -5882,24 +5861,24 @@ return this.lineNumber==-2;
58825861
});
58835862
m$(c$,"toString",
58845863
function(){
5885-
var buf=new StringBuilder(80);
5886-
buf.append(this.getClassName());
5887-
buf.append('.');
5888-
buf.append(this.getMethodName());
5864+
var buf=Clazz.$new(StringBuilder.construct$I, [80]);
5865+
buf.append$S(this.getClassName());
5866+
buf.append$S('.');
5867+
buf.append$S(this.getMethodName());
58895868
if(this.isNativeMethod()){
5890-
buf.append("(Native Method)");
5869+
buf.append$S("(Native Method)");
58915870
}else{
58925871
var fName=this.getFileName();
58935872
if(fName==null){
5894-
buf.append("(Unknown Source)");
5873+
buf.append$S("(Unknown Source)");
58955874
}else{
58965875
var lineNum=this.getLineNumber();
5897-
buf.append('(');
5898-
buf.append(fName);
5876+
buf.append$S('(');
5877+
buf.append$S(fName);
58995878
if(lineNum>=0){
5900-
buf.append(':');
5901-
buf.append(lineNum);
5902-
}buf.append(')');
5879+
buf.append$S(':');
5880+
buf.append$I(lineNum);
5881+
}buf.append$(')');
59035882
}}return buf.toString();
59045883
});
59055884

@@ -5910,12 +5889,17 @@ TypeError.prototype.getMessage || (TypeError.prototype.getMessage = function(){
59105889
Clazz.Error = Error;
59115890

59125891
var declareType = function(prefix, name, clazzSuper, interfacez) {
5913-
return Clazz.decorateAsClass(prefix, name, function(){}, clazzSuper, interfacez);
5892+
return Clazz.decorateAsClass(prefix, name, null, clazzSuper, interfacez);
59145893
};
59155894

59165895
// at least allow Error() by itself to work as before
59175896
Clazz._Error || (Clazz._Error = Error);
5897+
(function(){
5898+
for (var i in Throwable.prototype)
5899+
Clazz._Error.prototype[i] = Throwable.prototype[i];
5900+
})();
59185901

5902+
inheritClass(Clazz._Error, Throwable);
59195903
Clazz.decorateAsClass (java.lang, "Error", function (){return Clazz._Error();}, Throwable);
59205904

59215905
declareType(java.lang,"Exception",Throwable);

sources/net.sf.j2s.core/test/src/test/Test_2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ public void actionPerformed(ActionEvent e) {
778778
}
779779

780780
private void enableButtons(boolean isEnable) {
781+
@SuppressWarnings("synthetic-access")
781782
Enumeration<AbstractButton> x = bg.getElements();
782783
while (x.hasMoreElements())
783784
x.nextElement().setEnabled(isEnable);

sources/net.sf.j2s.core/test/src/test/Test_Array.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package test;
22

33
import java.lang.reflect.Array;
4-
import java.net.URL;
54

65
class Test_Array {
76

0 commit comments

Comments
 (0)