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

12
test/java/Model.java Normal file
View File

@@ -0,0 +1,12 @@
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.VarArgFunction;
public class Model extends VarArgFunction {
LuaValue[] u0;
public void initupvalue1(LuaValue env) {
u0 = this.newupl(env);
}
}

View File

@@ -0,0 +1,61 @@
package org.luaj.luajc;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
public class SampleMainChunk extends VarArgFunction {
static final LuaValue $print = valueOf("print");
static final LuaValue $foo = valueOf("foo");
LuaValue[] rw_ENV; // The environment when it is read-write
// LuaValue ro_ENV; // The environment when it is read-only in all sub-functions
LuaValue[] rw_openup1; // upvalue that we create and modify in "slot" 1, passed to sub-function in initer.
LuaValue[] rw_openup2; // array is instantiated on first set or before supply to closure, after that value is get, set.
LuaValue[] rw_openup3; // closing these nulls them out, sub-functions still retain references to array & can use
LuaValue ro_openup4; // open upvalue that is read-only once it is supplied to an inner function.
LuaValue ro_openup5; // closing this also nulls it out.
// Must have this in the main chunk so it can be loaded and instantiated on all platforms.
public SampleMainChunk() {
}
public void initupvalue1(LuaValue[] v) {
this.rw_ENV = v;
}
public Varargs invoke(Varargs args) {
rw_ENV[0].get($print).call($foo);
rw_ENV[0].set($print, new InnerFunction(rw_openup3, rw_openup1, ro_openup5));
return null;
}
static class InnerFunction extends TwoArgFunction {
static final LuaValue $print = valueOf("print"); // A constant, named for what it is.
static final LuaValue $foo = valueOf("foo");
final LuaValue[] rw_upvalue1; // from enclosing function, corresponds to upvaldesc not instack.
final LuaValue[] rw_upvalue2; // from enclosing function, corresponds to upvaldesc not instack.
final LuaValue ro_upvalue3; // from enclosing function, but read-only everywhere.
LuaValue[] rw_openup1; // closing these nulls them out, sub-functions still retain references to array & can use
LuaValue ro_openup2; // open upvalue that is read-only once it is supplied to an inner function.
InnerFunction(LuaValue[] rw_upvalue1, LuaValue[] rw_upvalue2, LuaValue ro_upvalue3) {
this.rw_upvalue1 = rw_upvalue1;
this.rw_upvalue2 = rw_upvalue2;
this.ro_upvalue3 = ro_upvalue3;
}
public LuaValue call(LuaValue arg1, LuaValue arg2) {
return NIL;
}
}
}

View File

@@ -37,12 +37,7 @@ public class TestLuaJC {
// create the script
public static String name = "script";
public static String script =
"local t = a or nil\n"+
"local t\n" +
"b = function()\n"+
" return t\n"+
"end\n"+
"return t\n"+
"_ENV={}; return _ENV\n"+
"";
public static void main(String[] args) throws Exception {
@@ -51,6 +46,8 @@ public class TestLuaJC {
// create an environment to run in
LuaTable _G = JsePlatform.standardGlobals();
System.out.println("_G: "+_G);
System.out.println("_G.print: "+_G.get("print"));
// compile into a chunk, or load as a class
LuaValue chunk;
@@ -60,7 +57,6 @@ public class TestLuaJC {
} else {
chunk = (LuaValue) Class.forName("script").newInstance();
}
//chunk.setfenv(_G); // TODO: convert to setupvalue()?
// call with arguments
LuaValue[] vargs = new LuaValue[args.length];

View File

@@ -87,6 +87,13 @@ public class FragmentsTest extends TestSuite {
}
}
public void testFirstArgNilExtended() {
runFragment( LuaValue.NIL,
"function f1(a) print( 'f1:', a ) return a end\n" +
"b = f1()\n" +
"return b" );
}
public void testForloopParamUpvalues() {
runFragment( LuaValue.varargsOf(new LuaValue[] {
LuaValue.valueOf(77),
@@ -119,7 +126,7 @@ public class FragmentsTest extends TestSuite {
"end\n" +
"return v('abc')\n" );
}
public void testSetlistVarargs() {
runFragment( LuaValue.valueOf("abc"),
"local f = function() return 'abc' end\n" +

View File

@@ -225,7 +225,6 @@ public class TableArrayTest extends TestCase {
LuaValue v = LuaString.valueOf( "Test Value! "+i );
t.set( i, v );
assertEquals( i, t.length() );
assertEquals( i, t.maxn() );
}
}
@@ -237,7 +236,6 @@ public class TableArrayTest extends TestCase {
t.set( i, LuaString.valueOf( "Test Value! "+i ) );
}
assertEquals( j, t.length() );
assertEquals( j, t.maxn() );
}
}
@@ -247,7 +245,6 @@ public class TableArrayTest extends TestCase {
for ( int i = 1; i <= 32; ++i ) {
t.set( "str-"+i, LuaString.valueOf( "String Key Test Value! "+i ) );
assertEquals( 0, t.length() );
assertEquals( 0, t.maxn() );
}
}
@@ -258,7 +255,6 @@ public class TableArrayTest extends TestCase {
t.set( "str-"+i, LuaString.valueOf( "String Key Test Value! "+i ) );
t.set( i, LuaString.valueOf( "Int Key Test Value! "+i ) );
assertEquals( i, t.length() );
assertEquals( i, t.maxn() );
}
}

View File

@@ -228,7 +228,6 @@ public class TableTest extends TestCase {
for ( int i = 1; i <= 32; ++i ) {
t.set( i, LuaValue.valueOf( "Test Value! "+i ) );
assertEquals( i, t.length() );
assertEquals( i, t.maxn() );
}
}
@@ -240,7 +239,6 @@ public class TableTest extends TestCase {
t.set( i, LuaValue.valueOf( "Test Value! "+i ) );
}
assertEquals( j, t.length() );
assertEquals( j, t.maxn() );
}
}
@@ -250,7 +248,6 @@ public class TableTest extends TestCase {
for ( int i = 1; i <= 32; ++i ) {
t.set( "str-"+i, LuaValue.valueOf( "String Key Test Value! "+i ) );
assertEquals( 0, t.length() );
assertEquals( 0, t.maxn() );
}
}
@@ -261,7 +258,6 @@ public class TableTest extends TestCase {
t.set( "str-"+i, LuaValue.valueOf( "String Key Test Value! "+i ) );
t.set( i, LuaValue.valueOf( "Int Key Test Value! "+i ) );
assertEquals( i, t.length() );
assertEquals( i, t.maxn() );
}
}

View File

@@ -122,7 +122,7 @@ local fields = { 'source', 'short_src', 'what',
'currentline', 'linedefined', 'lastlinedefined',
'nups', 'func', 'activelines' }
local printinfo = function(...)
for i,a in ipairs(arg) do
for i,a in ipairs({...}) do
if type(a) == 'table' then
for j,field in ipairs(fields) do
printfield( a, field)

View File

@@ -29,11 +29,6 @@ checkallpass('table.insert',{sometable,somei,notanil})
checkallerrors('table.insert',{notatable,somestring},'bad argument')
checkallerrors('table.insert',{sometable,notij,notanil},'bad argument')
-- table.maxn
banner('table.maxn')
checkallpass('table.maxn',{sometable})
checkallerrors('table.maxn',{notatable},'bad argument')
-- table.remove
banner('table.remove')
checkallpass('table.remove',{sometable})

View File

@@ -52,7 +52,7 @@ function eles(t,f)
return "{"..table.concat(all,',').."}"
end
-- insert, maxn
-- insert, len
print( '-- insert, len tests' )
local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' }
@@ -105,40 +105,6 @@ sorttest{ "one", "two", "three" }
sorttest{ "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" }
sorttest( { "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" }, function(a,b) return b<a end)
-- getn
t0 = {}
t1 = { 'one', 'two', 'three' }
t2 = { a1='aa', a2='bb', a3='cc' }
t3 = { 'one', 'two', 'three', a1='aa', a2='bb', a3='cc' }
print( 'getn('..eles(t0)..')', pcall( table.getn, t0 ) )
print( 'getn('..eles(t1)..')', pcall( table.getn, t1 ) )
print( 'getn('..eles(t2)..')', pcall( table.getn, t2 ) )
print( 'getn('..eles(t3)..')', pcall( table.getn, t3 ) )
-- foreach
function test( f, t, result, name )
status, value = pcall( f, t, function(...)
print(' -- ',...)
print(' next',next(t,(...)))
return result
end )
print( name, 's,v', status, value )
end
function testall( f, t, name )
test( f, t, nil, name..'nil' )
test( f, t, false, name..'fls' )
test( f, t, 100, name..'100' )
end
testall( table.foreach, t0, 'table.foreach('..eles(t0)..')' )
testall( table.foreach, t1, 'table.foreach('..eles(t1)..')' )
testall( table.foreach, t2, 'table.foreach('..eles(t2)..')' )
testall( table.foreach, t3, 'table.foreach('..eles(t3)..')' )
testall( table.foreachi, t0, 'table.foreachi('..eles(t0)..')' )
testall( table.foreachi, t1, 'table.foreachi('..eles(t1)..')' )
testall( table.foreachi, t2, 'table.foreachi('..eles(t2)..')' )
testall( table.foreachi, t3, 'table.foreachi('..eles(t3)..')' )
-- pairs, ipairs
--[[
function testpairs(f, t, name)

View File

@@ -50,7 +50,7 @@ local function varargstest()
print("a,arg[1],arg[2],arg[3]",a,arg.n,arg[1],arg[2],arg[3])
end
function r(a,...)
print("a,arg",a,arg)
print("a,arg[1],arg[2],arg[3]",a,arg.n,arg[1],arg[2],arg[3])
print("a",a)
print("...",...)
print("...,a",...,a)