Fix lua command vararg values passed into main script to match what is in global arg table

This commit is contained in:
James Roseborough
2011-11-12 23:59:37 +00:00
parent 3a266748be
commit 4950d3c612
2 changed files with 17 additions and 18 deletions

View File

@@ -731,6 +731,7 @@ and LuaForge:
<li>Add lib/luaj-sources-2.0.2.jar for easier integration into an IDE such as Netbeans </li> <li>Add lib/luaj-sources-2.0.2.jar for easier integration into an IDE such as Netbeans </li>
<tr valign="top"><td>&nbsp;&nbsp;<b>2.0.3</b></td><td><ul> <tr valign="top"><td>&nbsp;&nbsp;<b>2.0.3</b></td><td><ul>
<li>Improve coroutine state logic including let unreferenced coroutines be garbage collected </li> <li>Improve coroutine state logic including let unreferenced coroutines be garbage collected </li>
<li>Fix lua command vararg values passed into main script to match what is in global arg table </li>
</ul></td></tr> </ul></td></tr>
</table></td></tr></table> </table></td></tr></table>

View File

@@ -33,6 +33,7 @@ import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaFunction; import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.jse.JsePlatform; import org.luaj.vm2.lib.jse.JsePlatform;
import org.luaj.vm2.lua2java.Lua2Java; import org.luaj.vm2.lua2java.Lua2Java;
import org.luaj.vm2.luajc.LuaJC; import org.luaj.vm2.luajc.LuaJC;
@@ -138,12 +139,10 @@ public class lua {
processing = true; processing = true;
for ( int i=0; i<args.length; i++ ) { for ( int i=0; i<args.length; i++ ) {
if ( ! processing || ! args[i].startsWith("-") ) { if ( ! processing || ! args[i].startsWith("-") ) {
setGlobalArg( _G, args, i ); processScript( new FileInputStream(args[i]), args[i], args, i );
processScript( new FileInputStream(args[i]), args[i] );
break; break;
} else if ( "-".equals( args[i] ) ) { } else if ( "-".equals( args[i] ) ) {
setGlobalArg( _G, args, i ); processScript( System.in, "=stdin", args, i );
processScript( System.in, "=stdin" );
break; break;
} else { } else {
switch ( args[i].charAt(1) ) { switch ( args[i].charAt(1) ) {
@@ -152,8 +151,7 @@ public class lua {
break; break;
case 'e': case 'e':
++i; ++i;
setGlobalArg( _G, args, i ); processScript( new ByteArrayInputStream(args[i].getBytes()), "string", args, i );
processScript( new ByteArrayInputStream(args[i].getBytes()), "string" );
break; break;
case '-': case '-':
processing = false; processing = false;
@@ -171,13 +169,6 @@ public class lua {
} }
} }
private static void setGlobalArg(LuaValue _g2, String[] args, int i) {
LuaTable arg = LuaValue.tableOf();
for ( int j=0; j<args.length; j++ )
arg.set( j-i, LuaValue.valueOf(args[j]) );
_G.set( "arg", arg );
}
private static void loadLibrary( String libname ) throws IOException { private static void loadLibrary( String libname ) throws IOException {
LuaValue slibname =LuaValue.valueOf(libname); LuaValue slibname =LuaValue.valueOf(libname);
try { try {
@@ -195,7 +186,7 @@ public class lua {
} }
} }
private static void processScript( InputStream script, String chunkname ) throws IOException { private static void processScript( InputStream script, String chunkname, String[] args, int firstarg ) throws IOException {
try { try {
LuaFunction c; LuaFunction c;
try { try {
@@ -203,14 +194,21 @@ public class lua {
} finally { } finally {
script.close(); script.close();
} }
c.invoke( LuaValue.valueOf(chunkname) ); Varargs scriptargs = (args!=null? setGlobalArg(args, firstarg): LuaValue.NONE);
c.invoke( scriptargs );
} catch ( Throwable t ) { } catch ( Throwable t ) {
t.printStackTrace( System.err ); t.printStackTrace( System.err );
} }
} }
private static final String[] NOARGS = {}; private static Varargs setGlobalArg(String[] args, int i) {
LuaTable arg = LuaValue.tableOf();
for ( int j=0; j<args.length; j++ )
arg.set( j-i, LuaValue.valueOf(args[j]) );
_G.set( "arg", arg );
return _G.get("unpack").invoke(arg);
}
private static void interactiveMode( ) throws IOException { private static void interactiveMode( ) throws IOException {
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ); BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
while ( true ) { while ( true ) {
@@ -219,7 +217,7 @@ public class lua {
String line = reader.readLine(); String line = reader.readLine();
if ( line == null ) if ( line == null )
return; return;
processScript( new ByteArrayInputStream(line.getBytes()), "=stdin" ); processScript( new ByteArrayInputStream(line.getBytes()), "=stdin", null, 0 );
} }
} }
} }