Expose class name normalization.
This commit is contained in:
@@ -274,7 +274,7 @@ public class LoadState {
|
||||
luacSizeofLuaNumber = is.readByte();
|
||||
luacNumberFormat = is.readByte();
|
||||
}
|
||||
|
||||
|
||||
public static LuaFunction load( InputStream stream, String name, LuaValue env ) throws IOException {
|
||||
Prototype p = compile( stream, name );
|
||||
if ( compiler != null )
|
||||
|
||||
@@ -354,8 +354,9 @@ public class PackageLib extends LuaTable {
|
||||
Class c = null;
|
||||
LuaValue v = null;
|
||||
try {
|
||||
c = Class.forName(name);
|
||||
c = Class.forName(name.replace('/', '.'));
|
||||
v = (LuaValue) c.newInstance();
|
||||
v.setfenv(_G);
|
||||
return v;
|
||||
} catch ( ClassNotFoundException cnfe ) {
|
||||
return valueOf("\n\tno class '"+name+"'" );
|
||||
@@ -363,4 +364,41 @@ public class PackageLib extends LuaTable {
|
||||
return valueOf("\n\tjava load failed on '"+name+"', "+e );
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert lua filename to valid class name */
|
||||
public static final String toClassname( String filename ) {
|
||||
int n=filename.length();
|
||||
int j=n;
|
||||
if ( filename.endsWith(".lua") )
|
||||
j -= 4;
|
||||
for ( int k=0; k<j; k++ ) {
|
||||
char c = filename.charAt(k);
|
||||
if ( (!isClassnamePart(c)) && (c!='/') && (c!='\\') ) {
|
||||
StringBuffer sb = new StringBuffer(j);
|
||||
for ( int i=0; i<j; i++ ) {
|
||||
c = filename.charAt(k);
|
||||
sb.append(
|
||||
(isClassnamePart(c))? c:
|
||||
((c=='/') || (c=='\\'))? '.': '_' );
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
return n==j? filename: filename.substring(0,j);
|
||||
}
|
||||
|
||||
private static final boolean isClassnamePart(char c) {
|
||||
if ( (c>='a'&&c<='z') || (c>='A'&&c<='Z') || (c>='0'&&c<='9') )
|
||||
return true;
|
||||
switch ( c ) {
|
||||
case '.':
|
||||
case '$':
|
||||
case '_':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user