diff --git a/src/core/org/luaj/vm2/LuaClosure.java b/src/core/org/luaj/vm2/LuaClosure.java index 314aafc3..cd627116 100644 --- a/src/core/org/luaj/vm2/LuaClosure.java +++ b/src/core/org/luaj/vm2/LuaClosure.java @@ -560,5 +560,10 @@ public class LuaClosure extends LuaFunction { protected void setUpvalue(int i, LuaValue v) { upValues[i].setValue(v); } + + public String name() { + return "<"+p.shortsource()+":"+p.linedefined+">"; + } + } diff --git a/src/core/org/luaj/vm2/LuaFunction.java b/src/core/org/luaj/vm2/LuaFunction.java index b0278adf..e3dc9b96 100644 --- a/src/core/org/luaj/vm2/LuaFunction.java +++ b/src/core/org/luaj/vm2/LuaFunction.java @@ -77,4 +77,11 @@ public class LuaFunction extends LuaValue { String s = getClass().getName(); return s.substring(Math.max(s.lastIndexOf('.'),s.lastIndexOf('$'))+1); } + + /** Return a human-readable name for this function. Returns the last part of the class name by default. + * Is overridden by LuaClosure to return the source file and line, and by LibFunctions to return the name. + * @return common name for this function. */ + public String name() { + return classnamestub(); + } } diff --git a/src/core/org/luaj/vm2/Prototype.java b/src/core/org/luaj/vm2/Prototype.java index 79daa564..4f3e1bf0 100644 --- a/src/core/org/luaj/vm2/Prototype.java +++ b/src/core/org/luaj/vm2/Prototype.java @@ -79,4 +79,13 @@ public class Prototype { } return null; /* not found */ } + + public String shortsource() { + String name = source.tojstring(); + if ( name.startsWith("@") || name.startsWith("=") ) + name = name.substring(1); + else if ( name.startsWith("\033") ) + name = "binary string"; + return name; + } } diff --git a/src/core/org/luaj/vm2/lib/DebugLib.java b/src/core/org/luaj/vm2/lib/DebugLib.java index fbc4c533..e5483573 100644 --- a/src/core/org/luaj/vm2/lib/DebugLib.java +++ b/src/core/org/luaj/vm2/lib/DebugLib.java @@ -467,13 +467,13 @@ public class DebugLib extends OneArgFunction { this.linedefined = p.linedefined; this.lastlinedefined = p.lastlinedefined; this.what = (this.linedefined == 0) ? "main" : "Lua"; - this.short_src = DebugLib.shortsource(p); + this.short_src = p.shortsource(); } else { this.source = "=[Java]"; this.linedefined = -1; this.lastlinedefined = -1; this.what = "Java"; - this.short_src = f.classnamestub(); + this.short_src = f.name(); } } } @@ -598,9 +598,6 @@ public class DebugLib extends OneArgFunction { ar.name = nw.name; ar.namewhat = nw.namewhat; } - } else { - ar.name = ci.previous.f.classnamestub(); - ar.namewhat = "Java"; } } if (ar.namewhat == null) { @@ -635,7 +632,7 @@ public class DebugLib extends OneArgFunction { this.stack = stack; } public String shortsource() { - return f.isclosure()? DebugLib.shortsource(f.checkclosure().p): "[Java]"; + return f.isclosure()? f.checkclosure().p.shortsource(): "[Java]"; } void set(LuaFunction function) { this.f = function; @@ -675,7 +672,7 @@ public class DebugLib extends OneArgFunction { } String sourceline() { if ( !f.isclosure() ) return f.tojstring(); - return DebugLib.shortsource(f.checkclosure().p) + ":" + currentline(); + return f.checkclosure().p.shortsource() + ":" + currentline(); } private int linedefined() { return f.isclosure()? f.checkclosure().p.linedefined: -1; @@ -696,15 +693,6 @@ public class DebugLib extends OneArgFunction { return null; } - public static String shortsource(Prototype p) { - String name = p.source.tojstring(); - if ( name.startsWith("@") || name.startsWith("=") ) - name = name.substring(1); - else if ( name.startsWith("\033") ) - name = "binary string"; - return name; - } - static void lua_assert(boolean x) { if (!x) throw new RuntimeException("lua_assert failed"); }