Improve compatibility with lua 5.2.

This commit is contained in:
James Roseborough
2012-09-30 05:52:06 +00:00
parent b6f33f6e2e
commit a529cbaf5c
2 changed files with 42 additions and 35 deletions

View File

@@ -95,6 +95,17 @@ public class LuaThread extends LuaValue {
public final Globals globals; public final Globals globals;
// Hook function control state used by debug lib.
public LuaValue hookfunc;
public boolean hookline;
public boolean hookcall;
public boolean hookrtrn;
public int hookcount;
public boolean inhook;
public int lastline;
public int bytecodes;
/** Private constructor for main thread only */ /** Private constructor for main thread only */
public LuaThread(Globals globals) { public LuaThread(Globals globals) {
state = new State(globals, this, null); state = new State(globals, this, null);

View File

@@ -37,7 +37,6 @@ import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Print; import org.luaj.vm2.Print;
import org.luaj.vm2.Prototype; import org.luaj.vm2.Prototype;
import org.luaj.vm2.Varargs; import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.DebugLib.CallFrame;
/** /**
* Subclass of {@link LibFunction} which implements the lua standard {@code debug} * Subclass of {@link LibFunction} which implements the lua standard {@code debug}
@@ -95,15 +94,6 @@ public class DebugLib extends OneArgFunction {
Globals globals; Globals globals;
LuaValue hookfunc;
boolean hookline;
boolean hookcall;
boolean hookrtrn;
int hookcount;
boolean inhook;
int lastline;
int bytecodes;
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue env) {
globals = env.checkglobals(); globals = env.checkglobals();
globals.debuglib = this; globals.debuglib = this;
@@ -139,10 +129,11 @@ public class DebugLib extends OneArgFunction {
// debug.gethook ([thread]) // debug.gethook ([thread])
final class gethook extends VarArgFunction { final class gethook extends VarArgFunction {
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
LuaThread t = args.narg() > 0 ? args.checkthread(1): globals.running_thread;
return varargsOf( return varargsOf(
hookfunc != null? hookfunc: NIL, t.hookfunc != null? t.hookfunc: NIL,
valueOf((hookcall?"c":"")+(hookline?"l":"")+(hookrtrn?"r":"")), valueOf((t.hookcall?"c":"")+(t.hookline?"l":"")+(t.hookrtrn?"r":"")),
valueOf(hookcount)); valueOf(t.hookcount));
} }
} }
@@ -264,7 +255,7 @@ public class DebugLib extends OneArgFunction {
final class sethook extends VarArgFunction { final class sethook extends VarArgFunction {
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
int a=1; int a=1;
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread; LuaThread t = args.isthread(a)? args.checkthread(a++): globals.running_thread;
LuaValue func = args.optfunction(a++, null); LuaValue func = args.optfunction(a++, null);
String str = args.optjstring(a++,""); String str = args.optjstring(a++,"");
int count = args.optint(a++,0); int count = args.optint(a++,0);
@@ -275,11 +266,11 @@ public class DebugLib extends OneArgFunction {
case 'l': line=true; break; case 'l': line=true; break;
case 'r': rtrn=true; break; case 'r': rtrn=true; break;
} }
hookfunc = func; t.hookfunc = func;
hookcall = call; t.hookcall = call;
hookline = line; t.hookline = line;
hookcount = count; t.hookcount = count;
hookrtrn = rtrn; t.hookrtrn = rtrn;
return NONE; return NONE;
} }
} }
@@ -388,39 +379,43 @@ public class DebugLib extends OneArgFunction {
} }
public void onCall(LuaFunction f) { public void onCall(LuaFunction f) {
if (inhook) return; LuaThread t = globals.running_thread;
if (t.inhook) return;
callstack().onCall(f); callstack().onCall(f);
if (hookcall && hookfunc != null) if (t.hookcall && t.hookfunc != null)
callHook(CALL, NIL); callHook(CALL, NIL);
} }
public void onCall(LuaClosure c, Varargs varargs, LuaValue[] stack) { public void onCall(LuaClosure c, Varargs varargs, LuaValue[] stack) {
if (inhook) return; LuaThread t = globals.running_thread;
if (t.inhook) return;
callstack().onCall(c, varargs, stack); callstack().onCall(c, varargs, stack);
if (hookcall && hookfunc != null) if (t.hookcall && t.hookfunc != null)
callHook(CALL, NIL); callHook(CALL, NIL);
} }
public void onInstruction(int pc, Varargs v, int top) { public void onInstruction(int pc, Varargs v, int top) {
if (inhook) return; LuaThread t = globals.running_thread;
if (t.inhook) return;
callstack().onInstruction(pc, v, top); callstack().onInstruction(pc, v, top);
if (hookfunc == null) return; if (t.hookfunc == null) return;
if (hookcount > 0) if (t.hookcount > 0)
if (++bytecodes % hookcount == 0) if (++t.bytecodes % t.hookcount == 0)
callHook(COUNT, NIL); callHook(COUNT, NIL);
if (hookline) { if (t.hookline) {
int newline = callstack().currentline(); int newline = callstack().currentline();
if ( newline != lastline ) { if ( newline != t.lastline ) {
lastline = newline; t.lastline = newline;
callHook(LINE, LuaValue.valueOf(newline)); callHook(LINE, LuaValue.valueOf(newline));
} }
} }
} }
public void onReturn() { public void onReturn() {
if (inhook) return; LuaThread t = globals.running_thread;
if (t.inhook) return;
callstack().onReturn(); callstack().onReturn();
if (hookcall && hookfunc != null) if (t.hookcall && t.hookfunc != null)
callHook(RETURN, NIL); callHook(RETURN, NIL);
} }
@@ -429,13 +424,14 @@ public class DebugLib extends OneArgFunction {
} }
void callHook(LuaValue type, LuaValue arg) { void callHook(LuaValue type, LuaValue arg) {
inhook = true; LuaThread t = globals.running_thread;
t.inhook = true;
try { try {
hookfunc.call(type, arg); t.hookfunc.call(type, arg);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
inhook = false; t.inhook = false;
} }
} }