Add arg table to globals when executing from command line.

This commit is contained in:
James Roseborough
2009-10-31 05:41:35 +00:00
parent 97e6da241b
commit 05e6fa5774

View File

@@ -30,9 +30,10 @@ import java.io.InputStreamReader;
import org.luaj.vm2.LoadState; import org.luaj.vm2.LoadState;
import org.luaj.vm2.Lua; import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaFunction; import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.compiler.LuaC; import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.DebugLib;
import org.luaj.vm2.lib.JsePlatform; import org.luaj.vm2.lib.JsePlatform;
import org.luaj.vm2.luajc.JavaBytecodeCompiler; import org.luaj.vm2.luajc.JavaBytecodeCompiler;
@@ -65,7 +66,6 @@ public class lua {
// new lua state // new lua state
_G = JsePlatform.standardGlobals(); _G = JsePlatform.standardGlobals();
DebugLib.install( _G );
LuaC.install(); LuaC.install();
// process args // process args
@@ -88,6 +88,9 @@ public class lua {
usageExit(); usageExit();
// input script - defer to last stage // input script - defer to last stage
break; break;
case 'j':
JavaBytecodeCompiler.install();
break;
case 'l': case 'l':
if ( ++i >= args.length ) if ( ++i >= args.length )
usageExit(); usageExit();
@@ -99,9 +102,6 @@ public class lua {
case 'v': case 'v':
versioninfo = true; versioninfo = true;
break; break;
case 'j':
JavaBytecodeCompiler.install();
break;
case '-': case '-':
if ( args[i].length() > 2 ) if ( args[i].length() > 2 )
usageExit(); usageExit();
@@ -122,10 +122,12 @@ 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("-") ) {
processScript( new FileInputStream(args[i]), args[i], args, i+1 ); setGlobalArg( _G, args, i );
processScript( new FileInputStream(args[i]), args[i] );
break; break;
} else if ( args[i].length() <= 1 ) { } else if ( "-".equals( args[i] ) ) {
processScript( System.in, "-", args, i+1 ); setGlobalArg( _G, args, i );
processScript( System.in, "stdin" );
break; break;
} else { } else {
switch ( args[i].charAt(1) ) { switch ( args[i].charAt(1) ) {
@@ -134,7 +136,8 @@ public class lua {
break; break;
case 'e': case 'e':
++i; ++i;
processScript( new ByteArrayInputStream(args[i].getBytes()), args[i], null, 0 ); setGlobalArg( _G, args, i );
processScript( new ByteArrayInputStream(args[i].getBytes()), "string" );
break; break;
case '-': case '-':
processing = false; processing = false;
@@ -152,6 +155,13 @@ 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 {
@@ -169,7 +179,7 @@ public class lua {
} }
} }
private static void processScript( InputStream script, String chunkname, String[] args, int offset ) throws IOException { private static void processScript( InputStream script, String chunkname ) throws IOException {
try { try {
LuaFunction c; LuaFunction c;
try { try {
@@ -177,10 +187,7 @@ public class lua {
} finally { } finally {
script.close(); script.close();
} }
LuaValue[] a = new LuaValue[args.length-offset]; c.invoke( LuaValue.valueOf(chunkname) );
for ( int i=0; i<a.length; i++ )
a[i] = LuaValue.valueOf(args[offset+i]);
c.invoke( a );
} catch ( Throwable t ) { } catch ( Throwable t ) {
t.printStackTrace( System.err ); t.printStackTrace( System.err );
} }
@@ -196,7 +203,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()), "-", NOARGS, 0 ); processScript( new ByteArrayInputStream(line.getBytes()), "stdin" );
} }
} }
} }