Fix midlet example including luajc-compiled scripts

This commit is contained in:
James Roseborough
2010-04-25 05:35:24 +00:00
parent 90ca65c7d9
commit de9c967ac5
3 changed files with 96 additions and 73 deletions

View File

@@ -24,18 +24,16 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.luaj.vm2.Lua;
import org.luaj.vm2.compiler.DumpState;
import org.luaj.vm2.luajc.LuaJC;
/**
* Compiler for lua files to compile lua sources into java sources.
* Compiler for lua files to compile lua sources or lua binaries into java classes.
*/
public class luajc {
private static final String version = Lua._VERSION + "Copyright (C) 2009 luaj.org";
@@ -44,9 +42,9 @@ public class luajc {
"usage: java -cp luaj-jse.jar,bcel-5.2.jar luajc [options] [filenames].\n" +
"Available options are:\n" +
" - process stdin\n" +
" -o name output to file 'name' (default is \"luac.out\")\n" +
" -p parse only\n" +
" -s strip debug information\n" +
" -s source directory\n" +
" -d destination directory\n" +
" -n no debug information (strip debug)\n" +
" -e little endian format for numbers\n" +
" -i<n> number format 'n', (n=0,1 or 4, default="+DumpState.NUMBER_FORMAT_DEFAULT+")\n" +
" -v show version information\n" +
@@ -57,13 +55,12 @@ public class luajc {
System.exit(-1);
}
private String output = "luacj.out";
private boolean parseonly = false;
private String srcdir = null;
private String destdir = null;
private boolean stripdebug = false;
private boolean littleendian = false;
private int numberformat = DumpState.NUMBER_FORMAT_DEFAULT;
private boolean versioninfo = false;
private boolean processing = true;
private boolean verbose = false;
public static void main( String[] args ) throws IOException {
new luajc( args );
@@ -73,23 +70,25 @@ public class luajc {
// process args
try {
Vector files = new Vector();
// get stateful args
for ( int i=0; i<args.length; i++ ) {
if ( ! processing || ! args[i].startsWith("-") ) {
// input file - defer to next stage
} else if ( args[i].length() <= 1 ) {
// input file - defer to next stage
if ( ! args[i].startsWith("-") ) {
files.add(args[i]);
} else {
switch ( args[i].charAt(1) ) {
case 'o':
case 's':
if ( ++i >= args.length )
usageExit();
output = args[i];
srcdir = args[i];
break;
case 'p':
parseonly = true;
case 'd':
if ( ++i >= args.length )
usageExit();
destdir = args[i];
break;
case 's':
case 'n':
stripdebug = true;
break;
case 'e':
@@ -101,12 +100,7 @@ public class luajc {
numberformat = Integer.parseInt(args[i].substring(2));
break;
case 'v':
versioninfo = true;
break;
case '-':
if ( args[i].length() > 2 )
usageExit();
processing = false;
verbose = true;
break;
default:
usageExit();
@@ -116,34 +110,28 @@ public class luajc {
}
// echo version
if ( versioninfo )
if ( verbose ) {
System.out.println(version);
System.out.println("srcdir: "+srcdir);
System.out.println("destdir: "+srcdir);
System.out.println("stripdebug: "+stripdebug);
System.out.println("littleendian: "+littleendian);
System.out.println("numberformat: "+numberformat);
System.out.println("files: "+files);
}
// open output file
OutputStream fos = new FileOutputStream( output );
// process input files
try {
processing = true;
for ( int i=0; i<args.length; i++ ) {
if ( ! processing || ! args[i].startsWith("-") ) {
String chunkname = args[i];
processScript( new FileInputStream(args[i]), chunkname, fos );
} else if ( args[i].length() <= 1 ) {
processScript( System.in, "stdin", fos );
} else {
switch ( args[i].charAt(1) ) {
case 'o':
++i;
break;
case '-':
processing = false;
break;
}
}
}
} finally {
fos.close();
for ( int i=0; i<files.size(); i++ ) {
String filename = (String) files.elementAt(i);
int index = filename.lastIndexOf('.');
if ( index < 0 )
usageExit();
String chunkname = filename.substring(0,index);
String sourcepath = srcdir!=null? srcdir+"/"+filename: filename;
if ( verbose )
System.out.println("filename="+filename+" chunkname="+filename+" sourcepath="+sourcepath);
InputStream is = new FileInputStream( sourcepath );
processScript( is, chunkname, filename );
}
} catch ( IOException ioe ) {
@@ -152,23 +140,25 @@ public class luajc {
}
}
private void processScript( InputStream script, String chunkname, OutputStream out ) throws IOException {
private void processScript( InputStream script, String chunkname, String filename) throws IOException {
try {
// create the chunk
Hashtable t = LuaJC.getInstance().compileAll(script, chunkname, chunkname);
Hashtable t = LuaJC.getInstance().compileAll(script, chunkname, filename);
// write out the chunk
if (!parseonly) {
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
byte[] bytes = (byte[]) t.get(key);
String filename = key + ".class";
if ( versioninfo )
System.out.println(filename+": "+bytes.length+" bytes");
FileOutputStream fos = new FileOutputStream( filename );
fos.write( bytes );
fos.close();
}
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
byte[] bytes = (byte[]) t.get(key);
String destpath = (destdir!=null? destdir+"/": "") + key + ".class";
if ( verbose )
System.out.println(
"chunk "+chunkname+
" from "+filename+
" written to "+destpath
+" length="+bytes.length+" bytes");
FileOutputStream fos = new FileOutputStream( destpath );
fos.write( bytes );
fos.close();
}
} catch ( Throwable t ) {