Expose class name normalization.

This commit is contained in:
James Roseborough
2009-11-03 18:06:03 +00:00
parent 0aa2563cc6
commit 3906ab0711
4 changed files with 59 additions and 24 deletions

View File

@@ -32,6 +32,7 @@ import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Prototype;
import org.luaj.vm2.LoadState.LuaCompiler;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.PackageLib;
public class JavaBytecodeCompiler implements LuaCompiler {
@@ -89,7 +90,7 @@ public class JavaBytecodeCompiler implements LuaCompiler {
/** 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));
return gen.generateBytecode(p, PackageLib.toClassname(filename), toSourcename(filename));
}
/** Compile all classes produced by a prototype, and put results in a hashtable */
@@ -98,7 +99,7 @@ public class JavaBytecodeCompiler implements LuaCompiler {
install();
Prototype p = LoadState.compile( stream, filename );
Hashtable t = new Hashtable();
getInstance().genClass(t, p, toClassname(filename), toSourcename(filename));
getInstance().genClass(t, p, PackageLib.toClassname(filename), toSourcename(filename));
return t;
}
@@ -110,7 +111,7 @@ public class JavaBytecodeCompiler implements LuaCompiler {
public LuaFunction load(Prototype p, String filename, LuaValue env) {
try {
Class c = gen.toJavaBytecode(p, toClassname(filename), toSourcename(filename));
Class c = gen.toJavaBytecode(p, PackageLib.toClassname(filename), toSourcename(filename));
Object o = c.newInstance();
LuaFunction f = (LuaFunction) o;
f.setfenv(env);
@@ -121,16 +122,6 @@ public class JavaBytecodeCompiler implements LuaCompiler {
}
}
/** 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('\\', '.');
classname = classname.replaceAll("[^\\w\\.]", "_");
return classname;
}
private static final String toSourcename( String filename ) {
return filename.substring( filename.lastIndexOf('/')+1 );
}

View File

@@ -65,7 +65,9 @@ import org.luaj.vm2.lib.VarArgFunction;
public class JavaBytecodeGenerator {
public static boolean DUMPCLASSES = "true".equals(System.getProperty("DUMPCLASSES"));
public static boolean gendebuginfo = true;
private static final String STR_FUNCV = VarArgFunction.class.getName();
private static final String STR_VARARGS = Varargs.class.getName();
private static final String STR_LUAVALUE = LuaValue.class.getName();
@@ -928,10 +930,12 @@ public class JavaBytecodeGenerator {
}
// add line numbers
if ( p.lineinfo != null && p.lineinfo.length >= nc) {
for ( pc=0; pc<nc; pc++ ) {
if ( ih[pc] != null )
mg.addLineNumber( ih[pc], p.lineinfo[pc] );
if ( gendebuginfo ) {
if ( p.lineinfo != null && p.lineinfo.length >= nc) {
for ( pc=0; pc<nc; pc++ ) {
if ( ih[pc] != null )
mg.addLineNumber( ih[pc], p.lineinfo[pc] );
}
}
}
@@ -1199,10 +1203,12 @@ public class JavaBytecodeGenerator {
private String getlocalname(LocVars[] locvars, int j) {
int number = j+1;
for (int i = 0; i < locvars.length; i++) {
if (pc < locvars[i].endpc) { /* is variable active? */
if (--number == 0)
return locvars[i].varname.toString();
if ( gendebuginfo ) {
for (int i = 0; i < locvars.length; i++) {
if (pc < locvars[i].endpc) { /* is variable active? */
if (--number == 0)
return locvars[i].varname.toString();
}
}
}
return "$"+j;