Fixes to class generation logic.

This commit is contained in:
James Roseborough
2009-11-03 02:13:45 +00:00
parent ac91de7794
commit 3d4a29f1bc
3 changed files with 26 additions and 10 deletions

View File

@@ -25,12 +25,15 @@ 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 org.luaj.vm2.Lua;
import org.luaj.vm2.compiler.DumpState;
import org.luaj.vm2.luajc.JavaBytecodeCompiler;
/**
* Compiler for lua files to compile lua sources into java sources.
*/
@@ -152,13 +155,20 @@ public class luajc {
private void processScript( InputStream script, String chunkname, OutputStream out ) throws IOException {
try {
// create the chunk
byte[] bytes = JavaBytecodeCompiler.loadClass(script, chunkname);
Hashtable t = JavaBytecodeCompiler.loadClasses(script, chunkname);
// write out the chunk
if (!parseonly) {
FileOutputStream fos = new FileOutputStream( chunkname+".java" );
fos.write( bytes );
fos.close();
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();
}
}
} catch ( Throwable t ) {

View File

@@ -94,17 +94,18 @@ public class JavaBytecodeCompiler implements LuaCompiler {
/** Compile all classes produced by a prototype, and put results in a hashtable */
public static Hashtable loadClasses( InputStream stream, String filename ) throws IOException {
if ( LoadState.compiler == null )
install();
Prototype p = LoadState.compile( stream, filename );
Hashtable t = new Hashtable();
getInstance().genClass(t, p, toClassname(filename), toSourcename(filename));
return t;
}
private void genClass( Hashtable t, Prototype p, String className, String sourceName ) throws IOException {
for ( int i=0, n=p.p!=null? p.p.length: 0; i<n; i++ ) {
String name = className + "$" + i;
t.put( name, gen.generateBytecode( p.p[i], name, sourceName ) );
}
private void genClass( Hashtable t, Prototype p, String classname, String sourceName ) throws IOException {
t.put( classname, gen.generateBytecode( p, classname, sourceName ) );
for ( int i=0, n=p.p!=null? p.p.length: 0; i<n; i++ )
genClass( t, p.p[i], classname + "$" + i, sourceName );
}
public LuaFunction load(Prototype p, String filename, LuaValue env) {

View File

@@ -296,7 +296,7 @@ public class JavaBytecodeGenerator {
// pull out instruction
i = code[pc];
a = A(i);
// process the op code
switch ( OP(i) ) {
@@ -813,6 +813,7 @@ public class JavaBytecodeGenerator {
c = code[pc++];
int offset = (c-1) * Lua.LFIELDS_PER_FLUSH;
//o = stack[a];
ih[pc] =
loadLocal(a);
if ( (b=B(i)) == 0 ) {
int j=1;
@@ -914,9 +915,13 @@ public class JavaBytecodeGenerator {
}
}
// self-check
// resolve branches
for (pc = 0; pc < nc; pc++) {
if (branches[pc] != null) {
if ( ih[targets[pc]] == null )
throw new IllegalArgumentException("no target at "+targets[pc]+" op="+OP(code[targets[pc]]));
branches[pc].setTarget(ih[targets[pc]]);
}
}