Let compilers compile from prototypes, fix loading of code without debug info.

This commit is contained in:
James Roseborough
2009-10-31 00:37:22 +00:00
parent 4a2b2d7191
commit 97e6da241b
4 changed files with 30 additions and 11 deletions

View File

@@ -55,10 +55,16 @@ public class LoadState {
/** Interface for the compiler, if it is installed. */
public interface LuaCompiler {
/** Compile into a prototype, without taking the additional step of create a LuaFunction or LuaClosure */
public Prototype compile(int firstByte, InputStream stream, String name) throws IOException;
/** Load into a Closure or LuaFunction, with the supplied initial environment */
public LuaFunction load(int firstByte, InputStream stream, String name, LuaValue env) throws IOException;
/** Load into a LuaFunction given a prototype. May compile into a class, or return a LuaClosure
* @param filename TODO*/
public LuaFunction load(Prototype p, String filename, LuaValue env);
}
/** Compiler instance, if installed */
@@ -298,7 +304,10 @@ public class LoadState {
}
Prototype p = s.loadFunction( LuaString.valueOf(sname) );
return new LuaClosure( p, env );
if ( compiler != null )
return compiler.load(p, name, env);
else
return new LuaClosure( p, env );
}
public static String getSourceName(String name) {

View File

@@ -233,4 +233,8 @@ public class LuaC extends Lua implements LuaCompiler {
return string;
}
public LuaFunction load(Prototype p, String filename, LuaValue env) {
return new LuaClosure( p, env );
}
}

View File

@@ -83,6 +83,17 @@ public class JavaBytecodeCompiler implements LuaCompiler {
/** Compile into class form. */
public LuaFunction load(int firstByte, InputStream stream, String filename, LuaValue env) throws IOException {
Prototype p = compile( firstByte, stream, filename);
return load( p, filename, 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));
}
public LuaFunction load(Prototype p, String filename, LuaValue env) {
try {
Class c = gen.toJavaBytecode(p, toClassname(filename), toSourcename(filename));
Object o = c.newInstance();
@@ -94,12 +105,7 @@ public class JavaBytecodeCompiler implements LuaCompiler {
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 ) {

View File

@@ -251,7 +251,7 @@ public class JavaBytecodeGenerator {
}
// upvalues are fields
int nu = p.upvalues.length;
int nu = p.nups;
Field[] u = new Field[nu];
for (int i = 0; i < nu; i++) {
String name = getUpvalueName(p.upvalues, i);
@@ -403,7 +403,7 @@ public class JavaBytecodeGenerator {
case Lua.OP_GETUPVAL: /* A B R(A):= UpValue[B] */
ih[pc] =
il.append(InstructionConstants.THIS);
il.append(factory.createFieldAccess(classname, u[B(i)].getName(), TYPE_LOCALUPVALUE, Constants.GETFIELD));
il.append(factory.createFieldAccess(classname, getUpvalueName(p.upvalues, B(i)), TYPE_LOCALUPVALUE, Constants.GETFIELD));
il.append(new PUSH(cp,0));
il.append(InstructionConstants.AALOAD);
il_append_new_ASTORE(cp,il, (locals[a]));
@@ -444,7 +444,7 @@ public class JavaBytecodeGenerator {
// upValues[B(i)].setValue(stack[a]);
ih[pc] =
il.append(InstructionConstants.THIS);
il.append(factory.createFieldAccess(classname, u[B(i)].getName(), TYPE_LOCALUPVALUE, Constants.GETFIELD));
il.append(factory.createFieldAccess(classname, getUpvalueName( p.upvalues, B(i)), TYPE_LOCALUPVALUE, Constants.GETFIELD));
il.append(new PUSH(cp,0));
il_append_new_ALOAD(cp,il, (locals[a]));
il.append(InstructionConstants.AASTORE);
@@ -948,7 +948,7 @@ public class JavaBytecodeGenerator {
// findUpValue(stack,b);
if ( (i&4) != 0 ) {
il.append(InstructionConstants.THIS);
String srcname = u[b].getName();
String srcname = getUpvalueName( p.upvalues, b );
il.append(factory.createFieldAccess(classname, srcname, TYPE_LOCALUPVALUE, Constants.GETFIELD));
} else {
il.append( new ALOAD(locals[b].getIndex()) );