2009-10-27 06:12:24 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
|
* Copyright (c) 2009 Luaj.org. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
2009-11-03 02:13:45 +00:00
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.Hashtable;
|
2010-04-25 05:35:24 +00:00
|
|
|
import java.util.Vector;
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
import org.luaj.vm2.Lua;
|
|
|
|
|
import org.luaj.vm2.compiler.DumpState;
|
2010-04-13 14:31:40 +00:00
|
|
|
import org.luaj.vm2.luajc.LuaJC;
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
/**
|
2010-04-25 05:35:24 +00:00
|
|
|
* Compiler for lua files to compile lua sources or lua binaries into java classes.
|
2009-10-27 06:12:24 +00:00
|
|
|
*/
|
|
|
|
|
public class luajc {
|
|
|
|
|
private static final String version = Lua._VERSION + "Copyright (C) 2009 luaj.org";
|
|
|
|
|
|
|
|
|
|
private static final String usage =
|
2010-03-31 04:15:09 +00:00
|
|
|
"usage: java -cp luaj-jse.jar,bcel-5.2.jar luajc [options] [filenames].\n" +
|
2009-10-27 06:12:24 +00:00
|
|
|
"Available options are:\n" +
|
|
|
|
|
" - process stdin\n" +
|
2010-04-25 05:35:24 +00:00
|
|
|
" -s source directory\n" +
|
|
|
|
|
" -d destination directory\n" +
|
|
|
|
|
" -n no debug information (strip debug)\n" +
|
2009-10-27 06:12:24 +00:00
|
|
|
" -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" +
|
|
|
|
|
" -- stop handling options\n";
|
|
|
|
|
|
|
|
|
|
private static void usageExit() {
|
|
|
|
|
System.out.println(usage);
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-25 05:35:24 +00:00
|
|
|
private String srcdir = null;
|
|
|
|
|
private String destdir = null;
|
2009-10-27 06:12:24 +00:00
|
|
|
private boolean stripdebug = false;
|
|
|
|
|
private boolean littleendian = false;
|
|
|
|
|
private int numberformat = DumpState.NUMBER_FORMAT_DEFAULT;
|
2010-04-25 05:35:24 +00:00
|
|
|
private boolean verbose = false;
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
public static void main( String[] args ) throws IOException {
|
|
|
|
|
new luajc( args );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private luajc( String[] args ) throws IOException {
|
|
|
|
|
|
|
|
|
|
// process args
|
|
|
|
|
try {
|
2010-04-25 05:35:24 +00:00
|
|
|
Vector files = new Vector();
|
|
|
|
|
|
2009-10-27 06:12:24 +00:00
|
|
|
// get stateful args
|
|
|
|
|
for ( int i=0; i<args.length; i++ ) {
|
2010-04-25 05:35:24 +00:00
|
|
|
if ( ! args[i].startsWith("-") ) {
|
|
|
|
|
files.add(args[i]);
|
2009-10-27 06:12:24 +00:00
|
|
|
} else {
|
|
|
|
|
switch ( args[i].charAt(1) ) {
|
2010-04-25 05:35:24 +00:00
|
|
|
case 's':
|
2009-10-27 06:12:24 +00:00
|
|
|
if ( ++i >= args.length )
|
|
|
|
|
usageExit();
|
2010-04-25 05:35:24 +00:00
|
|
|
srcdir = args[i];
|
2009-10-27 06:12:24 +00:00
|
|
|
break;
|
2010-04-25 05:35:24 +00:00
|
|
|
case 'd':
|
|
|
|
|
if ( ++i >= args.length )
|
|
|
|
|
usageExit();
|
|
|
|
|
destdir = args[i];
|
2009-10-27 06:12:24 +00:00
|
|
|
break;
|
2010-04-25 05:35:24 +00:00
|
|
|
case 'n':
|
2009-10-27 06:12:24 +00:00
|
|
|
stripdebug = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'e':
|
|
|
|
|
littleendian = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'i':
|
|
|
|
|
if ( args[i].length() <= 2 )
|
|
|
|
|
usageExit();
|
|
|
|
|
numberformat = Integer.parseInt(args[i].substring(2));
|
|
|
|
|
break;
|
|
|
|
|
case 'v':
|
2010-04-25 05:35:24 +00:00
|
|
|
verbose = true;
|
2009-10-27 06:12:24 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
usageExit();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// echo version
|
2010-04-25 05:35:24 +00:00
|
|
|
if ( verbose ) {
|
2009-10-27 06:12:24 +00:00
|
|
|
System.out.println(version);
|
2010-04-25 05:35:24 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
// process input files
|
2010-04-25 05:35:24 +00:00
|
|
|
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 );
|
2009-10-27 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch ( IOException ioe ) {
|
|
|
|
|
System.err.println( ioe.toString() );
|
|
|
|
|
System.exit(-2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-25 05:35:24 +00:00
|
|
|
private void processScript( InputStream script, String chunkname, String filename) throws IOException {
|
2009-10-27 06:12:24 +00:00
|
|
|
try {
|
|
|
|
|
// create the chunk
|
2010-04-25 05:35:24 +00:00
|
|
|
Hashtable t = LuaJC.getInstance().compileAll(script, chunkname, filename);
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
// write out the chunk
|
2010-04-25 05:35:24 +00:00
|
|
|
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();
|
2009-10-27 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch ( Throwable t ) {
|
|
|
|
|
t.printStackTrace( System.err );
|
|
|
|
|
} finally {
|
|
|
|
|
script.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|