Fixes to class generation logic.
This commit is contained in:
@@ -25,12 +25,15 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
import org.luaj.vm2.Lua;
|
import org.luaj.vm2.Lua;
|
||||||
import org.luaj.vm2.compiler.DumpState;
|
import org.luaj.vm2.compiler.DumpState;
|
||||||
import org.luaj.vm2.luajc.JavaBytecodeCompiler;
|
import org.luaj.vm2.luajc.JavaBytecodeCompiler;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compiler for lua files to compile lua sources into java sources.
|
* 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 {
|
private void processScript( InputStream script, String chunkname, OutputStream out ) throws IOException {
|
||||||
try {
|
try {
|
||||||
// create the chunk
|
// create the chunk
|
||||||
byte[] bytes = JavaBytecodeCompiler.loadClass(script, chunkname);
|
Hashtable t = JavaBytecodeCompiler.loadClasses(script, chunkname);
|
||||||
|
|
||||||
// write out the chunk
|
// write out the chunk
|
||||||
if (!parseonly) {
|
if (!parseonly) {
|
||||||
FileOutputStream fos = new FileOutputStream( chunkname+".java" );
|
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
|
||||||
fos.write( bytes );
|
String key = (String) e.nextElement();
|
||||||
fos.close();
|
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 ) {
|
} catch ( Throwable t ) {
|
||||||
|
|||||||
@@ -94,17 +94,18 @@ public class JavaBytecodeCompiler implements LuaCompiler {
|
|||||||
|
|
||||||
/** Compile all classes produced by a prototype, and put results in a hashtable */
|
/** Compile all classes produced by a prototype, and put results in a hashtable */
|
||||||
public static Hashtable loadClasses( InputStream stream, String filename ) throws IOException {
|
public static Hashtable loadClasses( InputStream stream, String filename ) throws IOException {
|
||||||
|
if ( LoadState.compiler == null )
|
||||||
|
install();
|
||||||
Prototype p = LoadState.compile( stream, filename );
|
Prototype p = LoadState.compile( stream, filename );
|
||||||
Hashtable t = new Hashtable();
|
Hashtable t = new Hashtable();
|
||||||
getInstance().genClass(t, p, toClassname(filename), toSourcename(filename));
|
getInstance().genClass(t, p, toClassname(filename), toSourcename(filename));
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void genClass( Hashtable t, Prototype p, String className, String sourceName ) throws IOException {
|
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++ ) {
|
t.put( classname, gen.generateBytecode( p, classname, sourceName ) );
|
||||||
String name = className + "$" + i;
|
for ( int i=0, n=p.p!=null? p.p.length: 0; i<n; i++ )
|
||||||
t.put( name, gen.generateBytecode( p.p[i], name, sourceName ) );
|
genClass( t, p.p[i], classname + "$" + i, sourceName );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaFunction load(Prototype p, String filename, LuaValue env) {
|
public LuaFunction load(Prototype p, String filename, LuaValue env) {
|
||||||
|
|||||||
@@ -813,6 +813,7 @@ public class JavaBytecodeGenerator {
|
|||||||
c = code[pc++];
|
c = code[pc++];
|
||||||
int offset = (c-1) * Lua.LFIELDS_PER_FLUSH;
|
int offset = (c-1) * Lua.LFIELDS_PER_FLUSH;
|
||||||
//o = stack[a];
|
//o = stack[a];
|
||||||
|
ih[pc] =
|
||||||
loadLocal(a);
|
loadLocal(a);
|
||||||
if ( (b=B(i)) == 0 ) {
|
if ( (b=B(i)) == 0 ) {
|
||||||
int j=1;
|
int j=1;
|
||||||
@@ -914,9 +915,13 @@ public class JavaBytecodeGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// self-check
|
||||||
|
|
||||||
// resolve branches
|
// resolve branches
|
||||||
for (pc = 0; pc < nc; pc++) {
|
for (pc = 0; pc < nc; pc++) {
|
||||||
if (branches[pc] != null) {
|
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]]);
|
branches[pc].setTarget(ih[targets[pc]]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user