Fixed LuaJC not writing java package to bytecode correctly

This commit is contained in:
UnlegitDqrk
2026-03-01 13:11:33 +01:00
parent 01739d4e77
commit 7338475ae4
4 changed files with 185 additions and 167 deletions

View File

@@ -1,24 +1,24 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009-2012 Luaj.org. All rights reserved. * Copyright (c) 2009-2012 Luaj.org. All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
******************************************************************************/ ******************************************************************************/
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@@ -42,17 +42,17 @@ public class luajc {
private static final String version = Lua._VERSION + " Copyright (C) 2012 luaj.org"; private static final String version = Lua._VERSION + " Copyright (C) 2012 luaj.org";
private static final String usage = private static final String usage =
"usage: java -cp luaj-jse.jar,bcel-5.2.jar luajc [options] fileordir [, fileordir ...]\n" + "usage: java -cp luaj-jse.jar,bcel-5.2.jar luajc [options] fileordir [, fileordir ...]\n" +
"Available options are:\n" + "Available options are:\n" +
" - process stdin\n" + " - process stdin\n" +
" -s src source directory\n" + " -s src source directory\n" +
" -d dir destination directory\n" + " -d dir destination directory\n" +
" -p pkg package prefix to apply to all classes\n" + " -p pkg package prefix to apply to all classes\n" +
" -m generate main(String[]) function for JSE\n" + " -m generate main(String[]) function for JSE\n" +
" -r recursively compile all\n" + " -r recursively compile all\n" +
" -l load classes to verify generated bytecode\n" + " -l load classes to verify generated bytecode\n" +
" -c enc use the supplied encoding 'enc' for input files\n" + " -c enc use the supplied encoding 'enc' for input files\n" +
" -v verbose\n"; " -v verbose\n";
private static void usageExit() { private static void usageExit() {
System.out.println(usage); System.out.println(usage);
@@ -85,41 +85,41 @@ public class luajc {
seeds.add(args[i]); seeds.add(args[i]);
} else { } else {
switch ( args[i].charAt(1) ) { switch ( args[i].charAt(1) ) {
case 's': case 's':
if ( ++i >= args.length ) if ( ++i >= args.length )
usageExit();
srcdir = args[i];
break;
case 'd':
if ( ++i >= args.length )
usageExit();
destdir = args[i];
break;
case 'l':
loadclasses = true;
break;
case 'p':
if ( ++i >= args.length )
usageExit();
pkgprefix = args[i];
break;
case 'm':
genmain = true;
break;
case 'r':
recurse = true;
break;
case 'c':
if ( ++i >= args.length )
usageExit();
encoding = args[i];
break;
case 'v':
verbose = true;
break;
default:
usageExit(); usageExit();
srcdir = args[i]; break;
break;
case 'd':
if ( ++i >= args.length )
usageExit();
destdir = args[i];
break;
case 'l':
loadclasses = true;
break;
case 'p':
if ( ++i >= args.length )
usageExit();
pkgprefix = args[i];
break;
case 'm':
genmain = true;
break;
case 'r':
recurse = true;
break;
case 'c':
if ( ++i >= args.length )
usageExit();
encoding = args[i];
break;
case 'v':
verbose = true;
break;
default:
usageExit();
break;
} }
} }
} }
@@ -188,11 +188,13 @@ public class luajc {
} }
public Class findClass(String classname) throws ClassNotFoundException { public Class findClass(String classname) throws ClassNotFoundException {
byte[] bytes = (byte[]) t.get(classname); byte[] bytes = (byte[]) t.get(classname);
if ( bytes != null ) if ( bytes != null ) {
return defineClass(classname, bytes, 0, bytes.length); classname = classname.replace('/', '.');
return super.findClass(classname); return defineClass(classname, bytes, 0, bytes.length);
} }
return super.findClass(classname);
}
} }
class InputFile { class InputFile {
@@ -220,46 +222,46 @@ public class luajc {
if ( verbose ) if ( verbose )
System.out.println("chunk="+inf.luachunkname+" srcfile="+inf.srcfilename); System.out.println("chunk="+inf.luachunkname+" srcfile="+inf.srcfilename);
// create the chunk // create the chunk
FileInputStream fis = new FileInputStream( inf.infile ); FileInputStream fis = new FileInputStream( inf.infile );
final Hashtable t = encoding != null? final Hashtable t = encoding != null?
LuaJC.instance.compileAll( new InputStreamReader(fis, encoding), inf.luachunkname, inf.srcfilename, globals, genmain): LuaJC.instance.compileAll( new InputStreamReader(fis, encoding), inf.luachunkname, inf.srcfilename, globals, genmain):
LuaJC.instance.compileAll( fis, inf.luachunkname, inf.srcfilename, globals, genmain); LuaJC.instance.compileAll( fis, inf.luachunkname, inf.srcfilename, globals, genmain);
fis.close(); fis.close();
// write out the chunk // write out the chunk
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) { for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement(); String key = (String) e.nextElement();
byte[] bytes = (byte[]) t.get(key); byte[] bytes = (byte[]) t.get(key);
if ( key.indexOf('/')>=0 ) { if ( key.indexOf('/')>=0 ) {
String d = (destdir!=null? destdir+"/": "")+key.substring(0,key.lastIndexOf('/')); String d = (destdir!=null? destdir+"/": "")+key.substring(0,key.lastIndexOf('/'));
new File(d).mkdirs(); new File(d).mkdirs();
} }
String destpath = (destdir!=null? destdir+"/": "") + key + ".class"; String destpath = (destdir!=null? destdir+"/": "") + key + ".class";
if ( verbose ) if ( verbose )
System.out.println( " "+destpath +" ("+bytes.length+" bytes)"); System.out.println( " "+destpath +" ("+bytes.length+" bytes)");
FileOutputStream fos = new FileOutputStream( destpath ); FileOutputStream fos = new FileOutputStream( destpath );
fos.write( bytes ); fos.write( bytes );
fos.close(); fos.close();
} }
// try to load the files // try to load the files
if ( loadclasses ) { if ( loadclasses ) {
ClassLoader loader = new LocalClassLoader(t); ClassLoader loader = new LocalClassLoader(t);
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) { for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
String classname = (String) e.nextElement(); String classname = (String) e.nextElement();
try { try {
Class c = loader.loadClass(classname); Class c = loader.loadClass(classname);
Object o = c.newInstance(); Object o = c.newInstance();
if ( verbose ) if ( verbose )
System.out.println(" loaded "+classname+" as "+o ); System.out.println(" loaded "+classname+" as "+o );
} catch ( Exception ex ) { } catch ( Exception ex ) {
System.out.flush(); System.out.flush();
System.err.println(" failed to load "+classname+": "+ex ); System.err.println(" failed to load "+classname+": "+ex );
System.err.flush(); System.err.flush();
} }
} }
} }
} catch ( Exception e ) { } catch ( Exception e ) {
System.err.println(" failed to load "+inf.srcfilename+": "+e ); System.err.println(" failed to load "+inf.srcfilename+": "+e );

View File

@@ -77,7 +77,7 @@ public class JavaBuilder {
private static final String STR_LUATABLE = LuaTable.class.getName(); private static final String STR_LUATABLE = LuaTable.class.getName();
private static final String STR_BUFFER = Buffer.class.getName(); private static final String STR_BUFFER = Buffer.class.getName();
private static final String STR_STRING = String.class.getName(); private static final String STR_STRING = String.class.getName();
private static final String STR_JSEPLATFORM = "org.luaj.vm2.lib.jse.JsePlatform"; private static final String STR_JSEPLATFORM = "org.luaj.vm2.libs.jse.JsePlatform";
private static final ObjectType TYPE_VARARGS = new ObjectType(STR_VARARGS); private static final ObjectType TYPE_VARARGS = new ObjectType(STR_VARARGS);
private static final ObjectType TYPE_LUAVALUE = new ObjectType(STR_LUAVALUE); private static final ObjectType TYPE_LUAVALUE = new ObjectType(STR_LUAVALUE);

View File

@@ -1,24 +1,24 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2010 Luaj.org. All rights reserved. * Copyright (c) 2010 Luaj.org. All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
******************************************************************************/ ******************************************************************************/
package org.luaj.vm2.luajc; package org.luaj.vm2.luajc;
import java.io.IOException; import java.io.IOException;
@@ -32,13 +32,12 @@ import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Prototype; import org.luaj.vm2.Prototype;
import org.luaj.vm2.compiler.LuaC; import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.libs.jse.JsePlatform;
/** /**
* Implementation of {@link Globals.Compiler} which does direct * Implementation of {@link org.luaj.vm2.Globals.Compiler} which does direct
* lua-to-java-bytecode compiling. * lua-to-java-bytecode compiling.
* <p> * <p>
* By default, when using {@link JsePlatform} or * By default, when using {@link org.luaj.vm2.libs.jse.JsePlatform} or
* {@link org.luaj.vm2.libs.jme.JmePlatform} * {@link org.luaj.vm2.libs.jme.JmePlatform}
* to construct globals, the plain compiler {@link LuaC} is installed and lua code * to construct globals, the plain compiler {@link LuaC} is installed and lua code
* will only be compiled into lua bytecode and execute as {@link LuaClosure}. * will only be compiled into lua bytecode and execute as {@link LuaClosure}.
@@ -60,7 +59,7 @@ import org.luaj.vm2.libs.jse.JsePlatform;
* *
* @see Globals#compiler * @see Globals#compiler
* @see #install(Globals) * @see #install(Globals)
* @see LuaC * @see org.luaj.vm2.compiler.LuaC
* @see LuaValue * @see LuaValue
*/ */
public class LuaJC implements Globals.Loader { public class LuaJC implements Globals.Loader {
@@ -115,7 +114,24 @@ public class LuaJC implements Globals.Loader {
StringBuffer classname = new StringBuffer(); StringBuffer classname = new StringBuffer();
for (int i = 0, n = stub.length(); i < n; ++i) { for (int i = 0, n = stub.length(); i < n; ++i) {
final char c = stub.charAt(i); final char c = stub.charAt(i);
classname.append((((i == 0) && Character.isJavaIdentifierStart(c)) || ((i > 0) && Character.isJavaIdentifierPart(c)))? c: '_'); switch(i) {
case 0:
if(Character.isJavaIdentifierStart(c)) {
classname.append(c);
} else {
classname.append('_');
}
break;
default:
if(c == '/') {
classname.append(c);
} else if(Character.isJavaIdentifierPart(c)) {
classname.append(c);
} else {
classname.append('_');
}
break;
}
} }
return classname.toString(); return classname.toString();
} }

View File

@@ -61,7 +61,7 @@ public class LuajavaAccessibleMembersTest extends TestCase {
} }
public void testAccessPublicEnum() { public void testAccessPublicEnum() {
assertEquals("class org.luaj.vm2.lib.jse.TestClass$SomeEnum", invokeScript( assertEquals("class org.luaj.vm2.libs.jse.TestClass$SomeEnum", invokeScript(
"b = luajava.newInstance('"+TestClass.class.getName()+"');" + "b = luajava.newInstance('"+TestClass.class.getName()+"');" +
"return b.SomeEnum")); "return b.SomeEnum"));
} }