Let getinfo return without error when level extends beyond call stack

This commit is contained in:
James Roseborough
2009-04-02 13:45:51 +00:00
parent d236c31650
commit bb8ced423e
3 changed files with 14 additions and 3 deletions

View File

@@ -208,6 +208,10 @@ public class DebugLib extends LFunction {
String what = vm.optstring(3, "nSluf"); String what = vm.optstring(3, "nSluf");
if ( vm.isnumber(2) ) { if ( vm.isnumber(2) ) {
ci = this.getcallinfo(vm, threadVm, vm.tointeger(2)); ci = this.getcallinfo(vm, threadVm, vm.tointeger(2));
if ( ci == null ) {
vm.resettop();
return;
}
closure = ci.closure; closure = ci.closure;
} else { } else {
func = vm.checkfunction(2); func = vm.checkfunction(2);
@@ -354,8 +358,10 @@ public class DebugLib extends LFunction {
private CallInfo getcallinfo(LuaState vm, LuaState threadVm, int level) { private CallInfo getcallinfo(LuaState vm, LuaState threadVm, int level) {
--level ; // level 0 is the debug function itself --level ; // level 0 is the debug function itself
if ( level < 0 || level > threadVm.cc ) if ( level > threadVm.cc )
vm.error("level out of range"); return null;
if ( level < 0 )
level = 0;
int cc = threadVm.cc-level; int cc = threadVm.cc-level;
return threadVm.calls[cc]; return threadVm.calls[cc];
} }

View File

@@ -28,6 +28,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import org.luaj.compiler.LuaC; import org.luaj.compiler.LuaC;
import org.luaj.lib.DebugLib;
import org.luaj.platform.J2sePlatform; import org.luaj.platform.J2sePlatform;
import org.luaj.vm.LFunction; import org.luaj.vm.LFunction;
import org.luaj.vm.Lua; import org.luaj.vm.Lua;
@@ -62,6 +63,7 @@ public class lua {
Platform.setInstance(new J2sePlatform()); Platform.setInstance(new J2sePlatform());
LuaC.install(); LuaC.install();
LuaState vm = Platform.newLuaState(); LuaState vm = Platform.newLuaState();
DebugLib.install(vm);
// process args // process args
boolean interactive = (args.length == 0); boolean interactive = (args.length == 0);

View File

@@ -74,7 +74,7 @@ end
print( 'h', h() ) print( 'h', h() )
local funs = { f, g, h } local funs = { f, g, h }
local names = { 'f', 'g', 'h' } local names = { 'f', 'g', 'h' }
for i=1,3 do for i=1,3 do
local fun,name = funs[i],names[i] local fun,name = funs[i],names[i]
for index=0,10 do for index=0,10 do
local s1,x1,y1 = pcall( debug.getupvalue, fun, index ) local s1,x1,y1 = pcall( debug.getupvalue, fun, index )
@@ -153,11 +153,14 @@ function test()
x = x - 1 x = x - 1
print( '---' ) print( '---' )
printinfo( 'debug.getinfo(1)', debug.getinfo(1) ) printinfo( 'debug.getinfo(1)', debug.getinfo(1) )
printinfo( 'debug.getinfo(1,"")', debug.getinfo(1, "") )
printinfo( 'debug.getinfo(1,"l")', debug.getinfo(1, "l") ) printinfo( 'debug.getinfo(1,"l")', debug.getinfo(1, "l") )
printinfo( 'debug.getinfo(1,"fL")', debug.getinfo(1, "fL") ) printinfo( 'debug.getinfo(1,"fL")', debug.getinfo(1, "fL") )
printinfo( 'debug.getinfo(2)', debug.getinfo(2) ) printinfo( 'debug.getinfo(2)', debug.getinfo(2) )
printinfo( 'debug.getinfo(2,"l")', debug.getinfo(2, "l") ) printinfo( 'debug.getinfo(2,"l")', debug.getinfo(2, "l") )
printinfo( 'debug.getinfo(2,"fL")', debug.getinfo(2, "fL") ) printinfo( 'debug.getinfo(2,"fL")', debug.getinfo(2, "fL") )
printinfo( 'debug.getinfo(10,"")', pcall( debug.getinfo, 10, "" ) )
printinfo( 'debug.getinfo(-10,"")', pcall( debug.getinfo, -10, "" ) )
--[[ --[[
for i=1,3 do for i=1,3 do
printinfo( 'debug.traceback("msg")', debug.traceback('msg') ) printinfo( 'debug.traceback("msg")', debug.traceback('msg') )