Lua 5.2 compatibility fixes.

This commit is contained in:
James Roseborough
2012-09-09 16:26:17 +00:00
parent 8d1333c612
commit d5456b4b93
19 changed files with 111 additions and 152 deletions

View File

@@ -182,7 +182,7 @@ public class Lua {
public static final int OP_LOADK = 1;/* A Bx R(A) := Kst(Bx) */
public static final int OP_LOADKX = 2;/* A R(A) := Kst(extra arg) */
public static final int OP_LOADBOOL = 3;/* A B C R(A) := (Bool)B; if (C) pc++ */
public static final int OP_LOADNIL = 4; /* A B R(A) := ... := R(B) := nil */
public static final int OP_LOADNIL = 4; /* A B R(A) := ... := R(A+B) := nil */
public static final int OP_GETUPVAL = 5; /* A B R(A) := UpValue[B] */
public static final int OP_GETTABUP = 6; /* A B C R(A) := UpValue[B][RK(C)] */

View File

@@ -134,12 +134,16 @@ public class LuaClosure extends LuaFunction {
public final LuaValue call() {
LuaValue[] stack = new LuaValue[p.maxstacksize];
for (int i = 0; i < p.numparams; ++i )
stack[i] = NIL;
return execute(stack,NONE).arg1();
}
public final LuaValue call(LuaValue arg) {
LuaValue[] stack = new LuaValue[p.maxstacksize];
System.arraycopy(NILS, 0, stack, 0, p.maxstacksize);
for (int i = 1; i < p.numparams; ++i )
stack[i] = NIL;
switch ( p.numparams ) {
default: stack[0]=arg; return execute(stack,NONE).arg1();
case 0: return execute(stack,arg).arg1();
@@ -148,7 +152,8 @@ public class LuaClosure extends LuaFunction {
public final LuaValue call(LuaValue arg1, LuaValue arg2) {
LuaValue[] stack = new LuaValue[p.maxstacksize];
System.arraycopy(NILS, 0, stack, 0, p.maxstacksize);
for (int i = 2; i < p.numparams; ++i )
stack[i] = NIL;
switch ( p.numparams ) {
default: stack[0]=arg1; stack[1]=arg2; return execute(stack,NONE).arg1();
case 1: stack[0]=arg1; return execute(stack,arg2).arg1();
@@ -158,7 +163,8 @@ public class LuaClosure extends LuaFunction {
public final LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
LuaValue[] stack = new LuaValue[p.maxstacksize];
System.arraycopy(NILS, 0, stack, 0, p.maxstacksize);
for (int i = 3; i < p.numparams; ++i )
stack[i] = NIL;
switch ( p.numparams ) {
default: stack[0]=arg1; stack[1]=arg2; stack[2]=arg3; return execute(stack,NONE).arg1();
case 2: stack[0]=arg1; stack[1]=arg2; return execute(stack,arg3).arg1();
@@ -173,7 +179,6 @@ public class LuaClosure extends LuaFunction {
public Varargs onInvoke(Varargs varargs) {
LuaValue[] stack = new LuaValue[p.maxstacksize];
System.arraycopy(NILS, 0, stack, 0, p.maxstacksize);
for ( int i=0; i<p.numparams; i++ )
stack[i] = varargs.arg(i+1);
return execute(stack,p.is_vararg!=0? varargs.subargs(p.numparams+1): NONE);
@@ -223,8 +228,8 @@ public class LuaClosure extends LuaFunction {
pc++; /* skip next instruction (if C) */
continue;
case Lua.OP_LOADNIL: /* A B R(A):= ...:= R(B):= nil */
for ( b=i>>>23; a<=b; )
case Lua.OP_LOADNIL: /* A B R(A):= ...:= R(A+B):= nil */
for ( b=i>>>23; b-->=0; )
stack[a++] = LuaValue.NIL;
continue;

View File

@@ -322,8 +322,11 @@ public class LuaTable extends LuaValue {
* @return The removed item, or {@link #NONE} if not removed
*/
public LuaValue remove(int pos) {
int n = length();
if ( pos == 0 )
pos = length();
pos = n;
else if (pos > n)
return NONE;
LuaValue v = rawget(pos);
for ( LuaValue r=v; !r.isnil(); ) {
r = rawget(pos+1);
@@ -366,13 +369,6 @@ public class LuaTable extends LuaValue {
return sb.tostring();
}
public LuaValue getn() {
for ( int n=getArrayLength(); n>0; --n )
if ( !rawget(n).isnil() )
return LuaInteger.valueOf(n);
return ZERO;
}
public int length() {
int a = getArrayLength();
int n = a+1,m=0;
@@ -398,27 +394,6 @@ public class LuaTable extends LuaValue {
return length();
}
/** Return table.maxn() as defined by lua 5.0.
* <p>
* Provided for compatibility, not a scalable operation.
* @return value for maxn
*/
public int maxn() {
int n = 0;
for ( int i=0; i<array.length; i++ )
if ( array[i] != null )
n = i+1;
for ( int i=0; i<hashKeys.length; i++ ) {
LuaValue v = hashKeys[i];
if ( v!=null && v.isinttype() ) {
int key = v.toint();
if ( key > n )
n = key;
}
}
return n;
}
/**
* Get the next element after a particular key in the table
* @return key,value or nil
@@ -469,36 +444,6 @@ public class LuaTable extends LuaValue {
LuaValue v = rawget(k);
return v.isnil()? NONE: varargsOf(LuaInteger.valueOf(k),v);
}
/**
* Call the supplied function once for each key-value pair
*
* @param func function to call
*/
public LuaValue foreach(LuaValue func) {
Varargs n;
LuaValue k = NIL;
LuaValue v;
while ( !(k = ((n = next(k)).arg1())).isnil() )
if ( ! (v = func.call(k, n.arg(2))).isnil() )
return v;
return NIL;
}
/**
* Call the supplied function once for each key-value pair
* in the contiguous array part
*
* @param func
*/
public LuaValue foreachi(LuaValue func) {
LuaValue v,r;
for ( int k=0; !(v = rawget(++k)).isnil(); )
if ( ! (r = func.call(valueOf(k), v)).isnil() )
return r;
return NIL;
}
/**
* Set a hashtable value

View File

@@ -2014,12 +2014,6 @@ public class LuaValue extends Varargs {
* @throws LuaError if {@code this} is not a table or string.
*/
public int rawlen() { typerror("table or string"); return 0; }
/** Implementation of lua 5.0 getn() function.
* @return value of getn() as defined in lua 5.0 spec if {@code this} is a {@link LuaTable}
* @throws LuaError if {@code this} is not a {@link LuaTable}
*/
public LuaValue getn() { return typerror("getn"); }
// object equality, used for key comparison
public boolean equals(Object obj) { return this == obj; }

View File

@@ -185,11 +185,6 @@ public class WeakTable extends LuaTable {
}
return i;
}
public int maxn() {
return super.maxn();
}
/**
* Get the next element after a particular key in the table

View File

@@ -63,9 +63,9 @@ public class TableLib extends OneArgFunction {
private LuaTable init(LuaValue env) {
LuaTable t = new LuaTable();
bind(t, TableLib.class, new String[] { "getn", "maxn", }, 1 );
bind(t, TableLib.class, new String[] {}, 1 );
bind(t, TableLibV.class, new String[] {
"remove", "concat", "insert", "sort", "foreach", "foreachi", "unpack", } );
"remove", "concat", "insert", "sort", "unpack", } );
env.set("table", t);
PackageLib.instance.LOADED.set("table", t);
return t;
@@ -75,10 +75,6 @@ public class TableLib extends OneArgFunction {
switch ( opcode ) {
case 0: // init library
return init(arg);
case 1: // "getn" (table) -> number
return arg.checktable().getn();
case 2: // "maxn" (table) -> number
return valueOf( arg.checktable().maxn());
}
return NIL;
}
@@ -111,13 +107,7 @@ public class TableLib extends OneArgFunction {
table.sort( compare );
return NONE;
}
case 4: { // (table, func) -> void
return args.checktable(1).foreach( args.checkfunction(2) );
}
case 5: { // "foreachi" (table, func) -> void
return args.checktable(1).foreachi( args.checkfunction(2) );
}
case 6: // "unpack", // (list [,i [,j]]) -> result1, ...
case 4: // "unpack", // (list [,i [,j]]) -> result1, ...
{
LuaTable t = args.checktable(1);
switch (args.narg()) {

View File

@@ -122,10 +122,10 @@ public class JavaGen {
builder.storeLocal( pc, a );
break;
case Lua.OP_LOADNIL: /* A B R(A):= ...:= R(B):= nil */
case Lua.OP_LOADNIL: /* A B R(A):= ...:= R(A+B):= nil */
builder.loadNil();
for ( ; a<=b; a++ ) {
if ( a < b )
for ( ; b>=0; a++, b-- ) {
if ( b > 0 )
builder.dup();
builder.storeLocal( pc, a );
}

View File

@@ -265,10 +265,10 @@ public class ProtoInfo {
v[a+3][pc] = new VarInfo(a+3,pc);
break;
case Lua.OP_LOADNIL: /* A B R(A) := ... := R(B) := nil */
case Lua.OP_LOADNIL: /* A B R(A) := ... := R(A+B) := nil */
a = Lua.GETARG_A( ins );
b = Lua.GETARG_B( ins );
for ( ; a<=b; a++ )
for ( ; b-->=0; a++ )
v[a][pc] = new VarInfo(a,pc);
break;