Fix compatibility vararg support when 'arg' variable is used.

This commit is contained in:
James Roseborough
2009-01-29 17:53:49 +00:00
parent 241f139a68
commit ddcd496b7a
4 changed files with 18 additions and 11 deletions

View File

@@ -58,11 +58,6 @@ public class LuaC extends Lua implements LuaCompiler {
static final int LUAI_MAXVARS = 200;
static final int NO_REG = MAXARG_A;
/* masks for new-style vararg */
static final int VARARG_HASARG = 1;
static final int VARARG_ISVARARG = 2;
static final int VARARG_NEEDSARG = 4;
/* OpMode - basic instruction format */
static final int

View File

@@ -33,6 +33,10 @@ public class Lua {
/** use return values from previous op */
public static final int LUA_MULTRET = -1;
/** masks for new-style vararg */
public static final int VARARG_HASARG = 1;
public static final int VARARG_ISVARARG = 2;
public static final int VARARG_NEEDSARG = 4;
// from lopcodes.h

View File

@@ -171,12 +171,22 @@ public class LuaState extends Lua {
int nfix = Math.min(narg, npar);
int nvar = Math.max(0, narg-nfix);
// must copy args into position, add number parameter
stack[top] = LInteger.valueOf(nvar);
System.arraycopy(stack, base+1, stack, top+1, nfix);
base = top + 1;
top = base + nfix;
luaV_adjusttop( base + npar );
// add 'arg' compatibility variable
if ( (c.p.is_vararg & VARARG_NEEDSARG) != 0 ) {
LTable arg = new LTable();
for ( int i=1,j=base-nvar-1; i<=nvar; i++, j++ )
arg.put(i, stack[j]);
arg.put("n", nvar);
pushlvalue( arg );
}
}
final int newcc = cc + 1;
if ( newcc >= calls.length ) {

View File

@@ -5,18 +5,16 @@ function p(a,...)
print("...,a",...,a)
print("a,...",a,...)
end
--[[ -- these have semantics that depend on compatibity flags
function q(a,...)
print("a,arg[1],arg[2],arg[3]",a,arg[1],arg[2],arg[3])
print("a,arg[1],arg[2],arg[3]",a,arg.n,arg[1],arg[2],arg[3])
end
function r(a,...)
print("a,arg[1],arg[2],arg[3]",a,arg[1],arg[2],arg[3])
print("a,arg",a,arg)
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
--]]
function s(a)
local arg = { '1', '2', '3' }
print("a,arg[1],arg[2],arg[3]",a,arg[1],arg[2],arg[3])
@@ -43,7 +41,7 @@ function tryall(f,name)
print( ' ->', pcall( f, "q", "r", "s" ) )
end
tryall(p,'p')
-- tryall(q,'q')
-- tryall(r,'r')
tryall(q,'q')
tryall(r,'r')
tryall(s,'s')
tryall(t,'t')