Lua 5.2 compatibility fixes.

This commit is contained in:
James Roseborough
2012-09-05 15:34:38 +00:00
parent 1fd69ed62d
commit 41d9dd6176
2 changed files with 14 additions and 62 deletions

View File

@@ -426,22 +426,15 @@ public class LuaClosure extends LuaFunction {
case Lua.OP_TFORCALL: /* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
v = stack[a].invoke(varargsOf(stack[a+1],stack[a+2]));
c = (i>>14) & 0x1ff;
while (--c >= 0)
stack[a+3+c] = v.arg(c+1);
continue;
case Lua.OP_TFORLOOP: /*
* A C R(A+3), ... ,R(A+2+C):= R(A)(R(A+1),
* R(A+2)): if R(A+3) ~= nil then R(A+2)=R(A+3)
* else pc++
*/
// TODO: stack call on for loop body, such as: stack[a].call(ci);
v = stack[a].invoke(varargsOf(stack[a+1],stack[a+2]));
if ( (o=v.arg1()).isnil() )
++pc;
else {
stack[a+2] = stack[a+3] = o;
for ( c=(i>>14)&0x1ff; c>1; --c )
stack[a+2+c] = v.arg(c);
v = NONE; // todo: necessary?
case Lua.OP_TFORLOOP: /* A sBx if R(A) != nil then ps+= sBx */
if (!stack[a+1].isnil()) { /* continue loop? */
stack[a] = stack[a+1]; /* save control varible. */
pc += (i>>>14)-0x1ffff;
}
continue;
@@ -514,15 +507,13 @@ public class LuaClosure extends LuaFunction {
}
private UpValue findupval(LuaValue[] stack, short idx, UpValue[] openups) {
if (idx <= 0)
error("Upvalue index must be > 0");
final int n = openups.length;
for (int i = 0; i < n; ++i)
if (openups[i] != null && openups[i].index == idx)
return openups[i];
for (int i = 0; i < n; ++i)
if (openups[i] == null)
return openups[idx] = new UpValue(stack, idx);
return openups[i] = new UpValue(stack, idx);
this.error("No space for upvalue");
return null;
}

View File

@@ -71,7 +71,7 @@ public class FragmentsTest extends TestSuite {
public void runFragment( Varargs expected, String script ) {
try {
String name = getName();
LuaTable _G = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
LuaTable _G = org.luaj.vm2.lib.jse.JsePlatform.debugGlobals();
InputStream is = new ByteArrayInputStream(script.getBytes("UTF-8"));
LuaValue chunk ;
switch ( TEST_TYPE ) {
@@ -83,6 +83,7 @@ public class FragmentsTest extends TestSuite {
break;
default:
chunk = LuaC.instance.load( is, name, _G );
Print.print(((LuaClosure)chunk).p);
break;
}
Varargs actual = chunk.invoke();
@@ -109,35 +110,10 @@ public class FragmentsTest extends TestSuite {
"end\n");
}
public void testVarVarargsUseArg() {
runFragment( LuaValue.varargsOf( new LuaValue[] {
LuaValue.valueOf("a"),
LuaValue.valueOf(2),
LuaValue.valueOf("b"),
LuaValue.valueOf("c"),
LuaValue.NIL }),
"function q(a,...)\n" +
" return a,arg.n,arg[1],arg[2],arg[3]\n" +
"end\n" +
"return q('a','b','c')\n" );
}
public void testVarVarargsUseBoth() {
runFragment( LuaValue.varargsOf( new LuaValue[] {
LuaValue.valueOf("a"),
LuaValue.valueOf("nil"),
LuaValue.valueOf("b"),
LuaValue.valueOf("c")}),
"function r(a,...)\n" +
" return a,type(arg),...\n" +
"end\n" +
"return r('a','b','c')\n" );
}
public void testArgVarargsUseBoth() {
runFragment( LuaValue.varargsOf( new LuaValue[] {
LuaValue.NIL,
LuaValue.valueOf("a"),
LuaValue.valueOf("b"),
LuaValue.valueOf("c")}),
"function v(arg,...)\n" +
@@ -148,7 +124,7 @@ public class FragmentsTest extends TestSuite {
public void testArgParamUseNone() {
// the name "arg" is treated specially, and ends up masking the argument value in 5.1
runFragment( LuaValue.valueOf("table"),
runFragment( LuaValue.valueOf("string"),
"function v(arg,...)\n" +
" return type(arg)\n" +
"end\n" +
@@ -197,22 +173,7 @@ public class FragmentsTest extends TestSuite {
"print( 'c=', c )\n" +
"return c\n" );
}
public void testNeedsArgAndHasArg() {
runFragment( LuaValue.varargsOf(LuaValue.valueOf(333),LuaValue.NIL,LuaValue.valueOf(222)),
"function r(q,...)\n"+
" local a=arg\n"+
" return a and a[2]\n"+
"end\n" +
"function s(q,...)\n"+
" local a=arg\n"+
" local b=...\n"+
" return a and a[2],b\n"+
"end\n" +
"return r(111,222,333),s(111,222,333)" );
}
public void testNonAsciiStringLiterals() {
runFragment( LuaValue.valueOf("7,8,12,10,9,11,133,222"),
"local a='\\a\\b\\f\\n\\t\\v\\133\\222'\n"+