diff --git a/jse/src/main/java/luajc.java b/jse/src/main/java/luajc.java index 81113bc8..6af7cd7b 100644 --- a/jse/src/main/java/luajc.java +++ b/jse/src/main/java/luajc.java @@ -1,24 +1,24 @@ /******************************************************************************* -* Copyright (c) 2009-2012 Luaj.org. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* 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 -* THE SOFTWARE. -******************************************************************************/ + * Copyright (c) 2009-2012 Luaj.org. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * 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 + * THE SOFTWARE. + ******************************************************************************/ import java.io.File; import java.io.FileInputStream; @@ -36,27 +36,27 @@ import org.luaj.vm2.libs.jse.JsePlatform; import org.luaj.vm2.luajc.LuaJC; /** - * Compiler for lua files to compile lua sources or lua binaries into java classes. + * Compiler for lua files to compile lua sources or lua binaries into java classes. */ public class luajc { private static final String version = Lua._VERSION + " Copyright (C) 2012 luaj.org"; - private static final String usage = - "usage: java -cp luaj-jse.jar,bcel-5.2.jar luajc [options] fileordir [, fileordir ...]\n" + - "Available options are:\n" + - " - process stdin\n" + - " -s src source directory\n" + - " -d dir destination directory\n" + - " -p pkg package prefix to apply to all classes\n" + - " -m generate main(String[]) function for JSE\n" + - " -r recursively compile all\n" + - " -l load classes to verify generated bytecode\n" + - " -c enc use the supplied encoding 'enc' for input files\n" + - " -v verbose\n"; - + private static final String usage = + "usage: java -cp luaj-jse.jar,bcel-5.2.jar luajc [options] fileordir [, fileordir ...]\n" + + "Available options are:\n" + + " - process stdin\n" + + " -s src source directory\n" + + " -d dir destination directory\n" + + " -p pkg package prefix to apply to all classes\n" + + " -m generate main(String[]) function for JSE\n" + + " -r recursively compile all\n" + + " -l load classes to verify generated bytecode\n" + + " -c enc use the supplied encoding 'enc' for input files\n" + + " -v verbose\n"; + private static void usageExit() { System.out.println(usage); - System.exit(-1); + System.exit(-1); } private String srcdir = "."; @@ -75,55 +75,55 @@ public class luajc { } private luajc( String[] args ) throws IOException { - + // process args List seeds = new ArrayList (); - + // get stateful args for ( int i=0; i= args.length ) + case 's': + 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(); - 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(); - break; + break; } } } - + // echo version if ( verbose ) { System.out.println(version); @@ -142,19 +142,19 @@ public class luajc { // collect up files to process for ( int i=0; i=0 ) { + String d = (destdir!=null? destdir+"/": "")+key.substring(0,key.lastIndexOf('/')); + new File(d).mkdirs(); + } + String destpath = (destdir!=null? destdir+"/": "") + key + ".class"; + if ( verbose ) + System.out.println( " "+destpath +" ("+bytes.length+" bytes)"); + FileOutputStream fos = new FileOutputStream( destpath ); + fos.write( bytes ); + fos.close(); + } + + // try to load the files + if ( loadclasses ) { ClassLoader loader = new LocalClassLoader(t); - for ( Enumeration e = t.keys(); e.hasMoreElements(); ) { - String classname = (String) e.nextElement(); - try { - Class c = loader.loadClass(classname); - Object o = c.newInstance(); - if ( verbose ) - System.out.println(" loaded "+classname+" as "+o ); - } catch ( Exception ex ) { - System.out.flush(); - System.err.println(" failed to load "+classname+": "+ex ); - System.err.flush(); - } - } - } - + for ( Enumeration e = t.keys(); e.hasMoreElements(); ) { + String classname = (String) e.nextElement(); + try { + Class c = loader.loadClass(classname); + Object o = c.newInstance(); + if ( verbose ) + System.out.println(" loaded "+classname+" as "+o ); + } catch ( Exception ex ) { + System.out.flush(); + System.err.println(" failed to load "+classname+": "+ex ); + System.err.flush(); + } + } + } + } catch ( Exception e ) { System.err.println(" failed to load "+inf.srcfilename+": "+e ); e.printStackTrace( System.err ); System.err.flush(); } } -} +} \ No newline at end of file diff --git a/jse/src/main/java/org/luaj/vm2/luajc/JavaBuilder.java b/jse/src/main/java/org/luaj/vm2/luajc/JavaBuilder.java index c5ebf96e..52032412 100644 --- a/jse/src/main/java/org/luaj/vm2/luajc/JavaBuilder.java +++ b/jse/src/main/java/org/luaj/vm2/luajc/JavaBuilder.java @@ -77,7 +77,7 @@ public class JavaBuilder { private static final String STR_LUATABLE = LuaTable.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_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_LUAVALUE = new ObjectType(STR_LUAVALUE); diff --git a/jse/src/main/java/org/luaj/vm2/luajc/LuaJC.java b/jse/src/main/java/org/luaj/vm2/luajc/LuaJC.java index 036605fb..842ec973 100644 --- a/jse/src/main/java/org/luaj/vm2/luajc/LuaJC.java +++ b/jse/src/main/java/org/luaj/vm2/luajc/LuaJC.java @@ -1,24 +1,24 @@ /******************************************************************************* -* Copyright (c) 2010 Luaj.org. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* 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 -* THE SOFTWARE. -******************************************************************************/ + * Copyright (c) 2010 Luaj.org. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * 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 + * THE SOFTWARE. + ******************************************************************************/ package org.luaj.vm2.luajc; import java.io.IOException; @@ -32,19 +32,18 @@ import org.luaj.vm2.LuaFunction; import org.luaj.vm2.LuaValue; import org.luaj.vm2.Prototype; import org.luaj.vm2.compiler.LuaC; -import org.luaj.vm2.libs.jse.JsePlatform; /** - * Implementation of {@link Globals.Compiler} which does direct - * lua-to-java-bytecode compiling. + * Implementation of {@link org.luaj.vm2.Globals.Compiler} which does direct + * lua-to-java-bytecode compiling. *

- * 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} - * 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}. + * 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}. *

* To override the default compiling behavior with {@link LuaJC} - * lua-to-java bytecode compiler, install it before undumping code, + * lua-to-java bytecode compiler, install it before undumping code, * for example: *

 {@code
  * LuaValue globals = JsePlatform.standardGlobals();
@@ -54,27 +53,27 @@ import org.luaj.vm2.libs.jse.JsePlatform;
  * chunk.call();
  * } 
*

- * This requires the bcel library to be on the class path to work as expected. - * If the library is not found, the default {@link LuaC} lua-to-lua-bytecode - * compiler will be used. - * + * This requires the bcel library to be on the class path to work as expected. + * If the library is not found, the default {@link LuaC} lua-to-lua-bytecode + * compiler will be used. + * * @see Globals#compiler * @see #install(Globals) - * @see LuaC + * @see org.luaj.vm2.compiler.LuaC * @see LuaValue */ public class LuaJC implements Globals.Loader { - + public static final LuaJC instance = new LuaJC(); - - /** - * Install the compiler as the main Globals.Loader to use in a set of globals. + + /** + * Install the compiler as the main Globals.Loader to use in a set of globals. * Will fall back to the LuaC prototype compiler. */ public static final void install(Globals G) { - G.loader = instance; + G.loader = instance; } - + protected LuaJC() {} public Hashtable compileAll(InputStream script, String chunkname, String filename, Globals globals, boolean genmain) throws IOException { @@ -82,13 +81,13 @@ public class LuaJC implements Globals.Loader { final Prototype p = globals.loadPrototype(script, classname, "bt"); return compileProtoAndSubProtos(p, classname, filename, genmain); } - + public Hashtable compileAll(Reader script, String chunkname, String filename, Globals globals, boolean genmain) throws IOException { final String classname = toStandardJavaClassName( chunkname ); final Prototype p = globals.compilePrototype(script, classname); return compileProtoAndSubProtos(p, classname, filename, genmain); } - + private Hashtable compileProtoAndSubProtos(Prototype p, String classname, String filename, boolean genmain) throws IOException { final String luaname = toStandardLuaFileName( filename ); final Hashtable h = new Hashtable(); @@ -96,7 +95,7 @@ public class LuaJC implements Globals.Loader { insert( h, gen ); return h; } - + private void insert(Hashtable h, JavaGen gen) { h.put(gen.classname, gen.bytecode); for ( int i=0, n=gen.inners!=null? gen.inners.length: 0; 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(); } - + private static String toStandardLuaFileName( String luachunkname ) { String stub = toStub( luachunkname ); String filename = stub.replace('.','/')+".lua"; return filename.startsWith("@")? filename.substring(1): filename; } - + private static String toStub( String s ) { String stub = s.endsWith(".lua")? s.substring(0,s.length()-4): s; return stub; } -} +} \ No newline at end of file diff --git a/jse/src/test/java/org/luaj/vm2/libs/jse/LuajavaAccessibleMembersTest.java b/jse/src/test/java/org/luaj/vm2/libs/jse/LuajavaAccessibleMembersTest.java index 12044ee7..fa25367b 100644 --- a/jse/src/test/java/org/luaj/vm2/libs/jse/LuajavaAccessibleMembersTest.java +++ b/jse/src/test/java/org/luaj/vm2/libs/jse/LuajavaAccessibleMembersTest.java @@ -61,7 +61,7 @@ public class LuajavaAccessibleMembersTest extends TestCase { } 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()+"');" + "return b.SomeEnum")); }