Support for Thread.UncaughtExceptionHandler (#483)

This commit is contained in:
Jörg Hohwiller 2020-03-10 09:28:02 +01:00 committed by GitHub
parent a152a28a2d
commit a45d1e0d07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,29 @@
/*
* Copyright 2020 by Joerg Hohwiller
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.lang;
import org.teavm.classlib.java.lang.TThread.UncaughtExceptionHandler;
public class TDefaultUncaughtExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(TThread t, Throwable e) {
// print to browser console by default
e.printStackTrace();
}
}

View File

@ -28,6 +28,8 @@ public class TThread extends TObject implements TRunnable {
private static TThread currentThread = mainThread; private static TThread currentThread = mainThread;
private static int nextId = 1; private static int nextId = 1;
private static int activeCount = 1; private static int activeCount = 1;
private static UncaughtExceptionHandler defaultUncaughtExceptionHandler = new TDefaultUncaughtExceptionHandler();
private UncaughtExceptionHandler uncaughtExceptionHandler;
private long id; private long id;
private int priority; private int priority;
private boolean daemon; private boolean daemon;
@ -76,6 +78,8 @@ public class TThread extends TObject implements TRunnable {
activeCount++; activeCount++;
setCurrentThread(TThread.this); setCurrentThread(TThread.this);
TThread.this.run(); TThread.this.run();
} catch (Throwable t) {
getUncaughtExceptionHandler().uncaughtException(this, t);
} finally { } finally {
synchronized (finishedLock) { synchronized (finishedLock) {
finishedLock.notifyAll(); finishedLock.notifyAll();
@ -269,4 +273,27 @@ public class TThread extends TObject implements TRunnable {
public TClassLoader getContextClassLoader() { public TClassLoader getContextClassLoader() {
return TClassLoader.getSystemClassLoader(); return TClassLoader.getSystemClassLoader();
} }
public UncaughtExceptionHandler getUncaughtExceptionHandler() {
if (this.uncaughtExceptionHandler != null) {
return this.uncaughtExceptionHandler;
}
return defaultUncaughtExceptionHandler;
}
public void setUncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) {
this.uncaughtExceptionHandler = uncaughtExceptionHandler;
}
public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler() {
return defaultUncaughtExceptionHandler;
}
public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler handler) {
defaultUncaughtExceptionHandler = handler;
}
public interface UncaughtExceptionHandler {
void uncaughtException(TThread t, Throwable e);
}
} }