Skip to content

Commit 2c45a59

Browse files
committed
fix for $init0$ and $init$ not fully running
1 parent 5e4d89a commit 2c45a59

3 files changed

Lines changed: 32 additions & 20 deletions

File tree

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
11891189
buffer.append(", ");
11901190
if (isAnonymous) {
11911191
if (!(parent instanceof EnumConstantDeclaration))
1192-
func = "function(){Clazz.newInstance$(this, arguments[0], true);}";
1192+
func = "function(){Clazz.newInstance$(this, arguments[0],1,C$);}";
11931193
superclassName = "" + getSuperclassName(binding);
11941194
ITypeBinding[] declaredTypes = binding.getInterfaces();
11951195
if (declaredTypes != null && declaredTypes.length > 0) {
@@ -1229,10 +1229,9 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
12291229
// directly by the
12301230
// user using new Foo()
12311231
if (!isInterface) {
1232-
buffer.append("Clazz.newInstance$(this, arguments");
1233-
if (!isTopLevel)
1234-
buffer.append("[0], " + !isStatic(binding));
1235-
buffer.append(");\r\n");
1232+
buffer.append("Clazz.newInstance$(this, arguments")
1233+
.append(isTopLevel ? ",0" : "[0]," + !isStatic(binding))
1234+
.append(",C$);\r\n");
12361235
}
12371236
buffer.append("}");
12381237
}
@@ -1361,7 +1360,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
13611360
}
13621361
if (lstStatic.size() > 0 || hasDependents) {
13631362
pt = buffer.length();
1364-
buffer.append("\r\nC$.$clinit$ = function() {Clazz.load(C$, 1)");
1363+
buffer.append("\r\nC$.$clinit$ = function() {Clazz.load(C$, 1);");
13651364
for (int i = lstStatic.size(); --i >= 0;) {
13661365
BodyDeclaration element = lstStatic.remove(0);
13671366
if (element instanceof Initializer) {

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,16 @@ Clazz.new = function(c, args, cl) {
145145

146146
// an inner class will attach arguments to the arguments returned
147147
// Integer will be passed as is here, without c.exClazz, or cl
148+
var clInner = cl;
148149
cl = cl || c.exClazz || c;
149150
cl.$clinit$ && cl.$clinit$();
150151
var f = new (Function.prototype.bind.apply(cl, arguments));
151-
if (haveArgs && args[2] != inheritArgs) {
152-
cl.$init0$ && cl.$init0$.apply(f);
153-
c.apply(f, args);
152+
if (args[2] != inheritArgs) {
153+
// cl.$init0$ && cl.$init0$.apply(f);
154+
if (haveArgs) {
155+
c.apply(f, args);
156+
}
157+
clInner && clInner.$init$.apply(f);
154158
}
155159

156160
_profileNew && addProfileNew(myclass, window.performance.now() - t0);
@@ -179,7 +183,7 @@ Clazz.super = function(cl, obj, andInit) {
179183
}
180184
}
181185

182-
Clazz.newInstance$ = function (objThis, args, isInner) {
186+
Clazz.newInstance$ = function (objThis, args, isInner, clazz) {
183187
if (args && (
184188
args[0] == inheritArgs
185189
|| args[1] == inheritArgs
@@ -200,6 +204,8 @@ Clazz.newInstance$ = function (objThis, args, isInner) {
200204

201205
objThis.__JSID__ = ++_jsid;
202206

207+
clazz && clazz.$init0$ && clazz.$init0$.apply(objThis);
208+
203209
if (!isInner) {
204210
if ((!args || args.length == 0) && objThis.c$) {
205211
// allow for direct default call "new foo()" to run with its default constructor
@@ -270,7 +276,7 @@ Clazz.defineStatics$ = function(cl, a) {
270276

271277
Clazz.newMethod$ = function (clazzThis, funName, funBody, isStatic) {
272278
if (arguments.length == 1) {
273-
return Clazz.newMethod$(clazzThis, 'c$', function(){Clazz.super(clazzThis, this,1);}, 1);
279+
return Clazz.newMethod$(clazzThis, 'c$', function(){Clazz.super(clazzThis, this,1);clazzThis.$init$.apply(this)}, 1);
274280
}
275281
if (funName.constructor == Array) {
276282
// If funName is an array, we are setting aliases for generic calls.
@@ -654,7 +660,7 @@ Clazz.newClass$ = function (prefix, name, clazz, clazzSuper, interfacez, type) {
654660
var qualifiedName = (prefix ? (prefix.__PKG_NAME__ || prefix.__CLASS_NAME__) + "." : "") + name;
655661
checkDeclared(qualifiedName, type);
656662
}
657-
clazz || (clazz = function () {Clazz.newInstance$(this,arguments)});
663+
clazz || (clazz = function () {Clazz.newInstance$(this,arguments,0,clazz)});
658664
clazz.__NAME__ = name;
659665
clazz.$load$ = [clazzSuper, interfacez];
660666

@@ -1073,6 +1079,7 @@ var extendObject = function(clazz, exclude) {
10731079
var excludeSuper = function(o) {
10741080
return o == "b$"
10751081
|| o == "$init$"
1082+
|| o == "$init0$"
10761083
|| o == "$clinit$"
10771084
|| o == "$load$"
10781085
|| o == "c$"

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13109,12 +13109,16 @@ Clazz.new = function(c, args, cl) {
1310913109

1311013110
// an inner class will attach arguments to the arguments returned
1311113111
// Integer will be passed as is here, without c.exClazz, or cl
13112+
var clInner = cl;
1311213113
cl = cl || c.exClazz || c;
1311313114
cl.$clinit$ && cl.$clinit$();
1311413115
var f = new (Function.prototype.bind.apply(cl, arguments));
13115-
if (haveArgs && args[2] != inheritArgs) {
13116-
cl.$init0$ && cl.$init0$.apply(f);
13117-
c.apply(f, args);
13116+
if (args[2] != inheritArgs) {
13117+
// cl.$init0$ && cl.$init0$.apply(f);
13118+
if (haveArgs) {
13119+
c.apply(f, args);
13120+
}
13121+
clInner && clInner.$init$.apply(f);
1311813122
}
1311913123

1312013124
_profileNew && addProfileNew(myclass, window.performance.now() - t0);
@@ -13143,7 +13147,7 @@ Clazz.super = function(cl, obj, andInit) {
1314313147
}
1314413148
}
1314513149

13146-
Clazz.newInstance$ = function (objThis, args, isInner) {
13150+
Clazz.newInstance$ = function (objThis, args, isInner, clazz) {
1314713151
if (args && (
1314813152
args[0] == inheritArgs
1314913153
|| args[1] == inheritArgs
@@ -13164,6 +13168,8 @@ Clazz.newInstance$ = function (objThis, args, isInner) {
1316413168

1316513169
objThis.__JSID__ = ++_jsid;
1316613170

13171+
clazz && clazz.$init0$ && clazz.$init0$.apply(objThis);
13172+
1316713173
if (!isInner) {
1316813174
if ((!args || args.length == 0) && objThis.c$) {
1316913175
// allow for direct default call "new foo()" to run with its default constructor
@@ -13234,7 +13240,7 @@ Clazz.defineStatics$ = function(cl, a) {
1323413240

1323513241
Clazz.newMethod$ = function (clazzThis, funName, funBody, isStatic) {
1323613242
if (arguments.length == 1) {
13237-
return Clazz.newMethod$(clazzThis, 'c$', function(){Clazz.super(clazzThis, this,1);}, 1);
13243+
return Clazz.newMethod$(clazzThis, 'c$', function(){Clazz.super(clazzThis, this,1);clazzThis.$init$.apply(this)}, 1);
1323813244
}
1323913245
if (funName.constructor == Array) {
1324013246
// If funName is an array, we are setting aliases for generic calls.
@@ -13618,7 +13624,7 @@ Clazz.newClass$ = function (prefix, name, clazz, clazzSuper, interfacez, type) {
1361813624
var qualifiedName = (prefix ? (prefix.__PKG_NAME__ || prefix.__CLASS_NAME__) + "." : "") + name;
1361913625
checkDeclared(qualifiedName, type);
1362013626
}
13621-
clazz || (clazz = function () {Clazz.newInstance$(this,arguments)});
13627+
clazz || (clazz = function () {Clazz.newInstance$(this,arguments,0,clazz)});
1362213628
clazz.__NAME__ = name;
1362313629
clazz.$load$ = [clazzSuper, interfacez];
1362413630

@@ -14037,6 +14043,7 @@ var extendObject = function(clazz, exclude) {
1403714043
var excludeSuper = function(o) {
1403814044
return o == "b$"
1403914045
|| o == "$init$"
14046+
|| o == "$init0$"
1404014047
|| o == "$clinit$"
1404114048
|| o == "$load$"
1404214049
|| o == "c$"
@@ -14066,9 +14073,8 @@ var finalizeClazz = function(clazz, qname, bname, type, isNumber) {
1406614073
(type == 1) && (clazz.__ANON = clazz.prototype.__ANON = 1);
1406714074
(type == 2) && (clazz.__LOCAL = clazz.prototype.__LOCAL = 1);
1406814075

14069-
if (!isNumber && type != 0) {
14076+
if (!isNumber && type != 0)
1407014077
Clazz.newMethod$(clazz, '$init0$', function(){var c;if ((c=clazz.superClazz) && (c = c.$init0$))c.apply(this);}, 1);
14071-
}
1407214078
extendPrototype(clazz);
1407314079

1407414080
};

0 commit comments

Comments
 (0)