Improve vararg handling logic.

This commit is contained in:
James Roseborough
2007-06-12 06:22:55 +00:00
parent da6f63bb3d
commit b6f123d5e5
3 changed files with 21 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ final class Builtin extends LFunction {
// perform a lua call // perform a lua call
public void luaStackCall(StackState state, int base) { public void luaStackCall(StackState state, int base) {
int returnValues = 0;
switch ( id ) { switch ( id ) {
case PRINT: case PRINT:
for ( int i=base+1, n=state.top; i<n; i++ ) { for ( int i=base+1, n=state.top; i<n; i++ ) {
@@ -39,16 +40,17 @@ final class Builtin extends LFunction {
System.out.print( "\t" ); System.out.print( "\t" );
} }
System.out.println(); System.out.println();
return; break;
case PAIRS: case PAIRS:
state.adjustTop(base+2); state.adjustTop(base+2);
LValue value = state.stack[base+1].luaPairs(); LValue value = state.stack[base+1].luaPairs();
state.stack[base] = value; state.stack[base] = value;
state.adjustTop(base+1); returnValues = 1;
return; break;
default: default:
luaUnsupportedOperation(); luaUnsupportedOperation();
} }
state.adjustTop(base-1+returnValues);
} }
} }

View File

@@ -18,6 +18,7 @@ public class Closure extends LValue {
// perform a lua call // perform a lua call
public void luaStackCall(StackState state, int base) { public void luaStackCall(StackState state, int base) {
if ( (! p.is_vararg) || (state.top < base+1+p.numparams) )
state.adjustTop( base+1+p.numparams ); state.adjustTop( base+1+p.numparams );
state.vmExecute( this, base+1 ); state.vmExecute( this, base+1 );
} }

View File

@@ -39,3 +39,17 @@ print( func(11, 12, 13) )
print( func(23, 22, 21) ) print( func(23, 22, 21) )
print( func(func(32,33,34), func(45,46,47), func(58,59,50)) ) print( func(func(32,33,34), func(45,46,47), func(58,59,50)) )
--[[
function p(a,...)
print("a",a)
print("a,...",a,...)
print("...",...)
print("...,a",...,a)
end
local aa,bb,cc,dd
p()
p("q")
p("q","r")
p("q","r","s")
print(aa,bb,cc,dd)
--]]