Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c33048e

Browse files
committed
Support avoid Array being polluted by setting window["j2s.array.native"]
= true; Fixed bug Array.isArray is incorrect for JavaScript Fixed bug Number.toString is incorrect for JavaScript with arguments Avoid adding unnecessary global variables like "i", "p", "ua" and others
1 parent 676e1d5 commit c33048e

File tree

9 files changed

+135
-106
lines changed

9 files changed

+135
-106
lines changed

sources/net.sf.j2s.java.core/src/java/lang/Boolean.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ java.lang.B00lean = Boolean;
33
java.lang.Boolean = Boolean = function () {
44
Clazz.instantialize (this, arguments);
55
};
6-
if (Clazz.supportsNativeObject) {
7-
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
8-
var p = Clazz.extendedObjectMethods[i];
9-
Boolean.prototype[p] = JavaObject.prototype[p];
6+
(function() {
7+
if (Clazz.supportsNativeObject) {
8+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
9+
var p = Clazz.extendedObjectMethods[i];
10+
Boolean.prototype[p] = JavaObject.prototype[p];
11+
}
1012
}
11-
}
13+
}) ();
1214
Boolean.__CLASS_NAME__ = "Boolean";
1315
Clazz.implementOf (Boolean, [java.io.Serializable, java.lang.Comparable]);
1416
Boolean.equals = Clazz.innerFunctions.equals;

sources/net.sf.j2s.java.core/src/java/lang/Class.js

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ JavaObject = Object;
4949

5050
/* protected */
5151
Clazz.supportsNativeObject = window["j2s.object.native"];
52+
Clazz.supportsNativeArray = window["j2s.array.native"];
5253

5354
if (Clazz.supportsNativeObject) {
5455
JavaObject = function () {};
@@ -92,23 +93,28 @@ JavaObject.prototype.toString = function () {
9293
if (this.__CLASS_NAME__ != null) {
9394
return "[" + this.__CLASS_NAME__ + " object]";
9495
} else {
95-
return this.to$tring.apply (this, arguments);
96+
return JavaObject.prototype.to$tring.apply (this, arguments);
9697
}
9798
};
9899

99-
if (Clazz.supportsNativeObject) {
100-
/* protected */
101-
Clazz.extendedObjectMethods = [
102-
"equals", "hashCode", "getClass", "clone", "finalize", "notify", "notifyAll", "wait", "to$tring", "toString"
103-
];
104-
105-
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
106-
var p = Clazz.extendedObjectMethods[i];
107-
Array.prototype[p] = JavaObject.prototype[p];
100+
(function() {
101+
if (Clazz.supportsNativeObject) {
102+
/* protected */
103+
Clazz.extendedObjectMethods = [
104+
"equals", "hashCode", "getClass", "clone", "finalize", "notify", "notifyAll", "wait", "to$tring", "toString"
105+
];
106+
if (!Clazz.supportsNativeArray) {
107+
Array.prototype.to$tring = Array.prototype.toString;
108+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
109+
var p = Clazz.extendedObjectMethods[i];
110+
if (p == "toString" || p == "to$tring") continue;
111+
Array.prototype[p] = JavaObject.prototype[p];
112+
}
113+
}
114+
JavaObject.__CLASS_NAME__ = "Object";
115+
JavaObject["getClass"] = function () { return JavaObject; };
108116
}
109-
JavaObject.__CLASS_NAME__ = "Object";
110-
JavaObject["getClass"] = function () { return JavaObject; };
111-
}
117+
}) ();
112118

113119
/**
114120
* Try to fix bug on Safari
@@ -1518,15 +1524,17 @@ Clazz.defineType = function (qClazzName, clazzFun, clazzParent, interfacez) {
15181524

15191525
Clazz.isSafari = (navigator.userAgent.indexOf ("Safari") != -1);
15201526
Clazz.isSafari4Plus = false;
1521-
if (Clazz.isSafari) {
1522-
var ua = navigator.userAgent;
1523-
var verIdx = ua.indexOf ("Version/");
1524-
if (verIdx != -1) {
1525-
var verStr = ua.substring (verIdx + 8);
1526-
var verNumber = parseFloat (verStr);
1527-
Clazz.isSafari4Plus = verNumber >= 4.0;
1527+
(function() {
1528+
if (Clazz.isSafari) {
1529+
var ua = navigator.userAgent;
1530+
var verIdx = ua.indexOf ("Version/");
1531+
if (verIdx != -1) {
1532+
var verStr = ua.substring (verIdx + 8);
1533+
var verNumber = parseFloat (verStr);
1534+
Clazz.isSafari4Plus = verNumber >= 4.0;
1535+
}
15281536
}
1529-
}
1537+
}) ();
15301538

15311539
/* protected */
15321540
Clazz.instantialize = function (objThis, args) {
@@ -1970,31 +1978,34 @@ Clazz._ex_reg=function (msg, spliterName, spliterRegex) {
19701978
regexp = new RegExp("^"+str+"$");
19711979
return regexp;
19721980
};
1973-
// reproduce NullPointerException for knowing how to detect them, and create detector function Clazz._isNPEExceptionPredicate
1974-
var $$o$$ = null;
1975-
try {
1976-
$$o$$.hello ();
1977-
} catch (e) {
1978-
if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {// opera throws an exception with fixed messages like "Statement on line 23: Cannot convert undefined or null to Object Backtrace: Line....long text... "
1979-
var idx1 = e.message.indexOf(":"), idx2 = e.message.indexOf(":", idx1+2);
1980-
Clazz._NPEMsgFragment = e.message.substr(idx1+1, idx2-idx1-20);
1981-
Clazz._isNPEExceptionPredicate = function(e) {
1982-
return e.message.indexOf(Clazz._NPEMsgFragment)!=-1;
1983-
};
1984-
}
1985-
else if(navigator.userAgent.toLowerCase().indexOf("webkit")!=-1) { //webkit, google chrome prints the property name accessed.
1986-
Clazz._exceptionNPERegExp = Clazz._ex_reg(e.message, "hello");
1987-
Clazz._isNPEExceptionPredicate = function(e) {
1988-
return Clazz._exceptionNPERegExp.test(e.message);
1989-
};
1990-
}
1991-
else {// ie, firefox and others print the name of the object accessed:
1992-
Clazz._exceptionNPERegExp = Clazz._ex_reg(e.message, "$$o$$");
1993-
Clazz._isNPEExceptionPredicate = function(e) {
1994-
return Clazz._exceptionNPERegExp.test(e.message);
1995-
};
1996-
}
1997-
};
1981+
(function() {
1982+
// reproduce NullPointerException for knowing how to detect them, and create detector function Clazz._isNPEExceptionPredicate
1983+
var $$o$$ = null;
1984+
try {
1985+
$$o$$.hello ();
1986+
} catch (e) {
1987+
if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {// opera throws an exception with fixed messages like "Statement on line 23: Cannot convert undefined or null to Object Backtrace: Line....long text... "
1988+
var idx1 = e.message.indexOf(":"), idx2 = e.message.indexOf(":", idx1+2);
1989+
Clazz._NPEMsgFragment = e.message.substr(idx1+1, idx2-idx1-20);
1990+
Clazz._isNPEExceptionPredicate = function(e) {
1991+
return e.message.indexOf(Clazz._NPEMsgFragment)!=-1;
1992+
};
1993+
}
1994+
else if(navigator.userAgent.toLowerCase().indexOf("webkit")!=-1) { //webkit, google chrome prints the property name accessed.
1995+
Clazz._exceptionNPERegExp = Clazz._ex_reg(e.message, "hello");
1996+
Clazz._isNPEExceptionPredicate = function(e) {
1997+
return Clazz._exceptionNPERegExp.test(e.message);
1998+
};
1999+
}
2000+
else {// ie, firefox and others print the name of the object accessed:
2001+
Clazz._exceptionNPERegExp = Clazz._ex_reg(e.message, "$$o$$");
2002+
Clazz._isNPEExceptionPredicate = function(e) {
2003+
return Clazz._exceptionNPERegExp.test(e.message);
2004+
};
2005+
}
2006+
}
2007+
}) ();
2008+
19982009
/**sgurin
19992010
* Implements Java's keyword "instanceof" in JavaScript's way **for exception objects**.
20002011
*

sources/net.sf.j2s.java.core/src/java/lang/ClassExt.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,14 +983,20 @@ Clazz.innerFunctions.newInstance = function () {
983983

984984
//Object.newInstance = Clazz.innerFunctions.newInstance;
985985
(function() { // BH added wrapper here
986+
Array["isArray"] = (function (nativeFun) {
987+
return function() {
988+
if (arguments.length == 0) {
989+
return true;
990+
}
991+
return nativeFun(arguments[0]);
992+
};
993+
}) (Array.isArray);
986994
var inF = Clazz.innerFunctionNames;
987995
for (var i = 0; i < inF.length; i++) {
988996
JavaObject[inF[i]] = Clazz.innerFunctions[inF[i]];
997+
if ("isArray" == inF[i]) continue;
989998
Array[inF[i]] = Clazz.innerFunctions[inF[i]];
990999
}
991-
Array["isArray"] = function () {
992-
return true;
993-
};
9941000
}) ();
9951001

9961002
/* public */

sources/net.sf.j2s.java.core/src/java/lang/ClassLoader.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,21 @@ ClazzLoader.randomlyReload = ClazzLoader.isSafari;
220220
*
221221
* FIXME: This different loading order also causes bugs in single thread!
222222
*/
223-
if (ClazzLoader.isOpera) {
224-
ClazzLoader.maxLoadingThreads = 1;
225-
var index = ClazzLoader.userAgent.indexOf ("opera/");
226-
if (index != -1) {
227-
var verNumber = 9.0;
228-
try {
229-
verNumber = parseFloat(ClazzLoader.userAgent.subString (index + 6));
230-
} catch (e) {}
231-
if (verNumber >= 9.6) {
232-
ClazzLoader.maxLoadingThreads = 6;
233-
}
234-
}
235-
}
223+
(function() {
224+
if (ClazzLoader.isOpera) {
225+
ClazzLoader.maxLoadingThreads = 1;
226+
var index = ClazzLoader.userAgent.indexOf ("opera/");
227+
if (index != -1) {
228+
var verNumber = 9.0;
229+
try {
230+
verNumber = parseFloat(ClazzLoader.userAgent.subString (index + 6));
231+
} catch (e) {}
232+
if (verNumber >= 9.6) {
233+
ClazzLoader.maxLoadingThreads = 6;
234+
}
235+
}
236+
}
237+
}) ();
236238

237239
/**
238240
* Try to be compatiable with Clazz system.

sources/net.sf.j2s.java.core/src/java/lang/ClassLoaderProgressMonitor.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,29 @@ clpm.showStatus = function (msg, fading) {
171171
this.fadeOut();
172172
}
173173
};
174-
if (window["ClazzLoader"] != null) {
175-
ClazzLoader.scriptLoading = function (file) {
176-
ClassLoaderProgressMonitor.showStatus ("Loading " + file + "...");
177-
};
178-
ClazzLoader.scriptLoaded = function (file) {
179-
ClassLoaderProgressMonitor.showStatus (file + " loaded.", true);
180-
};
181-
ClazzLoader.globalLoaded = function (file) {
182-
ClassLoaderProgressMonitor.showStatus ("Application loaded.", true);
183-
};
184-
ClazzLoader.classUnloaded = function (clazz) {
185-
ClassLoaderProgressMonitor.showStatus ("Class " + clazz + " is unloaded.", true);
186-
};
187-
ClazzLoader.classReloaded = function (clazz) {
188-
ClassLoaderProgressMonitor.showStatus ("Class " + clazz + " is reloaded.", true);
189-
};
190-
191-
var ua = navigator.userAgent.toLowerCase ();
192-
if (ua.indexOf ("msie") != -1 && ua.indexOf ("opera") == -1) {
193-
ClazzLoader.setLoadingMode ("script", 5);
174+
(function() {
175+
if (window["ClazzLoader"] != null) {
176+
ClazzLoader.scriptLoading = function (file) {
177+
ClassLoaderProgressMonitor.showStatus ("Loading " + file + "...");
178+
};
179+
ClazzLoader.scriptLoaded = function (file) {
180+
ClassLoaderProgressMonitor.showStatus (file + " loaded.", true);
181+
};
182+
ClazzLoader.globalLoaded = function (file) {
183+
ClassLoaderProgressMonitor.showStatus ("Application loaded.", true);
184+
};
185+
ClazzLoader.classUnloaded = function (clazz) {
186+
ClassLoaderProgressMonitor.showStatus ("Class " + clazz + " is unloaded.", true);
187+
};
188+
ClazzLoader.classReloaded = function (clazz) {
189+
ClassLoaderProgressMonitor.showStatus ("Class " + clazz + " is reloaded.", true);
190+
};
191+
192+
var ua = navigator.userAgent.toLowerCase ();
193+
if (ua.indexOf ("msie") != -1 && ua.indexOf ("opera") == -1) {
194+
ClazzLoader.setLoadingMode ("script", 5);
195+
}
194196
}
195-
}
197+
}) ();
198+
196199
}

sources/net.sf.j2s.java.core/src/java/lang/Number.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
Clazz.load (["java.io.Serializable"], "java.lang.Number", null, function () {
22
java.lang.Number = Number;
3-
if (Clazz.supportsNativeObject) {
4-
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
5-
var p = Clazz.extendedObjectMethods[i];
6-
Number.prototype[p] = JavaObject.prototype[p];
3+
Number.prototype.to$tring = Number.prototype.toString;
4+
(function() {
5+
if (Clazz.supportsNativeObject) {
6+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
7+
var p = Clazz.extendedObjectMethods[i];
8+
if (p == "toString" || p == "to$tring") continue;
9+
Number.prototype[p] = JavaObject.prototype[p];
10+
}
711
}
8-
}
12+
}) ();
913
//Clazz.decorateAsType (Number, "Number", null, java.io.Serializable, null, true);
1014
Number.__CLASS_NAME__ = "Number";
1115
Clazz.implementOf (Number, java.io.Serializable);
@@ -44,12 +48,11 @@ function () {
4448
return this.valueOf ();
4549
});
4650

47-
Number.prototype.to$tring = Number.prototype.toString;
4851
//sgurin : added this because if not, a native number in native code will print as [Object Number] instead printing the number value...
4952
Clazz.overrideMethod (Number, "toString",
5053
function () {
5154
if (arguments.length > 0) {
52-
return this.to$tring (arguments[0]);
55+
return Number.prototype.to$tring.apply (this, arguments);
5356
} else {
5457
return this.valueOf () + "";
5558
}

sources/net.sf.j2s.java.core/src/java/lang/String.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
Clazz.load (["java.lang.CharSequence", "$.Comparable", "java.io.Serializable", "java.util.Comparator"], "java.lang.String", null, function () {
22
if (String.prototype.$replace == null) { // not defined yet! ClazzLoader may try to load this twice!
33
java.lang.String = String;
4-
if (Clazz.supportsNativeObject) {
5-
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
6-
var p = Clazz.extendedObjectMethods[i];
7-
if ("to$tring" == p || "toString" == p || "equals" == p || "hashCode" == p) {
8-
continue;
4+
(function() {
5+
if (Clazz.supportsNativeObject) {
6+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
7+
var p = Clazz.extendedObjectMethods[i];
8+
if ("to$tring" == p || "toString" == p || "equals" == p || "hashCode" == p) {
9+
continue;
10+
}
11+
String.prototype[p] = JavaObject.prototype[p];
912
}
10-
String.prototype[p] = JavaObject.prototype[p];
1113
}
12-
}
14+
}) ();
1315
//Clazz.decorateAsType (String, "String", null, [java.io.Serializable, CharSequence, Comparable]);
1416
Clazz.implementOf (String, [java.io.Serializable, CharSequence, Comparable]);
1517
//Number.equals = Clazz.innerFunctions.equals;

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/package.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,7 @@ if (ClazzLoader.classpathMap["@org.eclipse.swt"] != null) {
621621
/* private */
622622
window["org.eclipse.swt.registered"] = true;
623623

624-
var lazyCBKey = "swt.lazy.loading.callback";
625-
if (window[lazyCBKey] != null) {
626-
window[lazyCBKey]();
627-
window[lazyCBKey] = null;
624+
if (window["swt.lazy.loading.callback"] != null) {
625+
window["swt.lazy.loading.callback"]();
626+
window["swt.lazy.loading.callback"] = null;
628627
}

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Text.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
font-family:Tahoma, Arial, sans-serif;
1717
font-size:8pt;
1818
box-sizing: content-box !important;
19+
resize:none;
1920
}
2021
.text-editable textarea {
2122
/*background-color:white;*/

0 commit comments

Comments
 (0)