Skip to content

Commit ea79a2c

Browse files
hansonrhansonr
authored andcommitted
enables ThreadGroup
fixes System.exit(0) moves JSUtil.getAppletViewer() to JSToolkit
1 parent 08e3ec1 commit ea79a2c

File tree

11 files changed

+286
-223
lines changed

11 files changed

+286
-223
lines changed

sources/net.sf.j2s.java.core/src/java/awt/Window.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@
5252

5353
import sun.awt.AppContext;
5454
import swingjs.JSToolkit;
55-
//import java.util.concurrent.atomic.AtomicBoolean;
56-
//import java.util.logging.Logger;
57-
import swingjs.JSUtil;
5855
import swingjs.plaf.JSComponentUI;
5956
import swingjs.plaf.JSWindowUI;
6057

@@ -750,7 +747,7 @@ public void addNotify() {
750747
if (parent != null && parent.getPeer() == null)
751748
parent.addNotify();
752749
getOrCreatePeer();
753-
JSUtil.getAppletViewer().addWindow(this);
750+
JSToolkit.getAppletViewer().addWindow(this);
754751
// to JComponent now
755752
super.addNotify();
756753
}
@@ -765,7 +762,7 @@ protected ComponentPeer getOrCreatePeer() {
765762
*/
766763
@Override
767764
public void removeNotify() {
768-
JSUtil.getAppletViewer().allWindows.removeObj(this);
765+
JSToolkit.getAppletViewer().allWindows.removeObj(this);
769766
super.removeNotify();
770767
}
771768

@@ -1484,14 +1481,14 @@ public Dialog getModalBlocker() {
14841481
static ArrayList<Window> getAllWindows() {
14851482
// synchronized (allWindows) {
14861483
ArrayList<Window> v = new ArrayList<Window>();
1487-
v.addAll(JSUtil.getAppletViewer().allWindows);
1484+
v.addAll(JSToolkit.getAppletViewer().allWindows);
14881485
return v;
14891486
// }
14901487
}
14911488

14921489
static ArrayList<Window> getAllUnblockedWindows() {
14931490
// synchronized (allWindows) {
1494-
ArrayList<Window> allWindows = JSUtil.getAppletViewer().allWindows;
1491+
ArrayList<Window> allWindows = JSToolkit.getAppletViewer().allWindows;
14951492
ArrayList<Window> unblocked = new ArrayList<Window>();
14961493
for (int i = 0; i < allWindows.size(); i++) {
14971494
Window w = allWindows.get(i);

sources/net.sf.j2s.java.core/src/java/lang/Thread.java

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//import sun.reflect.CallerSensitive;
4343
//import sun.reflect.Reflection;
4444
//import sun.security.util.SecurityConstants;
45-
45+
import swingjs.JSThreadGroup;
4646
import swingjs.JSToolkit;
4747
import swingjs.JSUtil;
4848

@@ -207,7 +207,8 @@ private static synchronized int nextThreadNum() {
207207

208208
/* For generating thread ID */
209209
private static long threadSeqNumber;
210-
public static Thread thisThread;
210+
211+
public static JSThread 秘thisThread;
211212

212213
/* Java thread status for tools,
213214
* initialized to indicate thread 'not yet started'
@@ -275,51 +276,34 @@ private static synchronized long nextThreadID() {
275276
* @return the currently executing thread.
276277
*/
277278
public static Thread currentThread() {
278-
/**
279-
* @j2sNative
280-
*if (java.lang.Thread.thisThread === "working")
281-
* return null;
282-
*
283-
*
284-
*/
285-
{}
286-
if (thisThread == null) {
287-
/**
288-
*
289-
* technically, JSThread is an abstract class, but we go ahead and
290-
* instantialize it anyway in JavaScript. The reason it is abstract
291-
* is to force generation of necessary methods when using it.
292-
*
293-
* @j2sNative
294-
*
295-
*
296-
* java.lang.Thread.thisThread = "working";
297-
* java.lang.Thread.thisThread =
298-
* Clazz.new_(java.lang.Thread.c$$S, ["master"]);
299-
* var name = J2S._applets["master"]._id; var g =
300-
* Clazz.new_(Clazz.load('swingjs.JSThreadGroup').c$$ThreadGroup$S,
301-
* [null, name]); java.lang.Thread.thisThread =
302-
* Clazz.new_(Clazz.load("javajs.util.JSThread").c$$ThreadGroup$S,
303-
* [g, name]);
304-
*
305-
*/
306-
{
307-
// these are for reference only -- not used in JavaScript
308-
JSThread.interrupted();
309-
new swingjs.JSThreadGroup(null, null);
310-
thisThread = new Thread("master");
311-
}
312-
thisThread.setPriority(NORM_PRIORITY);
279+
Thread t = 秘thisThread;
280+
if ((Object) t == "working")
281+
return null;
282+
if (t == null) {
283+
秘thisThread = (JSThread) (Object) "working";
284+
秘thisThread = (JSThread) new Thread("master");
285+
String name = /** @j2sNative J2S._applets["master"]._id || */null;
286+
@SuppressWarnings("unused")
287+
JSThreadGroup g = new JSThreadGroup(null, name);
288+
秘thisThread = /** @j2sNative
289+
Clazz.new_(Clazz.load("javajs.util.JSThread").c$$ThreadGroup$S,
290+
[g, name]) || */ null;
291+
292+
// new JSThread(g, name);
293+
// technically, JSThread is an abstract class, but we go ahead and
294+
// instantialize it anyway in JavaScript. The reason it is abstract
295+
// is to force generation of necessary methods when using it.
296+
秘thisThread.setPriority(NORM_PRIORITY);
313297
}
314-
return thisThread;
298+
return 秘thisThread;
315299
}
316300

317301
/**
318302
* Causes the currently executing thread object to temporarily pause
319303
* and allow other threads to execute.
320304
*/
321305
public static void yield() {
322-
// SwingJS ;
306+
JSUtil.notImplemented(null);
323307
}
324308

325309
/**
@@ -335,7 +319,8 @@ public static void yield() {
335319
* @see Object#notify()
336320
*/
337321
public static void sleep(long millis) throws InterruptedException {
338-
// SwingJS ;
322+
JSUtil.notImplemented(null);
323+
JSUtil.warn("SwingJS does not implement Thread.sleep(long)");
339324
}
340325
/**
341326
* Causes the currently executing thread to sleep (cease execution)
@@ -368,8 +353,9 @@ public static void sleep(long millis, int nanos)
368353
if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
369354
millis++;
370355
}
371-
372-
sleep(millis);
356+
JSUtil.notImplemented(null);
357+
JSUtil.warn("SwingJS does not implement Thread.sleep(long,int)");
358+
// sleep(millis);
373359
}
374360

375361
/**
@@ -394,7 +380,7 @@ private void init(ThreadGroup g, Runnable target, String name,
394380
*/
395381
private void init(ThreadGroup g, Runnable target, String name,
396382
long stackSize, Object acc) { // was access controller
397-
Thread parent = (thisThread == null ? null : thisThread);
383+
Thread parent = (秘thisThread == null ? null : 秘thisThread);
398384
// SecurityManager security = System.getSecurityManager();
399385
if (g == null) {
400386
/* Determine if it's an applet or not */
@@ -418,13 +404,13 @@ private void init(ThreadGroup g, Runnable target, String name,
418404
g = newThreadGroup(null, name);
419405
parent = this; // ??
420406
}
421-
/* checkAccess regardless of whether or not threadgroup is
422-
explicitly passed in. */
423-
g.checkAccess();
424-
425-
/*
426-
* Do we have the required permissions?
427-
*/
407+
// /* checkAccess regardless of whether or not threadgroup is
408+
// explicitly passed in. */
409+
// g.checkAccess();
410+
//
411+
// /*
412+
// * Do we have the required permissions?
413+
// */
428414
// if (security != null) {
429415
// if (isCCLOverridden(getClass())) {
430416
// security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
@@ -744,8 +730,9 @@ public synchronized void start() {
744730
}
745731

746732
private void start0(){
747-
started = true;
748-
JSToolkit.dispatch(this, 0, 0); // run the run() method asynchronously
733+
started = true;
734+
currentThread().getThreadGroup().秘transferJ2SInfo(getThreadGroup());
735+
JSToolkit.startThread(this); // run the run() method asynchronously
749736
}
750737

751738
/**
@@ -1293,28 +1280,27 @@ public int countStackFrames(){
12931280
*/
12941281
public final synchronized void join(long millis)
12951282
throws InterruptedException {
1296-
long base = System.currentTimeMillis();
1297-
long now = 0;
1283+
// long base = System.currentTimeMillis();
1284+
// long now = 0;
12981285

12991286
if (millis < 0) {
13001287
throw new IllegalArgumentException("timeout value is negative");
13011288
}
1302-
1303-
JSUtil.warn("Cannot wait in Thread");
1304-
if (millis == 0) {
1305-
while (isAlive()) {
1306-
wait(0);
1307-
}
1308-
} else {
1309-
while (isAlive()) {
1310-
long delay = millis - now;
1311-
if (delay <= 0) {
1312-
break;
1313-
}
1314-
wait(delay);
1315-
now = System.currentTimeMillis() - base;
1316-
}
1317-
}
1289+
JSUtil.notImplemented(null);
1290+
// if (millis == 0) {
1291+
// while (isAlive()) {
1292+
// wait(0);
1293+
// }
1294+
// } else {
1295+
// while (isAlive()) {
1296+
// long delay = millis - now;
1297+
// if (delay <= 0) {
1298+
// break;
1299+
// }
1300+
// wait(delay);
1301+
// now = System.currentTimeMillis() - base;
1302+
// }
1303+
// }
13181304
}
13191305

13201306
/**

sources/net.sf.j2s.java.core/src/java/lang/ThreadGroup.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ThreadGroup implements Thread.UncaughtExceptionHandler {
7878
private int ngroups;
7979
private ThreadGroup groups[];
8080
protected HTML5Applet 秘html5Applet;
81-
public boolean 秘systemExited;
81+
public boolean 秘systemExited;
8282
public JSAppletViewer 秘appletViewer;
8383

8484
private ArrayList<Object> 秘timerQueue;
@@ -1066,4 +1066,10 @@ public String toString() {
10661066
}
10671067
秘systemExited = true;
10681068
}
1069+
1070+
public void 秘transferJ2SInfo(ThreadGroup g) {
1071+
g.秘appletViewer = 秘appletViewer;
1072+
g.秘html5Applet = 秘html5Applet;
1073+
g.秘timerQueue = 秘timerQueue;
1074+
}
10691075
}

sources/net.sf.j2s.java.core/src/javax/swing/SwingUtilities.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import sun.swing.SwingUtilities2;
6464
import sun.swing.UIAction;
6565
import swingjs.JSAppletViewer;
66-
import swingjs.JSUtil;
66+
import swingjs.JSToolkit;
6767
import swingjs.api.Interface;
6868
import swingjs.api.js.DOMNode;
6969
import swingjs.plaf.JSComponentUI;
@@ -1887,7 +1887,7 @@ public void dispose() {
18871887
* @see java.awt.GraphicsEnvironment#isHeadless
18881888
*/
18891889
public static JSFrame getSharedOwnerFrame() {
1890-
JSAppletViewer p = JSUtil.getAppletViewer();
1890+
JSAppletViewer p = JSToolkit.getAppletViewer();
18911891
JSFrame f = p.sharedOwnerFrame;
18921892
return (f == null ? (p.sharedOwnerFrame = new SharedOwnerFrame()) : f);
18931893
}

sources/net.sf.j2s.java.core/src/swingjs/JSAppletViewer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private void setDisplayParams(Hashtable<String, Object> params) {
186186

187187
threadGroup = new JSThreadGroup(appletName);
188188
myThread = new JSAppletThread(this, threadGroup, appletName);
189-
java.lang.Thread.thisThread = (java.lang.Thread) ((Object) myThread);
189+
java.lang.Thread.秘thisThread = myThread;
190190

191191
appContext = JSToolkit.createNewAppContext();
192192
// initialize toolkit and graphics configuration

0 commit comments

Comments
 (0)