Improve bytecode generation.

This commit is contained in:
James Roseborough
2010-08-13 00:23:53 +00:00
parent a950957318
commit a62350b06a
3 changed files with 33 additions and 13 deletions

View File

@@ -215,16 +215,14 @@ public class luajc {
if ( loadclasses ) { if ( loadclasses ) {
ClassLoader loader = new ClassLoader() { ClassLoader loader = new ClassLoader() {
public Class findClass(String classname) throws ClassNotFoundException { public Class findClass(String classname) throws ClassNotFoundException {
String key = classname.replace('.', '/'); byte[] bytes = (byte[]) t.get(classname);
byte[] bytes = (byte[]) t.get(key);
if ( bytes != null ) if ( bytes != null )
return defineClass(classname, bytes, 0, bytes.length); return defineClass(classname, bytes, 0, bytes.length);
return super.findClass(classname); return super.findClass(classname);
} }
}; };
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) { for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement(); String classname = (String) e.nextElement();
String classname = key.replace('/', '.');
try { try {
Class c = loader.loadClass(classname); Class c = loader.loadClass(classname);
Object o = c.newInstance(); Object o = c.newInstance();

View File

@@ -31,9 +31,9 @@ import org.luaj.vm2.Prototype;
*/ */
public class JavaGen { public class JavaGen {
public String classname; public final String classname;
public byte[] bytecode; public final byte[] bytecode;
public JavaGen[] inners; public final JavaGen[] inners;
public JavaGen( Prototype p, String classname, String filename ) { public JavaGen( Prototype p, String classname, String filename ) {
this( new ProtoInfo(p,classname), classname, filename ); this( new ProtoInfo(p,classname), classname, filename );

View File

@@ -34,7 +34,7 @@ import org.luaj.vm2.compiler.LuaC;
public class LuaJC implements LuaCompiler { public class LuaJC implements LuaCompiler {
private static final String NON_IDENTIFIER = "[^a-zA-Z0-9_$]"; private static final String NON_IDENTIFIER = "[^a-zA-Z0-9_$/.\\-]";
private static LuaJC instance; private static LuaJC instance;
@@ -55,10 +55,12 @@ public class LuaJC implements LuaCompiler {
public LuaJC() { public LuaJC() {
} }
public Hashtable compileAll(InputStream script, String classname, String filename) throws IOException { public Hashtable compileAll(InputStream script, String chunkname, String filename) throws IOException {
String classname = toStandardJavaClassName( chunkname );
String luaname = toStandardLuaFileName( filename );
Hashtable h = new Hashtable(); Hashtable h = new Hashtable();
Prototype p = LuaC.instance.compile(script, classname); Prototype p = LuaC.instance.compile(script, classname);
JavaGen gen = new JavaGen(p, classname, filename); JavaGen gen = new JavaGen(p, classname, luaname);
insert( h, gen ); insert( h, gen );
return h; return h;
} }
@@ -71,9 +73,29 @@ public class LuaJC implements LuaCompiler {
public LuaFunction load(InputStream stream, String name, LuaValue env) throws IOException { public LuaFunction load(InputStream stream, String name, LuaValue env) throws IOException {
Prototype p = LuaC.instance.compile(stream, name); Prototype p = LuaC.instance.compile(stream, name);
String classname = name.endsWith(".lua")? name.substring(0,name.length()-4): name; String classname = toStandardJavaClassName( name );
classname = classname.replaceAll(NON_IDENTIFIER, "_"); String luaname = toStandardLuaFileName( name );
JavaLoader loader = new JavaLoader(env); JavaLoader loader = new JavaLoader(env);
return loader.load(p, classname, name); return loader.load(p, classname, luaname);
}
private static String toStandardJavaClassName( String luachunkname ) {
String stub = toStub( luachunkname );
String classname = stub.replace('/','.').replaceAll(NON_IDENTIFIER, "_");
int c = classname.charAt(0);
if ( c!='_' && !Character.isJavaIdentifierStart(c) )
classname = "_"+classname;
return classname;
}
private static String toStandardLuaFileName( String luachunkname ) {
String stub = toStub( luachunkname );
String filename = stub.replace('.','/')+".lua";
return filename;
}
private static String toStub( String s ) {
String stub = s.endsWith(".lua")? s.substring(0,s.length()-4): s;
return stub;
} }
} }