Expose class name normalization.
This commit is contained in:
@@ -354,8 +354,9 @@ public class PackageLib extends LuaTable {
|
|||||||
Class c = null;
|
Class c = null;
|
||||||
LuaValue v = null;
|
LuaValue v = null;
|
||||||
try {
|
try {
|
||||||
c = Class.forName(name);
|
c = Class.forName(name.replace('/', '.'));
|
||||||
v = (LuaValue) c.newInstance();
|
v = (LuaValue) c.newInstance();
|
||||||
|
v.setfenv(_G);
|
||||||
return v;
|
return v;
|
||||||
} catch ( ClassNotFoundException cnfe ) {
|
} catch ( ClassNotFoundException cnfe ) {
|
||||||
return valueOf("\n\tno class '"+name+"'" );
|
return valueOf("\n\tno class '"+name+"'" );
|
||||||
@@ -363,4 +364,41 @@ public class PackageLib extends LuaTable {
|
|||||||
return valueOf("\n\tjava load failed on '"+name+"', "+e );
|
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.Prototype;
|
||||||
import org.luaj.vm2.LoadState.LuaCompiler;
|
import org.luaj.vm2.LoadState.LuaCompiler;
|
||||||
import org.luaj.vm2.compiler.LuaC;
|
import org.luaj.vm2.compiler.LuaC;
|
||||||
|
import org.luaj.vm2.lib.PackageLib;
|
||||||
|
|
||||||
public class JavaBytecodeCompiler implements LuaCompiler {
|
public class JavaBytecodeCompiler implements LuaCompiler {
|
||||||
|
|
||||||
@@ -89,7 +90,7 @@ public class JavaBytecodeCompiler implements LuaCompiler {
|
|||||||
/** Compile into a class */
|
/** Compile into a class */
|
||||||
private byte[] loadClass(int firstByte, InputStream stream, String filename) throws IOException {
|
private byte[] loadClass(int firstByte, InputStream stream, String filename) throws IOException {
|
||||||
Prototype p = compile(firstByte, stream, filename);
|
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 */
|
/** Compile all classes produced by a prototype, and put results in a hashtable */
|
||||||
@@ -98,7 +99,7 @@ public class JavaBytecodeCompiler implements LuaCompiler {
|
|||||||
install();
|
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, PackageLib.toClassname(filename), toSourcename(filename));
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +111,7 @@ public class JavaBytecodeCompiler implements LuaCompiler {
|
|||||||
|
|
||||||
public LuaFunction load(Prototype p, String filename, LuaValue env) {
|
public LuaFunction load(Prototype p, String filename, LuaValue env) {
|
||||||
try {
|
try {
|
||||||
Class c = gen.toJavaBytecode(p, toClassname(filename), toSourcename(filename));
|
Class c = gen.toJavaBytecode(p, PackageLib.toClassname(filename), toSourcename(filename));
|
||||||
Object o = c.newInstance();
|
Object o = c.newInstance();
|
||||||
LuaFunction f = (LuaFunction) o;
|
LuaFunction f = (LuaFunction) o;
|
||||||
f.setfenv(env);
|
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 ) {
|
private static final String toSourcename( String filename ) {
|
||||||
return filename.substring( filename.lastIndexOf('/')+1 );
|
return filename.substring( filename.lastIndexOf('/')+1 );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,8 @@ import org.luaj.vm2.lib.VarArgFunction;
|
|||||||
public class JavaBytecodeGenerator {
|
public class JavaBytecodeGenerator {
|
||||||
public static boolean DUMPCLASSES = "true".equals(System.getProperty("DUMPCLASSES"));
|
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_FUNCV = VarArgFunction.class.getName();
|
||||||
private static final String STR_VARARGS = Varargs.class.getName();
|
private static final String STR_VARARGS = Varargs.class.getName();
|
||||||
private static final String STR_LUAVALUE = LuaValue.class.getName();
|
private static final String STR_LUAVALUE = LuaValue.class.getName();
|
||||||
@@ -928,12 +930,14 @@ public class JavaBytecodeGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add line numbers
|
// add line numbers
|
||||||
|
if ( gendebuginfo ) {
|
||||||
if ( p.lineinfo != null && p.lineinfo.length >= nc) {
|
if ( p.lineinfo != null && p.lineinfo.length >= nc) {
|
||||||
for ( pc=0; pc<nc; pc++ ) {
|
for ( pc=0; pc<nc; pc++ ) {
|
||||||
if ( ih[pc] != null )
|
if ( ih[pc] != null )
|
||||||
mg.addLineNumber( ih[pc], p.lineinfo[pc] );
|
mg.addLineNumber( ih[pc], p.lineinfo[pc] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add initialization at front
|
// add initialization at front
|
||||||
il.insert( il.getStart(), init );
|
il.insert( il.getStart(), init );
|
||||||
@@ -1199,12 +1203,14 @@ public class JavaBytecodeGenerator {
|
|||||||
|
|
||||||
private String getlocalname(LocVars[] locvars, int j) {
|
private String getlocalname(LocVars[] locvars, int j) {
|
||||||
int number = j+1;
|
int number = j+1;
|
||||||
|
if ( gendebuginfo ) {
|
||||||
for (int i = 0; i < locvars.length; i++) {
|
for (int i = 0; i < locvars.length; i++) {
|
||||||
if (pc < locvars[i].endpc) { /* is variable active? */
|
if (pc < locvars[i].endpc) { /* is variable active? */
|
||||||
if (--number == 0)
|
if (--number == 0)
|
||||||
return locvars[i].varname.toString();
|
return locvars[i].varname.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return "$"+j;
|
return "$"+j;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user