Improve debug lib consistency with lua 5.2
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2;
|
||||
|
||||
import org.luaj.vm2.Varargs.SubVarargs;
|
||||
|
||||
|
||||
/**
|
||||
* Base class for all concrete lua type values.
|
||||
@@ -3387,8 +3389,8 @@ public class LuaValue extends Varargs {
|
||||
switch ( v.length ) {
|
||||
case 0: return NONE;
|
||||
case 1: return v[0];
|
||||
case 2: return new PairVarargs(v[0],v[1]);
|
||||
default: return new ArrayVarargs(v,NONE);
|
||||
case 2: return new Varargs.PairVarargs(v[0],v[1]);
|
||||
default: return new Varargs.ArrayVarargs(v,NONE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3403,8 +3405,8 @@ public class LuaValue extends Varargs {
|
||||
public static Varargs varargsOf(final LuaValue[] v,Varargs r) {
|
||||
switch ( v.length ) {
|
||||
case 0: return r;
|
||||
case 1: return new PairVarargs(v[0],r);
|
||||
default: return new ArrayVarargs(v,r);
|
||||
case 1: return new Varargs.PairVarargs(v[0],r);
|
||||
default: return new Varargs.ArrayVarargs(v,r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3421,8 +3423,8 @@ public class LuaValue extends Varargs {
|
||||
switch ( length ) {
|
||||
case 0: return NONE;
|
||||
case 1: return v[offset];
|
||||
case 2: return new PairVarargs(v[offset+0],v[offset+1]);
|
||||
default: return new ArrayPartVarargs(v,offset,length);
|
||||
case 2: return new Varargs.PairVarargs(v[offset+0],v[offset+1]);
|
||||
default: return new Varargs.ArrayPartVarargs(v,offset,length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3439,8 +3441,8 @@ public class LuaValue extends Varargs {
|
||||
public static Varargs varargsOf(final LuaValue[] v, final int offset, final int length,Varargs more) {
|
||||
switch ( length ) {
|
||||
case 0: return more;
|
||||
case 1: return new PairVarargs(v[offset],more);
|
||||
default: return new ArrayPartVarargs(v,offset,length,more);
|
||||
case 1: return new Varargs.PairVarargs(v[offset],more);
|
||||
default: return new Varargs.ArrayPartVarargs(v,offset,length,more);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3457,7 +3459,7 @@ public class LuaValue extends Varargs {
|
||||
public static Varargs varargsOf(LuaValue v, Varargs r) {
|
||||
switch ( r.narg() ) {
|
||||
case 0: return v;
|
||||
default: return new PairVarargs(v,r);
|
||||
default: return new Varargs.PairVarargs(v,r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3474,8 +3476,8 @@ public class LuaValue extends Varargs {
|
||||
*/
|
||||
public static Varargs varargsOf(LuaValue v1,LuaValue v2,Varargs v3) {
|
||||
switch ( v3.narg() ) {
|
||||
case 0: return new PairVarargs(v1,v2);
|
||||
default: return new ArrayVarargs(new LuaValue[] {v1,v2},v3);
|
||||
case 0: return new Varargs.PairVarargs(v1,v2);
|
||||
default: return new Varargs.ArrayVarargs(new LuaValue[] {v1,v2},v3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3530,120 +3532,21 @@ public class LuaValue extends Varargs {
|
||||
public int narg() { return 0; }
|
||||
public LuaValue arg1() { return NIL; }
|
||||
public String tojstring() { return "none"; }
|
||||
public Varargs subargs(final int start) { return this; }
|
||||
|
||||
}
|
||||
|
||||
/** Varargs implemenation backed by an array of LuaValues
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static methods on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[])
|
||||
* @see LuaValue#varargsOf(LuaValue[], Varargs)
|
||||
/**
|
||||
* Create a {@code Varargs} instance containing arguments starting at index {@code start}
|
||||
* @param start the index from which to include arguments, where 1 is the first argument.
|
||||
* @return Varargs containing argument { start, start+1, ... , narg-start-1 }
|
||||
*/
|
||||
static final class ArrayVarargs extends Varargs {
|
||||
private final LuaValue[] v;
|
||||
private final Varargs r;
|
||||
/** Construct a Varargs from an array of LuaValue.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static methods on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[])
|
||||
* @see LuaValue#varargsOf(LuaValue[], Varargs)
|
||||
*/
|
||||
ArrayVarargs(LuaValue[] v, Varargs r) {
|
||||
this.v = v;
|
||||
this.r = r ;
|
||||
}
|
||||
public LuaValue arg(int i) {
|
||||
return i >=1 && i<=v.length? v[i - 1]: r.arg(i-v.length);
|
||||
}
|
||||
public int narg() {
|
||||
return v.length+r.narg();
|
||||
}
|
||||
public LuaValue arg1() { return v.length>0? v[0]: r.arg1(); }
|
||||
}
|
||||
|
||||
/** Varargs implemenation backed by an array of LuaValues
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static methods on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[], int, int)
|
||||
* @see LuaValue#varargsOf(LuaValue[], int, int, Varargs)
|
||||
*/
|
||||
static final class ArrayPartVarargs extends Varargs {
|
||||
private final int offset;
|
||||
private final LuaValue[] v;
|
||||
private final int length;
|
||||
private final Varargs more;
|
||||
/** Construct a Varargs from an array of LuaValue.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static methods on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[], int, int)
|
||||
*/
|
||||
ArrayPartVarargs(LuaValue[] v, int offset, int length) {
|
||||
this.v = v;
|
||||
this.offset = offset;
|
||||
this.length = length;
|
||||
this.more = NONE;
|
||||
}
|
||||
/** Construct a Varargs from an array of LuaValue and additional arguments.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static method on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[], int, int, Varargs)
|
||||
*/
|
||||
public ArrayPartVarargs(LuaValue[] v, int offset, int length, Varargs more) {
|
||||
this.v = v;
|
||||
this.offset = offset;
|
||||
this.length = length;
|
||||
this.more = more;
|
||||
}
|
||||
public LuaValue arg(int i) {
|
||||
return i>=1&&i<=length? v[i+offset-1]: more.arg(i-length);
|
||||
}
|
||||
public int narg() {
|
||||
return length + more.narg();
|
||||
}
|
||||
public LuaValue arg1() {
|
||||
return length>0? v[offset]: more.arg1();
|
||||
}
|
||||
}
|
||||
|
||||
/** Varargs implemenation backed by two values.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static method on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue, Varargs)
|
||||
*/
|
||||
static final class PairVarargs extends Varargs {
|
||||
private final LuaValue v1;
|
||||
private final Varargs v2;
|
||||
/** Construct a Varargs from an two LuaValue.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static method on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue, Varargs)
|
||||
*/
|
||||
PairVarargs(LuaValue v1, Varargs v2) {
|
||||
this.v1 = v1;
|
||||
this.v2 = v2;
|
||||
}
|
||||
public LuaValue arg(int i) {
|
||||
return i==1? v1: v2.arg(i-1);
|
||||
}
|
||||
public int narg() {
|
||||
return 1+v2.narg();
|
||||
}
|
||||
public LuaValue arg1() {
|
||||
return v1;
|
||||
}
|
||||
public Varargs subargs(final int start) {
|
||||
if (start == 1)
|
||||
return this;
|
||||
if (start > 1)
|
||||
return NONE;
|
||||
return new Varargs.SubVarargs(this, start, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -505,7 +505,7 @@ public abstract class Varargs {
|
||||
int end = narg();
|
||||
switch ( end-start ) {
|
||||
case 0: return arg(start);
|
||||
case 1: return new LuaValue.PairVarargs(arg(start),arg(end));
|
||||
case 1: return new Varargs.PairVarargs(arg(start),arg(end));
|
||||
}
|
||||
return end<start? (Varargs) LuaValue.NONE: new SubVarargs(this,start,end);
|
||||
}
|
||||
@@ -514,7 +514,7 @@ public abstract class Varargs {
|
||||
* Implementation of Varargs for use in the Varargs.subargs() function.
|
||||
* @see Varargs#subargs(int)
|
||||
*/
|
||||
private static class SubVarargs extends Varargs {
|
||||
static class SubVarargs extends Varargs {
|
||||
private final Varargs v;
|
||||
private final int start;
|
||||
private final int end;
|
||||
@@ -533,5 +533,146 @@ public abstract class Varargs {
|
||||
public int narg() {
|
||||
return end+1-start;
|
||||
}
|
||||
public Varargs subargs(final int start) {
|
||||
if (start == 1)
|
||||
return this;
|
||||
final int newstart = this.start + start - 1;
|
||||
if (start > 0) {
|
||||
if (newstart >= this.end)
|
||||
return LuaValue.NONE;
|
||||
if (newstart == this.end)
|
||||
return v.arg(this.end);
|
||||
if (newstart == this.end-1)
|
||||
return new Varargs.PairVarargs(v.arg(this.end-1), v.arg(this.end));
|
||||
return new SubVarargs(v, newstart, this.end);
|
||||
}
|
||||
return new SubVarargs(v, newstart, this.end);
|
||||
}
|
||||
}
|
||||
|
||||
/** Varargs implemenation backed by two values.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static method on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue, Varargs)
|
||||
*/
|
||||
static final class PairVarargs extends Varargs {
|
||||
private final LuaValue v1;
|
||||
private final Varargs v2;
|
||||
/** Construct a Varargs from an two LuaValue.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static method on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue, Varargs)
|
||||
*/
|
||||
PairVarargs(LuaValue v1, Varargs v2) {
|
||||
this.v1 = v1;
|
||||
this.v2 = v2;
|
||||
}
|
||||
public LuaValue arg(int i) {
|
||||
return i==1? v1: v2.arg(i-1);
|
||||
}
|
||||
public int narg() {
|
||||
return 1+v2.narg();
|
||||
}
|
||||
public LuaValue arg1() {
|
||||
return v1;
|
||||
}
|
||||
public Varargs subargs(final int start) {
|
||||
if (start == 1)
|
||||
return this;
|
||||
if (start == 2)
|
||||
return v2;
|
||||
if (start > 2)
|
||||
return v2.subargs(start - 1);
|
||||
return new SubVarargs(this, start, 2);
|
||||
}
|
||||
}
|
||||
|
||||
/** Varargs implemenation backed by an array of LuaValues
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static methods on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[])
|
||||
* @see LuaValue#varargsOf(LuaValue[], Varargs)
|
||||
*/
|
||||
static final class ArrayVarargs extends Varargs {
|
||||
private final LuaValue[] v;
|
||||
private final Varargs r;
|
||||
/** Construct a Varargs from an array of LuaValue.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static methods on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[])
|
||||
* @see LuaValue#varargsOf(LuaValue[], Varargs)
|
||||
*/
|
||||
ArrayVarargs(LuaValue[] v, Varargs r) {
|
||||
this.v = v;
|
||||
this.r = r ;
|
||||
for (int i = 0; i < v.length; ++i)
|
||||
if (v[i] == null)
|
||||
throw new IllegalArgumentException("nulls in array");
|
||||
}
|
||||
public LuaValue arg(int i) {
|
||||
return i >=1 && i<=v.length? v[i - 1]: r.arg(i-v.length);
|
||||
}
|
||||
public int narg() {
|
||||
return v.length+r.narg();
|
||||
}
|
||||
public LuaValue arg1() { return v.length>0? v[0]: r.arg1(); }
|
||||
}
|
||||
|
||||
/** Varargs implemenation backed by an array of LuaValues
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static methods on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[], int, int)
|
||||
* @see LuaValue#varargsOf(LuaValue[], int, int, Varargs)
|
||||
*/
|
||||
static final class ArrayPartVarargs extends Varargs {
|
||||
private final int offset;
|
||||
private final LuaValue[] v;
|
||||
private final int length;
|
||||
private final Varargs more;
|
||||
/** Construct a Varargs from an array of LuaValue.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static methods on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[], int, int)
|
||||
*/
|
||||
ArrayPartVarargs(LuaValue[] v, int offset, int length) {
|
||||
this.v = v;
|
||||
this.offset = offset;
|
||||
this.length = length;
|
||||
this.more = LuaValue.NONE;
|
||||
}
|
||||
/** Construct a Varargs from an array of LuaValue and additional arguments.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
* Instead use the corresponding static method on LuaValue.
|
||||
*
|
||||
* @see LuaValue#varargsOf(LuaValue[], int, int, Varargs)
|
||||
*/
|
||||
public ArrayPartVarargs(LuaValue[] v, int offset, int length, Varargs more) {
|
||||
this.v = v;
|
||||
this.offset = offset;
|
||||
this.length = length;
|
||||
this.more = more;
|
||||
}
|
||||
public LuaValue arg(int i) {
|
||||
return i>=1&&i<=length? v[i+offset-1]: more.arg(i-length);
|
||||
}
|
||||
public int narg() {
|
||||
return length + more.narg();
|
||||
}
|
||||
public LuaValue arg1() {
|
||||
return length>0? v[offset]: more.arg1();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ import org.luaj.vm2.Varargs;
|
||||
* To instantiate and use it directly,
|
||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||
* <pre> {@code
|
||||
* LuaTable _G = new LuaTable();
|
||||
* Globals _G = new Globals();
|
||||
* _G.load(new DebugLib());
|
||||
* } </pre>
|
||||
* Doing so will ensure the library is properly initialized
|
||||
@@ -112,7 +112,7 @@ public class DebugLib extends OneArgFunction {
|
||||
int lastline;
|
||||
int bytecodes;
|
||||
|
||||
public LuaTable call(LuaValue env) {
|
||||
public LuaValue call(LuaValue env) {
|
||||
globals = env.checkglobals();
|
||||
globals.debuglib = this;
|
||||
LuaTable debug = new LuaTable();
|
||||
@@ -148,7 +148,7 @@ public class DebugLib extends OneArgFunction {
|
||||
final class gethook extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
return varargsOf(
|
||||
hookfunc,
|
||||
hookfunc != null? hookfunc: NIL,
|
||||
valueOf((hookcall?"c":"")+(hookline?"l":"")+(hookrtrn?"r":"")),
|
||||
valueOf(hookcount));
|
||||
}
|
||||
@@ -237,7 +237,8 @@ public class DebugLib extends OneArgFunction {
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
|
||||
int level = args.checkint(a++);
|
||||
int local = args.checkint(a++);
|
||||
return callstack(thread).getCallFrame(level).getLocal(local);
|
||||
CallFrame f = callstack(thread).getCallFrame(level);
|
||||
return f != null? f.getLocal(local): NONE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,7 +283,7 @@ public class DebugLib extends OneArgFunction {
|
||||
|
||||
// debug.sethook ([thread,] hook, mask [, count])
|
||||
final class sethook extends VarArgFunction {
|
||||
public Varargs call(Varargs args) {
|
||||
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);
|
||||
@@ -312,7 +313,8 @@ public class DebugLib extends OneArgFunction {
|
||||
int level = args.checkint(a++);
|
||||
int local = args.checkint(a++);
|
||||
LuaValue value = args.arg(a++);
|
||||
return callstack(thread).getCallFrame(level).setLocal(local, value);
|
||||
CallFrame f = callstack(thread).getCallFrame(level);
|
||||
return f != null? f.setLocal(local, value): NONE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,7 +385,7 @@ public class DebugLib extends OneArgFunction {
|
||||
if ( func instanceof LuaClosure ) {
|
||||
LuaClosure c = (LuaClosure) func;
|
||||
if ( c.upValues != null && up > 0 && up <= c.upValues.length ) {
|
||||
return userdataOf(c.upValues[up-1].hashCode());
|
||||
return valueOf(c.upValues[up-1].hashCode());
|
||||
}
|
||||
}
|
||||
return NIL;
|
||||
@@ -397,6 +399,7 @@ public class DebugLib extends OneArgFunction {
|
||||
int n1 = args.checkint(2);
|
||||
LuaValue f2 = args.checkfunction(3);
|
||||
int n2 = args.checkint(4);
|
||||
f1.checkclosure().upValues[n1] = f2.checkclosure().upValues[n2];
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user