Improve error handling in coroutines.

This commit is contained in:
James Roseborough
2010-04-19 16:11:55 +00:00
parent fdea0828fc
commit 4f3cd304a1

View File

@@ -36,6 +36,7 @@ public class LuaThread extends LuaValue implements Runnable {
private static final int STATUS_RUNNING = 1; private static final int STATUS_RUNNING = 1;
private static final int STATUS_NORMAL = 2; private static final int STATUS_NORMAL = 2;
private static final int STATUS_DEAD = 3; private static final int STATUS_DEAD = 3;
private static final int STATUS_ERROR = 4;
private static final String[] STATUS_NAMES = { private static final String[] STATUS_NAMES = {
"suspended", "suspended",
"running", "running",
@@ -158,8 +159,12 @@ public class LuaThread extends LuaValue implements Runnable {
synchronized ( this ) { synchronized ( this ) {
try { try {
this.args = func.invoke(this.args); this.args = func.invoke(this.args);
} finally {
status = STATUS_DEAD; status = STATUS_DEAD;
} catch ( Throwable t ) {
String msg = t.getMessage();
this.args = valueOf(msg!=null? msg: t.toString());
status = STATUS_ERROR;
} finally {
this.notify(); this.notify();
} }
} }
@@ -214,7 +219,12 @@ public class LuaThread extends LuaValue implements Runnable {
this.wait(); this.wait();
// copy return values from yielding stack state // copy return values from yielding stack state
return varargsOf(LuaValue.TRUE, this.args); if ( status == STATUS_ERROR ) {
status = STATUS_DEAD;
return varargsOf(FALSE, this.args);
} else {
return varargsOf(TRUE, this.args);
}
} catch ( Throwable t ) { } catch ( Throwable t ) {
status = STATUS_DEAD; status = STATUS_DEAD;