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];