Improve debug hooks to match C behavior more closely.

This commit is contained in:
James Roseborough
2009-09-10 00:13:40 +00:00
parent 9cad7f1001
commit 66873ff268
2 changed files with 46 additions and 40 deletions

View File

@@ -311,7 +311,7 @@ public class DebugLib extends LFunction {
LString name = (p!=null? p.getlocalname(local, ci.currentpc()): null); LString name = (p!=null? p.getlocalname(local, ci.currentpc()): null);
if ( name != null ) { if ( name != null ) {
threadVm.stack[ci.base+(local-1)] = value; threadVm.stack[ci.base+(local-1)] = value;
vm.pushlvalue(name); vm.pushlvalue( name );
} else { } else {
vm.pushnil(); vm.pushnil();
} }
@@ -413,14 +413,15 @@ public class DebugLib extends LFunction {
* @return { name, value } or null if not found. * @return { name, value } or null if not found.
*/ */
public static LValue[] getlocal(LuaState vm, CallInfo ci, int local) { public static LValue[] getlocal(LuaState vm, CallInfo ci, int local) {
LPrototype p = ci.closure.p; LPrototype p = (ci!=null? ci.closure.p: null);
LString name = p.getlocalname(local, ci.currentpc()); LString name = (p!=null? p.getlocalname(local, ci.currentpc()): null);
if ( name != null ) { if ( name != null ) {
LValue value = vm.stack[ci.base+(local-1)]; LValue value = vm.stack[ci.base+(local-1)];
return new LValue[] { name, value }; return new LValue[] { name, value };
} } else {
return null; return null;
} }
}
/** /**
* @param c the LClosure to inspect * @param c the LClosure to inspect

View File

@@ -248,7 +248,20 @@ public class LuaState extends Lua {
*/ */
public void invokeJavaFunction(LFunction javaFunction) { public void invokeJavaFunction(LFunction javaFunction) {
++base; ++base;
// call hook
if ( hooksenabled && !inhook ) {
debugCallHooks( );
}
int nactual = javaFunction.invoke(this); int nactual = javaFunction.invoke(this);
// call hook
if ( hooksenabled && !inhook ) {
debugReturnHooks( );
}
if (nactual < 0) if (nactual < 0)
nactual = top - base; nactual = top - base;
System.arraycopy(stack, top-nactual, stack, --base, nactual); System.arraycopy(stack, top-nactual, stack, --base, nactual);
@@ -325,7 +338,7 @@ public class LuaState extends Lua {
if (this.stack[base].luaStackCall(this)) { if (this.stack[base].luaStackCall(this)) {
// call hook // call hook
if ( hooksenabled ) { if ( hooksenabled && ! inhook ) {
debugCallHooks( ); debugCallHooks( );
} }
@@ -576,7 +589,7 @@ public class LuaState extends Lua {
// allow debug hooks a chance to operate // allow debug hooks a chance to operate
debugHooks( ci.pc ); debugHooks( ci.pc );
if ( hooksenabled ) { if ( hooksenabled && ! inhook ) {
//Print.printState(this, base, top, base+cl.p.maxstacksize, cl, ci.pc); //Print.printState(this, base, top, base+cl.p.maxstacksize, cl, ci.pc);
debugBytecodeHooks( ci.pc ); debugBytecodeHooks( ci.pc );
} }
@@ -754,7 +767,7 @@ public class LuaState extends Lua {
if (this.stack[base].luaStackCall(this)) { if (this.stack[base].luaStackCall(this)) {
// call hook // call hook
if ( hooksenabled ) { if ( hooksenabled && ! inhook ) {
debugCallHooks( ); debugCallHooks( );
} }
@@ -770,18 +783,12 @@ public class LuaState extends Lua {
// restore base // restore base
base = ci.base; base = ci.base;
// call hook
if ( hooksenabled ) {
debugReturnHooks( );
}
continue; continue;
} }
case LuaState.OP_TAILCALL: { case LuaState.OP_TAILCALL: {
// return hook // return hook
if ( hooksenabled ) { if ( hooksenabled && ! inhook ) {
debugTailReturnHooks( ); debugTailReturnHooks( );
} }
@@ -809,7 +816,7 @@ public class LuaState extends Lua {
if (this.stack[base].luaStackCall(this)) { if (this.stack[base].luaStackCall(this)) {
// call hook // call hook
if ( hooksenabled ) { if ( hooksenabled && ! inhook ) {
debugCallHooks( ); debugCallHooks( );
} }
@@ -835,7 +842,7 @@ public class LuaState extends Lua {
case LuaState.OP_RETURN: { case LuaState.OP_RETURN: {
// return hook // return hook
if ( hooksenabled ) { if ( hooksenabled && ! inhook ) {
debugReturnHooks( ); debugReturnHooks( );
} }
@@ -2389,20 +2396,17 @@ public class LuaState extends Lua {
*/ */
public String luaV_call_errfunc(String message) { public String luaV_call_errfunc(String message) {
// error function is run at most once // error function runs without hooks
if ( errfunc == null ) if ( inhook || errfunc == null )
return message; return message;
// run the error function // run the error function
int oldtop = top; int oldtop = top;
int oldmask = hookmask;
LValue olderr = errfunc;
try { try {
hookmask = 0; inhook = true;
errfunc = null;
if ( cc >= 0 ) if ( cc >= 0 )
top = base + this.calls[cc].closure.p.maxstacksize; top = base + this.calls[cc].closure.p.maxstacksize;
pushlvalue(olderr); pushlvalue(errfunc);
pushstring(message); pushstring(message);
call(1,1); call(1,1);
return poplvalue().toJavaString(); return poplvalue().toJavaString();
@@ -2412,8 +2416,7 @@ public class LuaState extends Lua {
} finally { } finally {
top = oldtop; top = oldtop;
hookmask = oldmask; inhook = false;
errfunc = olderr;
} }
} }
@@ -2432,7 +2435,7 @@ public class LuaState extends Lua {
* @param count 0, or number of bytecodes between count events. * @param count 0, or number of bytecodes between count events.
*/ */
public void sethook( LFunction func, int mask, int count ) { public void sethook( LFunction func, int mask, int count ) {
hooksenabled = (mask != 0); hooksenabled = (mask != 0 || count > 0);
hookfunc = func; hookfunc = func;
hookmask = mask; hookmask = mask;
hookcount = count; hookcount = count;
@@ -2468,17 +2471,19 @@ public class LuaState extends Lua {
// line number and count hooks // line number and count hooks
private void debugBytecodeHooks(int pc) { private void debugBytecodeHooks(int pc) {
if ( hookfunc != null && (hookmask & LUA_MASKLINE) != 0 ) { if ( hookfunc != null ) {
if (hookcount != 0) {
if ( --hookincr <= 0 ) {
hookincr = hookcount;
debugInvokeHook(LUA_HOOKCOUNT, -1);
}
}
if ( (hookmask & LUA_MASKLINE) != 0 ) {
int line = calls[cc].currentline(); int line = calls[cc].currentline();
if ( (line != hookline || cc != hookcc) && line >= 0 ) { if ( (line != hookline || cc != hookcc) && line >= 0 ) {
hookline = line; hookline = line;
hookcc = cc; hookcc = cc;
debugCallHook(LUA_HOOKLINE, line); debugInvokeHook(LUA_HOOKLINE, line);
}
if (hookcount != 0) {
if ( --hookincr <= 0 ) {
hookincr = hookcount;
debugCallHook(LUA_HOOKCOUNT, -1);
} }
} }
} }
@@ -2486,23 +2491,23 @@ public class LuaState extends Lua {
private void debugCallHooks() { private void debugCallHooks() {
if ( hookfunc != null && ((hookmask & LUA_MASKCALL) != 0) ) { if ( hookfunc != null && ((hookmask & LUA_MASKCALL) != 0) ) {
debugCallHook(LUA_HOOKCALL, calls[cc].currentline()); debugInvokeHook(LUA_HOOKCALL, calls[cc].currentline());
} }
} }
private void debugReturnHooks() { private void debugReturnHooks() {
if ( hookfunc != null && ((hookmask & LUA_MASKRET) != 0) ) { if ( hookfunc != null && ((hookmask & LUA_MASKRET) != 0) ) {
debugCallHook(LUA_HOOKRET, calls[cc].currentline()); debugInvokeHook(LUA_HOOKRET, calls[cc].currentline());
} }
} }
private void debugTailReturnHooks() { private void debugTailReturnHooks() {
if ( hookfunc != null && ((hookmask & LUA_MASKRET) != 0) ) { if ( hookfunc != null && ((hookmask & LUA_MASKRET) != 0) ) {
debugCallHook(LUA_HOOKTAILRET, calls[cc].currentline()); debugInvokeHook(LUA_HOOKTAILRET, calls[cc].currentline());
} }
} }
private void debugCallHook(int mask, int line) { private void debugInvokeHook(int mask, int line) {
if ( inhook ) if ( inhook )
return; return;
int oldtop = top; int oldtop = top;