Add ability to create runnable jar file from lua script with sample build file build-app.xml
This commit is contained in:
@@ -732,6 +732,7 @@ and LuaForge:
|
||||
<li>Add line and column info to org.luaj.vm2.ast parse tree elements generated using LuaParser </li>
|
||||
<li>Drop support for lua source to java surce (lua2java) in favor of direct java bytecode output (luajc) </li>
|
||||
<li>Remove compatibility functions like table.getn(), table.maxn(), table.foreach(), and math.log10() </li>
|
||||
<li>Add ability to create runnable jar file from lua script with sample build file build-app.xml </li>
|
||||
</ul></td></tr>
|
||||
</table></td></tr></table>
|
||||
|
||||
|
||||
108
build-app.xml
Normal file
108
build-app.xml
Normal file
@@ -0,0 +1,108 @@
|
||||
<!-- And build script to compile lua scripts into runnable jar files using the "luajc"
|
||||
lua to java bytecode compiler.
|
||||
|
||||
Each source file is converted into a runnable jar file that takes arguments from the command line.
|
||||
For example, the program test/lua/perf/binarytrees.lua is converted to a jar that can be run with
|
||||
java -jar binarytrees.jar 15
|
||||
-->
|
||||
<project default="all">
|
||||
<import file="build.xml"/>
|
||||
|
||||
<import file="build-libs.xml"/>
|
||||
|
||||
<available file="luaj-jse-${version}.jar" property="luaj.lib.exists"/>
|
||||
|
||||
<!-- this may need to be changed when building on mac -->
|
||||
<property name="rt.jar" value="${java.home}/lib/rt.jar"/>
|
||||
|
||||
<target name="luaj-lib" unless="luaj.lib.exists">
|
||||
<antcall target="jar-jse"/>
|
||||
</target>
|
||||
|
||||
<macrodef name="perftest">
|
||||
<attribute name="cmd"/>
|
||||
<sequential>
|
||||
<echo level="info">------ @{cmd}</echo>
|
||||
<exec executable="bash">
|
||||
<arg value="-c"/>
|
||||
<arg value="time @{cmd}"/>
|
||||
</exec>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
<macrodef name="buildappjar">
|
||||
<attribute name="luaprog"/>
|
||||
<attribute name="arg" default=""/>
|
||||
<attribute name="srcdir" default="test/lua/perf"/>
|
||||
<sequential>
|
||||
<echo level="info">=========== @{srcdir}/@{luaprog} =============</echo>
|
||||
<delete dir="build/@{luaprog}"/>
|
||||
<mkdir dir="build/@{luaprog}/class"/>
|
||||
<java classname="luajc">
|
||||
<classpath>
|
||||
<pathelement path="luaj-jse-${version}.jar"/>
|
||||
<pathelement path="lib/bcel-5.2.jar"/>
|
||||
</classpath>
|
||||
<arg value="-s"/>
|
||||
<arg path="@{srcdir}"/>
|
||||
<arg value="-d"/>
|
||||
<arg path="build/@{luaprog}/class"/>
|
||||
<arg value="-m"/>
|
||||
<arg value="-v"/>
|
||||
<arg value="@{luaprog}.lua"/>
|
||||
</java>
|
||||
<jar destfile="build/@{luaprog}.jar">
|
||||
<fileset dir="build/@{luaprog}/class"/>
|
||||
<zipfileset includes="org/luaj/vm2/*.class,org/luaj/vm2/lib/*.class,org/luaj/vm2/lib/jse/*.class,org/luaj/vm2/compiler/*.class" src="luaj-jse-${version}.jar" />
|
||||
<manifest>
|
||||
<attribute name="Main-Class" value="@{luaprog}" />
|
||||
</manifest>
|
||||
</jar>
|
||||
<unjar src="build/@{luaprog}.jar" dest="build/@{luaprog}/unjarred"/>
|
||||
<perftest cmd="java -jar build/@{luaprog}.jar @{arg}"/>
|
||||
|
||||
<!-- The following can be adapted to produce an optimized jar.
|
||||
<taskdef resource="proguard/ant/task.properties" classpath="lib/proguard.jar" />
|
||||
<proguard>
|
||||
-injars build/@{luaprog}.jar
|
||||
-outjars build/@{luaprog}-opt.jar
|
||||
-libraryjars ${rt.jar}
|
||||
-overloadaggressively
|
||||
-repackageclasses ''
|
||||
-allowaccessmodification
|
||||
-printmapping build/@{luaprog}.map
|
||||
|
||||
-keep public class @{luaprog} {
|
||||
public static void main(java.lang.String[]);
|
||||
}
|
||||
</proguard>
|
||||
<unjar src="build/@{luaprog}-opt.jar" dest="build/@{luaprog}/unjarred-opt"/>
|
||||
<perftest cmd="java -jar build/@{luaprog}-opt.jar @{arg}"/>
|
||||
-->
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
<target name="binarytrees" depends="luaj-lib,proguard-lib">
|
||||
<buildappjar luaprog="binarytrees" arg="15"/>
|
||||
</target>
|
||||
|
||||
<target name="fannkuch" depends="luaj-lib,proguard-lib">
|
||||
<buildappjar luaprog="fannkuch" arg="10"/>
|
||||
</target>
|
||||
|
||||
<target name="nbody" depends="luaj-lib,proguard-lib">
|
||||
<buildappjar luaprog="nbody" arg="1000000"/>
|
||||
</target>
|
||||
|
||||
<target name="nsieve" depends="luaj-lib,proguard-lib">
|
||||
<buildappjar luaprog="nsieve" arg="8"/>
|
||||
</target>
|
||||
|
||||
<target name="swingapp" depends="luaj-lib,proguard-lib">
|
||||
<buildappjar luaprog="swingapp" srcdir="examples/lua"/>
|
||||
</target>
|
||||
|
||||
<target name="allappjars" depends="binarytrees,fannkuch,nbody,nsieve,swingapp"/>
|
||||
|
||||
<target name="all" depends="allappjars"/>
|
||||
</project>
|
||||
@@ -62,14 +62,6 @@ public class LuaFunction extends LuaValue {
|
||||
return s_metatable;
|
||||
}
|
||||
|
||||
/** Hook for implementations such as LuaJC to load the environment of the main chunk
|
||||
* into the first upvalue location. If the function has no upvalues or is not a main chunk,
|
||||
* calling this will be no effect.
|
||||
* @param env The environment to load into the first upvalue, if there is one.
|
||||
*/
|
||||
public void initupvalue1(LuaValue env) {
|
||||
}
|
||||
|
||||
public String tojstring() {
|
||||
return "function: " + classnamestub();
|
||||
}
|
||||
|
||||
@@ -3524,6 +3524,13 @@ public class LuaValue extends Varargs {
|
||||
return invoke(args);
|
||||
}
|
||||
|
||||
/** Hook for implementations such as LuaJC to load the environment of the main chunk
|
||||
* into the first upvalue location. If the function has no upvalues or is not a main chunk,
|
||||
* calling this will be no effect.
|
||||
* @param env The environment to load into the first upvalue, if there is one.
|
||||
*/
|
||||
public void initupvalue1(LuaValue env) {}
|
||||
|
||||
/** Varargs implemenation with no values.
|
||||
* <p>
|
||||
* This is an internal class not intended to be used directly.
|
||||
|
||||
@@ -24,6 +24,7 @@ package org.luaj.vm2.lib;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.luaj.vm2.Globals;
|
||||
import org.luaj.vm2.LuaFunction;
|
||||
import org.luaj.vm2.LuaString;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
@@ -306,6 +307,8 @@ public class PackageLib extends OneArgFunction {
|
||||
try {
|
||||
c = Class.forName(classname);
|
||||
v = (LuaValue) c.newInstance();
|
||||
if (v.isfunction())
|
||||
((LuaFunction)v).initupvalue1(globals);
|
||||
return v;
|
||||
} catch ( ClassNotFoundException cnfe ) {
|
||||
return valueOf("\n\tno class '"+classname+"'" );
|
||||
|
||||
@@ -46,6 +46,7 @@ import org.luaj.vm2.Varargs;
|
||||
* @see ThreeArgFunction
|
||||
*/
|
||||
abstract public class VarArgFunction extends LibFunction {
|
||||
|
||||
public VarArgFunction() {
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ import org.luaj.vm2.luajc.LuaJC;
|
||||
* 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) 2009 luaj.org";
|
||||
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" +
|
||||
@@ -46,6 +46,7 @@ public class luajc {
|
||||
" -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" +
|
||||
" -v verbose\n";
|
||||
@@ -57,6 +58,7 @@ public class luajc {
|
||||
|
||||
private String srcdir = null;
|
||||
private String destdir = null;
|
||||
private boolean genmain = false;
|
||||
private boolean recurse = false;
|
||||
private boolean verbose = false;
|
||||
private boolean loadclasses = false;
|
||||
@@ -96,6 +98,9 @@ public class luajc {
|
||||
usageExit();
|
||||
pkgprefix = args[i];
|
||||
break;
|
||||
case 'm':
|
||||
genmain = true;
|
||||
break;
|
||||
case 'r':
|
||||
recurse = true;
|
||||
break;
|
||||
@@ -113,7 +118,7 @@ public class luajc {
|
||||
if ( verbose ) {
|
||||
System.out.println(version);
|
||||
System.out.println("srcdir: "+srcdir);
|
||||
System.out.println("destdir: "+srcdir);
|
||||
System.out.println("destdir: "+destdir);
|
||||
System.out.println("files: "+seeds);
|
||||
System.out.println("recurse: "+recurse);
|
||||
}
|
||||
@@ -192,7 +197,7 @@ public class luajc {
|
||||
|
||||
// create the chunk
|
||||
FileInputStream fis = new FileInputStream( inf.infile );
|
||||
final Hashtable t = LuaJC.getInstance().compileAll(fis, inf.luachunkname, inf.srcfilename);
|
||||
final Hashtable t = LuaJC.getInstance().compileAll(fis, inf.luachunkname, inf.srcfilename, genmain);
|
||||
fis.close();
|
||||
|
||||
// write out the chunk
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.luaj.vm2.Globals;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaThread;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.Varargs;
|
||||
import org.luaj.vm2.compiler.LuaC;
|
||||
import org.luaj.vm2.lib.Bit32Lib;
|
||||
import org.luaj.vm2.lib.CoroutineLib;
|
||||
@@ -120,4 +121,22 @@ public class JsePlatform {
|
||||
_G.load(new DebugLib());
|
||||
return _G;
|
||||
}
|
||||
|
||||
|
||||
/** Simple wrapper for invoking a lua function with command line arguments.
|
||||
* The supplied function is first given a new Globals object,
|
||||
* then the program is run with arguments.
|
||||
*/
|
||||
public static void luaMain(LuaValue mainChunk, String[] args) {
|
||||
Globals g = standardGlobals();
|
||||
int n = args.length;
|
||||
LuaValue[] vargs = new LuaValue[args.length];
|
||||
for (int i = 0; i < n; ++i)
|
||||
vargs[i] = LuaValue.valueOf(args[i]);
|
||||
LuaTable arg = LuaValue.listOf(vargs);
|
||||
arg.set("n", n);
|
||||
g.set("arg", arg);
|
||||
mainChunk.initupvalue1(g);
|
||||
mainChunk.invoke(LuaValue.varargsOf(vargs));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +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 ObjectType TYPE_VARARGS = new ObjectType(STR_VARARGS);
|
||||
private static final ObjectType TYPE_LUAVALUE = new ObjectType(STR_LUAVALUE);
|
||||
@@ -86,10 +87,11 @@ public class JavaBuilder {
|
||||
private static final ObjectType TYPE_LUABOOLEAN = new ObjectType(STR_LUABOOLEAN);
|
||||
private static final ObjectType TYPE_LUATABLE = new ObjectType(STR_LUATABLE);
|
||||
private static final ObjectType TYPE_BUFFER = new ObjectType(STR_BUFFER);
|
||||
private static final ObjectType TYPE_STRING = new ObjectType(STR_STRING);
|
||||
|
||||
private static final ArrayType TYPE_LOCALUPVALUE = new ArrayType( TYPE_LUAVALUE, 1 );
|
||||
private static final ArrayType TYPE_CHARARRAY = new ArrayType( Type.CHAR, 1 );
|
||||
private static final ArrayType TYPE_LUAVALUEARRAY = new ArrayType( TYPE_LUAVALUE, 1 );
|
||||
private static final ArrayType TYPE_STRINGARRAY = new ArrayType( TYPE_STRING, 1 );
|
||||
|
||||
|
||||
private static final String STR_FUNCV = VarArgFunction.class.getName();
|
||||
@@ -116,6 +118,8 @@ public class JavaBuilder {
|
||||
private static final Type[] ARG_TYPES_INT_INT = { Type.INT, Type.INT };
|
||||
private static final Type[] ARG_TYPES_LUAVALUE = { TYPE_LUAVALUE };
|
||||
private static final Type[] ARG_TYPES_BUFFER = { TYPE_BUFFER };
|
||||
private static final Type[] ARG_TYPES_STRINGARRAY = { TYPE_STRINGARRAY };
|
||||
private static final Type[] ARG_TYPES_LUAVALUE_STRINGARRAY = { TYPE_LUAVALUE, TYPE_STRINGARRAY };
|
||||
|
||||
// names, arg types for main prototype classes
|
||||
private static final String[] SUPER_NAME_N = { STR_FUNC0, STR_FUNC1, STR_FUNC2, STR_FUNC3, STR_FUNCV, };
|
||||
@@ -257,7 +261,7 @@ public class JavaBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] completeClass() {
|
||||
public byte[] completeClass(boolean genmain) {
|
||||
|
||||
// add class initializer
|
||||
if ( ! init.isEmpty() ) {
|
||||
@@ -303,6 +307,26 @@ public class JavaBuilder {
|
||||
main.dispose();
|
||||
}
|
||||
|
||||
// add main function so class is invokable from the java command line
|
||||
if (genmain) {
|
||||
MethodGen mg = new MethodGen( Constants.ACC_PUBLIC | Constants.ACC_STATIC, // access flags
|
||||
Type.VOID, // return type
|
||||
ARG_TYPES_STRINGARRAY, // argument types
|
||||
new String[] { "arg" }, // arg names
|
||||
"main",
|
||||
classname, // method, defining class
|
||||
main, cp);
|
||||
append(factory.createNew(classname));
|
||||
append(InstructionConstants.DUP);
|
||||
append(factory.createInvoke(classname, Constants.CONSTRUCTOR_NAME, Type.VOID, ARG_TYPES_NONE, Constants.INVOKESPECIAL));
|
||||
append(new ALOAD(0));
|
||||
append(factory.createInvoke(STR_JSEPLATFORM, "luaMain", Type.VOID, ARG_TYPES_LUAVALUE_STRINGARRAY, Constants.INVOKESTATIC));
|
||||
append(InstructionConstants.RETURN);
|
||||
mg.setMaxStack();
|
||||
cg.addMethod(mg.getMethod());
|
||||
main.dispose();
|
||||
}
|
||||
|
||||
|
||||
// convert to class bytes
|
||||
try {
|
||||
|
||||
@@ -37,11 +37,11 @@ public class JavaGen {
|
||||
public final byte[] bytecode;
|
||||
public final JavaGen[] inners;
|
||||
|
||||
public JavaGen( Prototype p, String classname, String filename ) {
|
||||
this( new ProtoInfo(p,classname), classname, filename );
|
||||
public JavaGen( Prototype p, String classname, String filename, boolean genmain ) {
|
||||
this( new ProtoInfo(p,classname), classname, filename, genmain );
|
||||
}
|
||||
|
||||
private JavaGen( ProtoInfo pi, String classname, String filename ) {
|
||||
private JavaGen( ProtoInfo pi, String classname, String filename, boolean genmain ) {
|
||||
this.classname = classname;
|
||||
|
||||
// build this class
|
||||
@@ -51,14 +51,14 @@ public class JavaGen {
|
||||
LocVars l = pi.prototype.locvars[i];
|
||||
builder.setVarStartEnd(i, l.startpc, l.endpc, l.varname.tojstring());
|
||||
}
|
||||
this.bytecode = builder.completeClass();
|
||||
this.bytecode = builder.completeClass(genmain);
|
||||
|
||||
// build sub-prototypes
|
||||
if ( pi.subprotos != null ) {
|
||||
int n = pi.subprotos.length;
|
||||
inners = new JavaGen[n];
|
||||
for ( int i=0; i<n; i++ )
|
||||
inners[i] = new JavaGen(pi.subprotos[i], closureName(classname,i), filename);
|
||||
inners[i] = new JavaGen(pi.subprotos[i], closureName(classname,i), filename, false);
|
||||
} else {
|
||||
inners = null;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class JavaLoader extends ClassLoader {
|
||||
}
|
||||
|
||||
public LuaFunction load( Prototype p, String classname, String filename, LuaValue env ) {
|
||||
JavaGen jg = new JavaGen( p, classname, filename );
|
||||
JavaGen jg = new JavaGen( p, classname, filename, false );
|
||||
return load( jg, env );
|
||||
}
|
||||
|
||||
|
||||
@@ -79,12 +79,12 @@ public class LuaJC implements LuaCompiler {
|
||||
public LuaJC() {
|
||||
}
|
||||
|
||||
public Hashtable compileAll(InputStream script, String chunkname, String filename) throws IOException {
|
||||
public Hashtable compileAll(InputStream script, String chunkname, String filename, boolean genmain) throws IOException {
|
||||
String classname = toStandardJavaClassName( chunkname );
|
||||
String luaname = toStandardLuaFileName( filename );
|
||||
Hashtable h = new Hashtable();
|
||||
Prototype p = LuaC.instance.compile(script, classname);
|
||||
JavaGen gen = new JavaGen(p, classname, luaname);
|
||||
JavaGen gen = new JavaGen(p, classname, luaname, genmain);
|
||||
insert( h, gen );
|
||||
return h;
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public class TestLuaJC {
|
||||
String destdir = ".";
|
||||
|
||||
InputStream is = _G.FINDER.findResource(filename);
|
||||
Hashtable t = LuaJC.getInstance().compileAll(is, filename, filename);
|
||||
Hashtable t = LuaJC.getInstance().compileAll(is, filename, filename, true);
|
||||
|
||||
// write out the chunk
|
||||
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
|
||||
|
||||
Reference in New Issue
Block a user