Fix JsePlatform.luaMain() to provide an "arg" table in the chunk's environment.

This commit is contained in:
James Roseborough
2018-09-13 11:53:00 +02:00
parent ebe45e4a8a
commit 828e4be019
6 changed files with 59 additions and 25 deletions

View File

@@ -97,14 +97,19 @@ public class LuaClosure extends LuaFunction {
*/
public LuaClosure(Prototype p, LuaValue env) {
this.p = p;
this.initupvalue1(env);
globals = env instanceof Globals? (Globals) env: null;
}
public void initupvalue1(LuaValue env) {
if (p.upvalues == null || p.upvalues.length == 0)
this.upValues = NOUPVALUES;
else {
this.upValues = new UpValue[p.upvalues.length];
this.upValues[0] = new UpValue(new LuaValue[] {env}, 0);
}
globals = env instanceof Globals? (Globals) env: null;
}
public boolean isclosure() {
return true;

View File

@@ -25,6 +25,7 @@ import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LuaThread;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.Bit32Lib;
import org.luaj.vm2.lib.CoroutineLib;
@@ -124,10 +125,11 @@ public class JsePlatform {
/** Simple wrapper for invoking a lua function with command line arguments.
* The supplied function is first given a new Globals object,
* then the program is run with arguments.
* The supplied function is first given a new Globals object as its environment
* then the program is run with arguments.
* @return {@link Varargs} containing any values returned by mainChunk.
*/
public static void luaMain(LuaValue mainChunk, String[] args) {
public static Varargs luaMain(LuaValue mainChunk, String[] args) {
Globals g = standardGlobals();
int n = args.length;
LuaValue[] vargs = new LuaValue[args.length];
@@ -137,6 +139,6 @@ public class JsePlatform {
arg.set("n", n);
g.set("arg", arg);
mainChunk.initupvalue1(g);
mainChunk.invoke(LuaValue.varargsOf(vargs));
return mainChunk.invoke(LuaValue.varargsOf(vargs));
}
}