Improve debug lib consistency with lua 5.2

This commit is contained in:
James Roseborough
2012-09-15 04:53:37 +00:00
parent 4795171956
commit 6ae66f6fce
9 changed files with 217 additions and 164 deletions

View File

@@ -124,6 +124,10 @@ public class LuaClosure extends LuaFunction {
return s_metatable;
}
public String tojstring() {
return "function: " + p.toString();
}
public final LuaValue call() {
LuaValue[] stack = new LuaValue[p.maxstacksize];
for (int i = 0; i < p.numparams; ++i )
@@ -185,7 +189,8 @@ public class LuaClosure extends LuaFunction {
LuaValue[] k = p.k;
// upvalues are only possible when closures create closures
UpValue[] openups = p.p.length>0? new UpValue[p.p.length]: null;
// TODO: use linked list.
UpValue[] openups = p.p.length>0? new UpValue[stack.length]: null;
// debug wants args to this function
if (DebugLib.DEBUG_ENABLED && globals != null)

View File

@@ -47,8 +47,8 @@ public class LuaError extends RuntimeException {
protected Throwable cause;
public String getMessage() {
return (fileline != null? fileline + " ": "")
+ (traceback != null? traceback: super.getMessage());
return traceback != null? traceback:
(fileline != null? fileline: "?:-1") + " " + super.getMessage();
}
/** Construct LuaError when a program exception occurs.

View File

@@ -72,7 +72,10 @@ public class LuaFunction extends LuaValue {
public String tojstring() {
String s = getClass().getName();
return "function: " + s.substring(s.lastIndexOf('.')+1);
return "function: " + s.substring(Math.max(s.lastIndexOf('.'),s.lastIndexOf('$'))+1);
}
public LuaString strvalue() {
return valueOf(tojstring());
}
}

View File

@@ -307,9 +307,17 @@ public class LuaThread extends LuaValue {
public String traceback(int level) {
StringBuffer sb = new StringBuffer();
sb.append( "stack traceback:" );
for (LuaFunction f = null; (f = getFunction(level)) != null; ++level) {
sb.append( "\n\t" );
sb.append( f.tostring() );
CallFrame c = getCallFrame(level);
if (c != null) {
sb.append("\n\t");
sb.append( c.sourceline() );
sb.append( " in " );
while ( (c = getCallFrame(++level)) != null ) {
sb.append( c.tracename() );
sb.append( "\n\t" );
sb.append( c.sourceline() );
sb.append( " in " );
}
sb.append( "main chunk" );
}
return sb.toString();
@@ -332,17 +340,16 @@ public class LuaThread extends LuaValue {
public static class CallFrame {
public LuaFunction f;
int pc, top;
public int pc;
int top;
Varargs v;
LuaValue[] stack;
boolean tail;
public void set(LuaClosure function, Varargs varargs, LuaValue[] stack) {
this.f = function;
this.v = varargs;
this.stack = stack;
}
public void print() {
// TODO Auto-generated method stub
this.tail = false;
}
public void set(LuaFunction function) {
this.f = function;
@@ -356,7 +363,9 @@ public class LuaThread extends LuaValue {
this.pc = pc;
this.v = v;
this.top = top;
if (DebugLib.DEBUG_ENABLED && DebugLib.TRACE & f.isclosure())
if (f.checkclosure().p.code[pc] == Lua.OP_TAILCALL)
this.tail = true;
if (DebugLib.TRACE)
Print.printState(f.checkclosure(), pc, stack, top, v);
}
public Varargs getLocal(int i) {
@@ -377,34 +386,32 @@ public class LuaThread extends LuaValue {
}
public int currentline() {
if ( !f.isclosure() ) return -1;
int[] li = ((LuaClosure)f).p.lineinfo;
int[] li = f.checkclosure().p.lineinfo;
return li==null || pc<0 || pc>=li.length? -1: li[pc];
}
public LuaString[] getfunckind() {
if ( !f.isclosure() || pc<0 ) return null;
Prototype p = ((LuaClosure)f).p;
int stackpos = (p.code[pc] >> 6) & 0xff;
return DebugLib.getobjname(p, pc, stackpos);
}
public String sourceline() {
if ( !f.isclosure() ) return f.tojstring();
String s = ((LuaClosure)f).p.source.tojstring();
String s = f.checkclosure().p.source.tojstring();
int line = currentline();
return (s.startsWith("@")||s.startsWith("=")? s.substring(1): s) + ":" + line;
}
public String tracename() {
LuaString[] kind = getfunckind();
LuaString[] kind = DebugLib.getfuncname(this);
if ( kind == null )
return "function ?";
return "function "+kind[0].tojstring();
}
public LuaString getlocalname(int index) {
if ( !f.isclosure() ) return null;
return ((LuaClosure)f).p.getlocalname(index, pc);
return f.checkclosure().p.getlocalname(index, pc);
}
public String tojstring() {
return tracename()+" "+sourceline();
}
public boolean istailcall() {
return tail;
}
}
}

View File

@@ -410,8 +410,7 @@ public class Print extends Lua {
s.substring(0, 32).tojstring()+"...+"+(s.length()-32)+"b");
break;
case LuaValue.TFUNCTION:
ps.print( ( v instanceof LuaClosure )?
((LuaClosure)v).p.toString(): v.tojstring() );
ps.print( v.tojstring() );
break;
case LuaValue.TUSERDATA:
Object o = v.touserdata();

View File

@@ -21,9 +21,8 @@
******************************************************************************/
package org.luaj.vm2.lib;
import java.lang.ref.WeakReference;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState.LuaCompiler;
import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaBoolean;
import org.luaj.vm2.LuaClosure;
@@ -34,11 +33,14 @@ import org.luaj.vm2.LuaNumber;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaThread;
import org.luaj.vm2.LuaThread.CallFrame;
import org.luaj.vm2.LuaUserdata;
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.jme.JmePlatform;
import org.luaj.vm2.lib.jse.JsePlatform;
import org.luaj.vm2.luajc.LuaJC;
/**
* Subclass of {@link LibFunction} which implements the lua standard {@code debug}
@@ -76,23 +78,28 @@ public class DebugLib extends OneArgFunction {
// remove it in production builds
public static boolean DEBUG_ENABLED;
private static final LuaString LUA = valueOf("Lua");
private static final LuaString JAVA = valueOf("Java");
private static final LuaString QMARK = valueOf("?");
private static final LuaString GLOBAL = valueOf("global");
private static final LuaString LOCAL = valueOf("local");
private static final LuaString METHOD = valueOf("method");
private static final LuaString UPVALUE = valueOf("upvalue");
private static final LuaString FIELD = valueOf("field");
private static final LuaString CALL = valueOf("call");
private static final LuaString LINE = valueOf("line");
private static final LuaString COUNT = valueOf("count");
private static final LuaString RETURN = valueOf("return");
private static final LuaString TAILRETURN = valueOf("tail return");
private static final LuaString CONSTANT = valueOf("constant");
private static final LuaString LUA = valueOf("Lua");
private static final LuaString JAVA = valueOf("Java");
private static final LuaString QMARK = valueOf("?");
private static final LuaString GLOBAL = valueOf("global");
private static final LuaString LOCAL = valueOf("local");
private static final LuaString METHOD = valueOf("method");
private static final LuaString UPVALUE = valueOf("upvalue");
private static final LuaString FIELD = valueOf("field");
private static final LuaString CALL = valueOf("call");
private static final LuaString LINE = valueOf("line");
private static final LuaString COUNT = valueOf("count");
private static final LuaString RETURN = valueOf("return");
private static final LuaString TAILRETURN = valueOf("tail return");
private static final LuaString CONSTANT = valueOf("constant");
private static final LuaString FOR_ITERATOR = valueOf("for iterator");
private static final LuaString METAMETHOD = valueOf("metamethod");
private static final LuaString FUNC = valueOf("func");
private static final LuaString ISTAILCALL = valueOf("istailcall");
private static final LuaString ISVARARG = valueOf("isvararg");
private static final LuaString NUPS = valueOf("nups");
private static final LuaString NPARAMS = valueOf("nparams");
private static final LuaString NAME = valueOf("name");
private static final LuaString NAMEWHAT = valueOf("namewhat");
private static final LuaString WHAT = valueOf("what");
@@ -110,9 +117,23 @@ public class DebugLib extends OneArgFunction {
globals.debuglib = this;
DEBUG_ENABLED = true;
LuaTable debug = new LuaTable();
env.set("debug", new debug());
env.set("gethook", new gethook());
env.set("sethook", new sethook());
debug.set("debug", new debug());
debug.set("gethook", new gethook());
debug.set("getinfo", new getinfo());
debug.set("getlocal", new getlocal());
debug.set("getmetatable", new getmetatable());
debug.set("getregistry", new getregistry());
debug.set("getupvalue", new getupvalue());
debug.set("getuservalue", new getuservalue());
debug.set("sethook", new sethook());
debug.set("setlocal", new setlocal());
debug.set("setmetatable", new setmetatable());
debug.set("setupvalue", new setupvalue());
debug.set("setuservalue", new setuservalue());
debug.set("traceback", new traceback());
debug.set("upvalueid", new upvalueid());
debug.set("upvaluejoin", new upvaluejoin());
env.set("debug", debug);
env.get("package").get("loaded").set("debug", debug);
return debug;
}
@@ -140,9 +161,74 @@ public class DebugLib extends OneArgFunction {
public Varargs invoke(Varargs args) {
int a=1;
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
LuaValue func = args.optfunction(a++, null);
String what = args.optjstring(a++,"");
return NONE;
LuaValue func = args.arg(a++);
String what = args.optjstring(a++, "flnStu");
LuaThread.CallStack callstack = thread.callstack;
// find the stack info
LuaThread.CallFrame frame;
if ( func.isnumber() ) {
frame = callstack.getCallFrame(func.toint());
if (frame == null)
return NONE;
func = frame.f;
} else if ( func.isfunction() ) {
frame = callstack.findCallFrame(func);
} else {
return argerror(a-2, "function or level");
}
// start a table
LuaTable info = new LuaTable();
LuaClosure c = func.optclosure(null);
if (what.indexOf('S') >= 0) {
if (c != null) {
Prototype p = c.p;
info.set(WHAT, LUA);
info.set(SOURCE, p.source);
info.set(SHORT_SRC, valueOf(sourceshort(p)));
info.set(LINEDEFINED, valueOf(p.linedefined));
info.set(LASTLINEDEFINED, valueOf(p.lastlinedefined));
} else {
String shortName = func.tojstring();
LuaString name = LuaString.valueOf("[Java] " + shortName);
info.set(WHAT, JAVA);
info.set(SOURCE, name);
info.set(SHORT_SRC, valueOf(shortName));
info.set(LINEDEFINED, LuaValue.MINUSONE);
info.set(LASTLINEDEFINED, LuaValue.MINUSONE);
}
}
if (what.indexOf('l') >= 0) {
info.set( CURRENTLINE, valueOf(frame!=null? frame.currentline(): -1) );
}
if (what.indexOf('u') >= 0) {
info.set(NUPS, valueOf(c!=null? c.p.upvalues.length: 0));
info.set(NPARAMS, valueOf(c!=null? c.p.numparams: 0));
info.set(ISVARARG, valueOf(c!=null? c.p.is_vararg: 0));
}
if (what.indexOf('n') >= 0) {
if (frame != null) {
LuaString[] kind = getfuncname(frame);
info.set(NAME, kind!=null? kind[0]: QMARK);
info.set(NAMEWHAT, kind!=null? kind[1]: EMPTYSTRING);
}
}
if (what.indexOf('t') >= 0) {
info.set(ISTAILCALL, frame != null && frame.istailcall()? ONE: ZERO);
}
if (what.indexOf('L') >= 0) {
LuaTable lines = new LuaTable();
info.set(ACTIVELINES, lines);
CallFrame cf;
for (int l = 1; (cf=callstack.getCallFrame(l)) != null; ++l)
if (cf.f == func)
lines.insert(-1, valueOf(cf.currentline()));
}
if (what.indexOf('f') >= 0) {
info.set( FUNC, func );
}
return info;
}
}
@@ -151,9 +237,9 @@ public class DebugLib extends OneArgFunction {
public Varargs invoke(Varargs args) {
int a=1;
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
LuaValue func = args.optfunction(a++, null);
LuaValue local = args.checkvalue(a++);
return thread.callstack.findCallFrame(func).getLocal(local.toint());
int level = args.checkint(a++);
int local = args.checkint(a++);
return thread.callstack.getCallFrame(level).getLocal(local);
}
}
@@ -225,32 +311,27 @@ public class DebugLib extends OneArgFunction {
public Varargs invoke(Varargs args) {
int a=1;
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
LuaValue func = args.optfunction(a++, null);
LuaValue local = args.checkvalue(a++);
LuaValue value = args.checkvalue(a++);
return thread.callstack.findCallFrame(func).setLocal(local.toint(), value);
int level = args.checkint(a++);
int local = args.checkint(a++);
LuaValue value = args.arg(a++);
return thread.callstack.getCallFrame(level).setLocal(local, value);
}
}
// debug.setmetatable (value, table)
final class setmetatable extends VarArgFunction {
public Varargs invoke(Varargs args) {
LuaValue object = args.arg(1);
try {
LuaValue mt = args.opttable(2, null);
switch ( object.type() ) {
case TNIL: LuaNil.s_metatable = mt; break;
case TNUMBER: LuaNumber.s_metatable = mt; break;
case TBOOLEAN: LuaBoolean.s_metatable = mt; break;
case TSTRING: LuaString.s_metatable = mt; break;
case TFUNCTION: LuaFunction.s_metatable = mt; break;
case TTHREAD: LuaThread.s_metatable = mt; break;
default: object.setmetatable( mt );
}
return LuaValue.TRUE;
} catch ( LuaError e ) {
return varargsOf(FALSE, valueOf(e.toString()));
final class setmetatable extends TwoArgFunction {
public LuaValue call(LuaValue value, LuaValue table) {
LuaValue mt = table.opttable(null);
switch ( value.type() ) {
case TNIL: LuaNil.s_metatable = mt; break;
case TNUMBER: LuaNumber.s_metatable = mt; break;
case TBOOLEAN: LuaBoolean.s_metatable = mt; break;
case TSTRING: LuaString.s_metatable = mt; break;
case TFUNCTION: LuaFunction.s_metatable = mt; break;
case TTHREAD: LuaThread.s_metatable = mt; break;
default: value.setmetatable( mt );
}
return value;
}
}
@@ -291,7 +372,7 @@ public class DebugLib extends OneArgFunction {
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
String message = args.optjstring(a++, null);
int level = args.optint(a++,1);
String tb = thread.callstack.traceback(level-1);
String tb = thread.callstack.traceback(level);
return valueOf(message!=null? message+"\n"+tb: tb);
}
}
@@ -331,83 +412,7 @@ public class DebugLib extends OneArgFunction {
}
return null;
}
// ------------------- library function implementations -----------------
protected Varargs _getinfo(Varargs args, LuaValue level0func) {
int a=1;
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
LuaValue func = args.arg(a++);
String what = args.optjstring(a++, "nSluf");
LuaThread.CallStack callstack = thread.callstack;
// find the stack info
LuaThread.CallFrame frame;
if ( func.isnumber() ) {
frame = callstack.getCallFrame(func.toint());
} else {
frame = callstack.findCallFrame(func);
}
if (frame == null)
return NIL;
// start a table
LuaTable info = new LuaTable();
LuaClosure c = frame.f.isclosure()? (LuaClosure) frame.f: null;
for (int i = 0, j = what.length(); i < j; i++) {
switch (what.charAt(i)) {
case 'S': {
if ( c != null ) {
Prototype p = c.p;
info.set(WHAT, LUA);
info.set(SOURCE, p.source);
info.set(SHORT_SRC, valueOf(sourceshort(p)));
info.set(LINEDEFINED, valueOf(p.linedefined));
info.set(LASTLINEDEFINED, valueOf(p.lastlinedefined));
} else {
String shortName = frame.f.tojstring();
LuaString name = LuaString.valueOf("[Java] "+shortName);
info.set(WHAT, JAVA);
info.set(SOURCE, name);
info.set(SHORT_SRC, valueOf(shortName));
info.set(LINEDEFINED, LuaValue.MINUSONE);
info.set(LASTLINEDEFINED, LuaValue.MINUSONE);
}
break;
}
case 'l': {
int line = frame.currentline();
info.set( CURRENTLINE, valueOf(line) );
break;
}
case 'u': {
info.set(NUPS, valueOf(c!=null? c.p.upvalues.length: 0));
break;
}
case 'n': {
LuaString[] kind = frame.getfunckind();
info.set(NAME, kind!=null? kind[0]: QMARK);
info.set(NAMEWHAT, kind!=null? kind[1]: EMPTYSTRING);
break;
}
case 'f': {
info.set( FUNC, frame.f );
break;
}
case 'L': {
LuaTable lines = new LuaTable();
info.set(ACTIVELINES, lines);
int line = frame.currentline();
if ( line >= 0 )
lines.set(1, valueOf(line));
break;
}
}
}
return info;
}
public static String sourceshort(Prototype p) {
String name = p.source.tojstring();
if ( name.startsWith("@") || name.startsWith("=") )
@@ -416,13 +421,48 @@ public class DebugLib extends OneArgFunction {
name = "binary string";
return name;
}
// =======================================================
static void lua_assert(boolean x) {
if (!x) throw new RuntimeException("lua_assert failed");
}
public static LuaString[] getfuncname(CallFrame frame) {
if (!frame.f.isclosure())
return new LuaString[] { frame.f.strvalue(), JAVA };
Prototype p = frame.f.checkclosure().p;
int pc = frame.pc;
int i = p.code[pc]; /* calling instruction */
LuaString tm;
switch (Lua.GET_OPCODE(i)) {
case Lua.OP_CALL:
case Lua.OP_TAILCALL: /* get function name */
return getobjname(p, pc, Lua.GETARG_A(i));
case Lua.OP_TFORCALL: /* for iterator */
return new LuaString[] { FOR_ITERATOR, FOR_ITERATOR};
/* all other instructions can call only through metamethods */
case Lua.OP_SELF:
case Lua.OP_GETTABUP:
case Lua.OP_GETTABLE: tm = LuaValue.INDEX; break;
case Lua.OP_SETTABUP:
case Lua.OP_SETTABLE: tm = LuaValue.NEWINDEX; break;
case Lua.OP_EQ: tm = LuaValue.EQ; break;
case Lua.OP_ADD: tm = LuaValue.ADD; break;
case Lua.OP_SUB: tm = LuaValue.SUB; break;
case Lua.OP_MUL: tm = LuaValue.MUL; break;
case Lua.OP_DIV: tm = LuaValue.DIV; break;
case Lua.OP_MOD: tm = LuaValue.MOD; break;
case Lua.OP_POW: tm = LuaValue.POW; break;
case Lua.OP_UNM: tm = LuaValue.UNM; break;
case Lua.OP_LEN: tm = LuaValue.LEN; break;
case Lua.OP_LT: tm = LuaValue.LT; break;
case Lua.OP_LE: tm = LuaValue.LE; break;
case Lua.OP_CONCAT: tm = LuaValue.CONCAT; break;
default:
return null; /* else no useful name can be found */
}
return new LuaString[] { tm, METAMETHOD };
}
// return StrValue[] { name, namewhat } if found, null if not
public static LuaString[] getobjname(Prototype p, int lastpc, int reg) {
int pc = lastpc; // currentpc(L, ci);
@@ -445,12 +485,12 @@ public class DebugLib extends OneArgFunction {
case Lua.OP_GETTABUP:
case Lua.OP_GETTABLE: {
int k = Lua.GETARG_C(i); /* key index */
int t = Lua.GETARG_Bx(i); /* table index */
int t = Lua.GETARG_B(i); /* table index */
LuaString vn = (Lua.GET_OPCODE(i) == Lua.OP_GETTABLE) /* name of indexed variable */
? p.getlocalname(t + 1, pc)
: (t < p.upvalues.length ? p.upvalues[t].name : QMARK);
name = kname(p, k);
return new LuaString[] { name, vn.eq_b(ENV)? GLOBAL: FIELD };
return new LuaString[] { name, vn != null && vn.eq_b(ENV)? GLOBAL: FIELD };
}
case Lua.OP_GETUPVAL: {
int u = Lua.GETARG_B(i); /* upvalue index */
@@ -460,7 +500,7 @@ public class DebugLib extends OneArgFunction {
case Lua.OP_LOADK:
case Lua.OP_LOADKX: {
int b = (Lua.GET_OPCODE(i) == Lua.OP_LOADK) ? Lua.GETARG_Bx(i)
: Lua.GETARG_Ax(p.code[pc + 1]);
: Lua.GETARG_Ax(p.code[pc + 1]);
if (p.k[b].isstring()) {
name = p.k[b].strvalue();
return new LuaString[] { name, CONSTANT };

View File

@@ -63,10 +63,10 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
default:
case JSE:
case LUAJIT:
globals = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
globals = org.luaj.vm2.lib.jse.JsePlatform.debugGlobals();
break;
case JME:
globals = org.luaj.vm2.lib.jme.JmePlatform.standardGlobals();
globals = org.luaj.vm2.lib.jme.JmePlatform.debugGlobals();
break;
}
}
@@ -179,8 +179,6 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
if ( script == null )
fail("Could not load script for test case: " + name);
try {
// Use "stdin" instead of resource name so that output matches
// standard Lua.
switch ( this.platform ) {
case LUAJIT:
if ( nocompile ) {
@@ -190,7 +188,7 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
return LuaJC.getInstance().load( script, name, _G);
}
default:
return LoadState.load(script, "=stdin", "bt", _G);
return LoadState.load(script, "@"+name+".lua", "bt", _G);
}
} catch ( Exception e ) {
e.printStackTrace();

View File

@@ -80,6 +80,7 @@ local echo = function(msg,...)
return ...
end
local echocr = function(...)
local arg = table.pack(...)
echo('(echocr) first args', table.unpack(arg,1,arg.n))
local a = arg
while true do

View File

@@ -85,11 +85,11 @@ print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..'
local s1,x1,y1 = pcall( debug.getmetatable, a )
local s2,x2,y2 = pcall( debug.setmetatable, a, mt )
print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..' b.b='..tostring(b.b))
local s3,x3,y3 = pcall( debug.getmetatable, a )
local s4,x4,y4 = pcall( debug.getmetatable, b )
local s5,x5,y5 = pcall( debug.setmetatable, a, nil )
local s3,x3,y3 = pcall( debug.getmetatable, a ) print(type(s3), type(x3), type(y3), type(getmetatable(a)))
local s4,x4,y4 = pcall( debug.getmetatable, b ) print(type(s4), type(x4), type(y4), type(getmetatable(b)))
local s5,x5,y5 = pcall( debug.setmetatable, a, nil ) print(type(s5), type(x5), type(y5), type(getmetatable(a)))
print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..' b.b='..tostring(b.b))
local s6,x6,y6 = pcall( debug.getmetatable, a )
local s6,x6,y6 = pcall( debug.getmetatable, a ) print(type(s6), type(x6), type(y6), type(getmetatable(a)))
if not s1 then print( 's1 error', x1 ) end
if not s2 then print( 's2 error', x2 ) end
if not s3 then print( 's3 error', x3 ) end