diff --git a/.classpath b/.classpath index 7b5ef63e..d7059f2f 100644 --- a/.classpath +++ b/.classpath @@ -1,7 +1,7 @@ - + diff --git a/.cvsignore b/.cvsignore index cd047079..0986517c 100644 --- a/.cvsignore +++ b/.cvsignore @@ -1,6 +1,7 @@ bin target build +lib luaj*.jar jit *.ser diff --git a/build.xml b/build.xml index 29caece5..5a82b2fe 100644 --- a/build.xml +++ b/build.xml @@ -7,13 +7,15 @@ - + + @@ -21,45 +23,49 @@ - - - - - - Generating files using ${antlr.tool.jar} - - - + + + + + + + + + + + + + - + - + + + - - - - - - - - - - - + + + + @@ -107,6 +113,9 @@ + + + diff --git a/src/jse/luajc.java b/src/jse/luajc.java index b3bd27a1..24ab4779 100644 --- a/src/jse/luajc.java +++ b/src/jse/luajc.java @@ -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(); } diff --git a/src/jse/org/luaj/vm2/luajc/JavaBytecodeCompiler.java b/src/jse/org/luaj/vm2/luajc/JavaBytecodeCompiler.java index 5bb50f3a..d9310593 100644 --- a/src/jse/org/luaj/vm2/luajc/JavaBytecodeCompiler.java +++ b/src/jse/org/luaj/vm2/luajc/JavaBytecodeCompiler.java @@ -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 ); } } diff --git a/src/jse/org/luaj/vm2/luajc/JavaBytecodeGenerator.java b/src/jse/org/luaj/vm2/luajc/JavaBytecodeGenerator.java index c3c97b19..b47e9ed8 100644 --- a/src/jse/org/luaj/vm2/luajc/JavaBytecodeGenerator.java +++ b/src/jse/org/luaj/vm2/luajc/JavaBytecodeGenerator.java @@ -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 }, diff --git a/src/jse/org/luaj/vm2/luajc/LuaJCompiler.java b/src/jse/org/luaj/vm2/luajc/LuaJCompiler.java index 3bef5032..4005f864 100644 --- a/src/jse/org/luaj/vm2/luajc/LuaJCompiler.java +++ b/src/jse/org/luaj/vm2/luajc/LuaJCompiler.java @@ -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" ); } diff --git a/version.properties b/version.properties index ec7f350e..a06c69ed 100644 --- a/version.properties +++ b/version.properties @@ -1,2 +1,2 @@ # on the way to version 2.0 -version: 1.9.50 +version: 1.9.51