From a529cbaf5c535094cb811d7c4d638adc480c89a2 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Sun, 30 Sep 2012 05:52:06 +0000 Subject: [PATCH] Improve compatibility with lua 5.2. --- src/core/org/luaj/vm2/LuaThread.java | 11 +++++ src/core/org/luaj/vm2/lib/DebugLib.java | 66 ++++++++++++------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/core/org/luaj/vm2/LuaThread.java b/src/core/org/luaj/vm2/LuaThread.java index fce6a5a3..32071c1b 100644 --- a/src/core/org/luaj/vm2/LuaThread.java +++ b/src/core/org/luaj/vm2/LuaThread.java @@ -95,6 +95,17 @@ public class LuaThread extends LuaValue { 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 */ public LuaThread(Globals globals) { state = new State(globals, this, null); diff --git a/src/core/org/luaj/vm2/lib/DebugLib.java b/src/core/org/luaj/vm2/lib/DebugLib.java index a528d711..0b2635fc 100644 --- a/src/core/org/luaj/vm2/lib/DebugLib.java +++ b/src/core/org/luaj/vm2/lib/DebugLib.java @@ -37,7 +37,6 @@ import org.luaj.vm2.LuaValue; import org.luaj.vm2.Print; import org.luaj.vm2.Prototype; import org.luaj.vm2.Varargs; -import org.luaj.vm2.lib.DebugLib.CallFrame; /** * Subclass of {@link LibFunction} which implements the lua standard {@code debug} @@ -95,15 +94,6 @@ public class DebugLib extends OneArgFunction { Globals globals; - LuaValue hookfunc; - boolean hookline; - boolean hookcall; - boolean hookrtrn; - int hookcount; - boolean inhook; - int lastline; - int bytecodes; - public LuaValue call(LuaValue env) { globals = env.checkglobals(); globals.debuglib = this; @@ -139,10 +129,11 @@ public class DebugLib extends OneArgFunction { // debug.gethook ([thread]) final class gethook extends VarArgFunction { public Varargs invoke(Varargs args) { + LuaThread t = args.narg() > 0 ? args.checkthread(1): globals.running_thread; return varargsOf( - hookfunc != null? hookfunc: NIL, - valueOf((hookcall?"c":"")+(hookline?"l":"")+(hookrtrn?"r":"")), - valueOf(hookcount)); + t.hookfunc != null? t.hookfunc: NIL, + valueOf((t.hookcall?"c":"")+(t.hookline?"l":"")+(t.hookrtrn?"r":"")), + valueOf(t.hookcount)); } } @@ -264,7 +255,7 @@ public class DebugLib extends OneArgFunction { final class sethook extends VarArgFunction { public Varargs invoke(Varargs args) { 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); String str = args.optjstring(a++,""); int count = args.optint(a++,0); @@ -275,11 +266,11 @@ public class DebugLib extends OneArgFunction { case 'l': line=true; break; case 'r': rtrn=true; break; } - hookfunc = func; - hookcall = call; - hookline = line; - hookcount = count; - hookrtrn = rtrn; + t.hookfunc = func; + t.hookcall = call; + t.hookline = line; + t.hookcount = count; + t.hookrtrn = rtrn; return NONE; } } @@ -388,39 +379,43 @@ public class DebugLib extends OneArgFunction { } public void onCall(LuaFunction f) { - if (inhook) return; + LuaThread t = globals.running_thread; + if (t.inhook) return; callstack().onCall(f); - if (hookcall && hookfunc != null) + if (t.hookcall && t.hookfunc != null) callHook(CALL, NIL); } 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); - if (hookcall && hookfunc != null) + if (t.hookcall && t.hookfunc != null) callHook(CALL, NIL); } 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); - if (hookfunc == null) return; - if (hookcount > 0) - if (++bytecodes % hookcount == 0) + if (t.hookfunc == null) return; + if (t.hookcount > 0) + if (++t.bytecodes % t.hookcount == 0) callHook(COUNT, NIL); - if (hookline) { + if (t.hookline) { int newline = callstack().currentline(); - if ( newline != lastline ) { - lastline = newline; + if ( newline != t.lastline ) { + t.lastline = newline; callHook(LINE, LuaValue.valueOf(newline)); } } } public void onReturn() { - if (inhook) return; + LuaThread t = globals.running_thread; + if (t.inhook) return; callstack().onReturn(); - if (hookcall && hookfunc != null) + if (t.hookcall && t.hookfunc != null) callHook(RETURN, NIL); } @@ -429,13 +424,14 @@ public class DebugLib extends OneArgFunction { } void callHook(LuaValue type, LuaValue arg) { - inhook = true; + LuaThread t = globals.running_thread; + t.inhook = true; try { - hookfunc.call(type, arg); + t.hookfunc.call(type, arg); } catch (Exception e) { e.printStackTrace(); } finally { - inhook = false; + t.inhook = false; } }