Let ant build download bcel jar as needed.

This commit is contained in:
James Roseborough
2009-10-30 21:57:35 +00:00
parent 3e3909b14b
commit 17c86b8eb3
8 changed files with 72 additions and 52 deletions

View File

@@ -28,7 +28,7 @@ import java.io.OutputStream;
import org.luaj.vm2.Lua;
import org.luaj.vm2.compiler.DumpState;
import org.luaj.vm2.luajc.antlr.AntlrLuaJCompiler;
import org.luaj.vm2.luajc.JavaBytecodeCompiler;
/**
@@ -41,7 +41,6 @@ public class luajc {
"usage: java -cp luaj-jse.jar,antlr-3.1.3.jar luajc [options] [filenames].\n" +
"Available options are:\n" +
" - process stdin\n" +
" -l list\n" +
" -o name output to file 'name' (default is \"luac.out\")\n" +
" -p parse only\n" +
" -s strip debug information\n" +
@@ -55,7 +54,6 @@ public class luajc {
System.exit(-1);
}
private boolean list = false;
private String output = "luacj.out";
private boolean parseonly = false;
private boolean stripdebug = false;
@@ -80,9 +78,6 @@ public class luajc {
// input file - defer to next stage
} else {
switch ( args[i].charAt(1) ) {
case 'l':
list = true;
break;
case 'o':
if ( ++i >= args.length )
usageExit();
@@ -157,16 +152,12 @@ public class luajc {
private void processScript( InputStream script, String chunkname, OutputStream out ) throws IOException {
try {
// create the chunk
String source = AntlrLuaJCompiler.compile(script, chunkname);
// list the chunk
if (list)
System.out.println(source);
byte[] bytes = JavaBytecodeCompiler.loadClass(script, chunkname);
// write out the chunk
if (!parseonly) {
FileOutputStream fos = new FileOutputStream( chunkname+".java" );
fos.write( source.getBytes() );
fos.write( bytes );
fos.close();
}

View File

@@ -74,15 +74,17 @@ public class JavaBytecodeCompiler implements LuaCompiler {
return luac.compile(firstByte, stream, chunkname);
}
/** Compile and load a chunk
* @throws IOException */
public static byte[] loadClass(InputStream is, String filename) throws IOException {
return getInstance().loadClass( is.read(), is, filename );
}
/** Compile into class form. */
public LuaFunction load(int firstByte, InputStream stream, String filename, LuaValue env) throws IOException {
Prototype p = compile( firstByte, stream, filename);
try {
String classname = filename.endsWith(".lua")? filename.substring(0,filename.length()-4): filename;
classname = classname.replace('/', '.');
classname = classname.replace('\\', '.');
String sourcename = filename.substring( filename.lastIndexOf('/')+1 );
Class c = gen.toJavaBytecode(p, classname, sourcename);
Class c = gen.toJavaBytecode(p, toClassname(filename), toSourcename(filename));
Object o = c.newInstance();
LuaFunction f = (LuaFunction) o;
f.setfenv(env);
@@ -91,7 +93,24 @@ public class JavaBytecodeCompiler implements LuaCompiler {
t.printStackTrace();
return new LuaClosure( p, env );
}
}
/** Compile into a class */
private byte[] loadClass(int firstByte, InputStream stream, String filename) throws IOException {
Prototype p = compile(firstByte, stream, filename);
return gen.generateBytecode(p, toClassname(filename), toSourcename(filename));
}
/** Convert filename to class name */
private static final String toClassname( String filename ) {
String classname = filename.endsWith(".lua")? filename.substring(0,filename.length()-4): filename;
classname = classname.replace('/', '.');
classname = classname.replace('\\', '.');
return classname;
}
private static final String toSourcename( String filename ) {
return filename.substring( filename.lastIndexOf('/')+1 );
}
}

View File

@@ -23,6 +23,7 @@ package org.luaj.vm2.luajc;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.Field;
@@ -60,8 +61,6 @@ import org.luaj.vm2.Prototype;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.VarArgFunction;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
public class JavaBytecodeGenerator {
public static boolean DUMPCLASSES = "true".equals(System.getProperty("DUMPCLASSES"));
@@ -164,7 +163,7 @@ public class JavaBytecodeGenerator {
return (i >> 14) & 0x1ff;
}
private byte[] generateBytecode(Prototype p, String classname, String filename)
byte[] generateBytecode(Prototype p, String classname, String filename)
throws IOException {
// compile our class next
@@ -211,6 +210,7 @@ public class JavaBytecodeGenerator {
k[i].getName(), k[i].getType()));
break;
case LuaValue.TSTRING:
// TODO: quote non-utf8 byte sequences
il.append(new PUSH(cp, ki.toString()));
il.append(factory.createInvoke(STR_LUASTRING, "valueOf",
TYPE_LUASTRING, new Type[] { Type.STRING },

View File

@@ -35,7 +35,6 @@ import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.luajc.antlr.AntlrLuaJCompiler;
public class LuaJCompiler {
@@ -47,7 +46,8 @@ public class LuaJCompiler {
}
public static String compileToJava( InputStream luaSource, String chunkName ) throws Exception {
return AntlrLuaJCompiler.compile( luaSource, chunkName );
// return AntlrLuaJCompiler.compile( luaSource, chunkName );
throw new RuntimeException( "not supported" );
}