From 643af145d355e6e30a32d3e529dc0f19e0082d40 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Tue, 24 Mar 2009 22:44:07 +0000 Subject: [PATCH] Add debug hook functions to LuaState --- src/core/org/luaj/lib/DebugLib.java | 82 +++++++------- src/core/org/luaj/vm/Lua.java | 1 - src/core/org/luaj/vm/LuaState.java | 161 ++++++++++++++++++++++++++-- src/test/res/debuglib.lua | 40 +++++++ version.properties | 2 +- 5 files changed, 238 insertions(+), 48 deletions(-) create mode 100644 src/test/res/debuglib.lua diff --git a/src/core/org/luaj/lib/DebugLib.java b/src/core/org/luaj/lib/DebugLib.java index ca916a9c..ca945683 100644 --- a/src/core/org/luaj/lib/DebugLib.java +++ b/src/core/org/luaj/lib/DebugLib.java @@ -56,25 +56,23 @@ public class DebugLib extends LFunction { private static final int DEBUG = 1; private static final int GETFENV = 2; private static final int GETHOOK = 3; - private static final int GETHOOKCOUNT = 4; - private static final int GETHOOKMASK = 5; - private static final int GETINFO = 6; - private static final int GETLOCAL = 7; - private static final int GETMETATABLE = 8; - private static final int GETREGISTRY = 9; - private static final int GETUPVALUE = 10; - private static final int SETFENV = 11; - private static final int SETHOOK = 12; - private static final int SETLOCAL = 13; - private static final int SETMETATABLE = 14; - private static final int SETUPVALUE = 15; - private static final int TRACEBACK = 16; + private static final int GETINFO = 4; + private static final int GETLOCAL = 5; + private static final int GETMETATABLE = 6; + private static final int GETREGISTRY = 7; + private static final int GETUPVALUE = 8; + private static final int SETFENV = 9; + private static final int SETHOOK = 10; + private static final int SETLOCAL = 11; + private static final int SETMETATABLE = 12; + private static final int SETUPVALUE = 13; + private static final int TRACEBACK = 14; - public static void install( LTable globals ) { + public static void install( LuaState vm ) { LTable debug = new LTable(); for (int i = 0; i < NAMES.length; i++) debug.put(NAMES[i], new DebugLib(i + 1)); - globals.put("debug", debug); + vm._G.put("debug", debug); PackageLib.setIsLoaded("debug", debug); } @@ -91,7 +89,7 @@ public class DebugLib extends LFunction { public boolean luaStackCall( LuaState vm ) { switch ( id ) { case INSTALL: - install(vm._G); + install(vm); break; case DEBUG: debug(vm); @@ -102,12 +100,6 @@ public class DebugLib extends LFunction { case GETHOOK: gethook(vm); break; - case GETHOOKCOUNT: - gethookcount(vm); - break; - case GETHOOKMASK: - gethookmask(vm); - break; case GETINFO: getinfo(vm); break; @@ -148,29 +140,37 @@ public class DebugLib extends LFunction { } private void debug(LuaState vm) { - // TODO Auto-generated method stub + // TODO: interactive console impl vm.resettop(); } private void gethook(LuaState vm) { - // TODO Auto-generated method stub + LuaState threadVm = vm; + if ( vm.gettop() >= 2 ) + threadVm = vm.checkthread(2).vm; vm.resettop(); - } - - private void gethookcount(LuaState vm) { - // TODO Auto-generated method stub - vm.resettop(); - vm.pushinteger(0); - } - - private void gethookmask(LuaState vm) { - // TODO Auto-generated method stub - vm.resettop(); - vm.pushinteger(0); + vm.pushlvalue(threadVm.gethook()); + vm.pushinteger(threadVm.gethookmask()); + vm.pushinteger(threadVm.gethookcount()); } private void sethook(LuaState vm) { - // TODO Auto-generated method stub + LuaState threadVm = vm; + if ( vm.gettop() >= 4 ) { + threadVm = vm.checkthread(2).vm; + vm.remove(2); + } + LFunction func = vm.checkfunction(2); + LString str = vm.checklstring(3); + int count = vm.optint(4,0); + int mask = 0; + for ( int i=0; i pc ? + lineNumbers[pc] : + -1); + return line; + } + + private void debugCallHook(int mask, int newline) { + int prevmask = hookmask; + try { + hookmask = 0; + this.pushfunction(hookfunc); + switch ( mask ) { + default: this.pushstring("line"); break; + case LUA_HOOKCOUNT: this.pushstring("count"); break; + case LUA_HOOKCALL: this.pushstring("call"); break; + case LUA_HOOKRET: this.pushstring("return"); break; + case LUA_HOOKTAILRET: this.pushstring("tail return"); break; + } + this.pushinteger(newline); + this.pcall(2, 0, 0); + } finally { + hookmask = prevmask; + } + } } diff --git a/src/test/res/debuglib.lua b/src/test/res/debuglib.lua new file mode 100644 index 00000000..da7727d3 --- /dev/null +++ b/src/test/res/debuglib.lua @@ -0,0 +1,40 @@ + +local print = print +print( 'has debug', debug~=nil ) + +local printinfo = function(...) + for i,a in ipairs(arg) do + if type(a) == 'table' then + print( ' source: '..tostring(a.source) ) + print( ' short_src: '..tostring(a.short_src) ) + print( ' what: '..tostring(a.what) ) + print( ' currentline: '..tostring(a.currentline) ) + print( ' linedefined: '..tostring(a.linedefined) ) + print( ' lastlinedefined: '..tostring(a.lastlinedefined) ) + else + print( tostring(a) ) + end + end +end + +function test() + local x = 5 + function f() + x = x + 1 + return x + end + function g() + x = x - 1 + return x + end + print(f()) + print(g()) + return f, g +end + +local e,f,g = pcall( test ) +print( 'e,f,g', e, type(f), type(g) ) + +printinfo( 'debug.getinfo(f,"Sl")', pcall(debug.getinfo, f, "Sl") ) +printinfo( 'debug.getinfo(g,"Sl")', pcall(debug.getinfo, g, "Sl") ) +printinfo( 'debug.getinfo(test,"Sl")', pcall(debug.getinfo, test, "Sl") ) diff --git a/version.properties b/version.properties index 6715c53a..17ade615 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -version: 0.92 +version: 0.93