diff --git a/src/debug/org/luaj/debug/Print.java b/src/debug/org/luaj/debug/Print.java index d01780d1..69f49390 100644 --- a/src/debug/org/luaj/debug/Print.java +++ b/src/debug/org/luaj/debug/Print.java @@ -65,7 +65,7 @@ public class Print extends Lua { }; - static void printString(final LString s) { + static void printString(PrintStream ps, final LString s) { final byte[] bytes = s.m_bytes; final int off = s.m_offset; @@ -110,9 +110,9 @@ public class Print extends Lua { ps.print('"'); } - static void printValue( LValue v ) { + static void printValue( PrintStream ps, LValue v ) { if ( v instanceof LString ) - printString( v.luaAsString() ); + printString( ps, v.luaAsString() ); else if ( v instanceof LInteger ) { ps.print( v.toJavaInt() ); } else if ( v instanceof LDouble ) { @@ -128,8 +128,8 @@ public class Print extends Lua { } } - static void printConstant(LPrototype f, int i) { - printValue( f.k[i] ); + static void printConstant(PrintStream ps, LPrototype f, int i) { + printValue( ps, f.k[i] ); } public static void printCode(LPrototype f) { @@ -142,6 +142,10 @@ public class Print extends Lua { } public static void printOpCode(LPrototype f, int pc) { + printOpCode(ps,f,pc); + } + + public static void printOpCode(PrintStream ps, LPrototype f, int pc) { int[] code = f.code; int i = code[pc]; int o = GET_OPCODE(i); @@ -182,26 +186,26 @@ public class Print extends Lua { switch (o) { case OP_LOADK: ps.print(" ; "); - printConstant(f, bx); + printConstant(ps, f, bx); break; case OP_GETUPVAL: case OP_SETUPVAL: ps.print(" ; "); if ( f.upvalues.length > b ) - printValue(f.upvalues[b]); + printValue(ps, f.upvalues[b]); else ps.print( "-" ); break; case OP_GETGLOBAL: case OP_SETGLOBAL: ps.print(" ; "); - printConstant( f, bx ); + printConstant( ps, f, bx ); break; case OP_GETTABLE: case OP_SELF: if (ISK(c)) { ps.print(" ; "); - printConstant(f, INDEXK(c)); + printConstant(ps, f, INDEXK(c)); } break; case OP_SETTABLE: @@ -216,12 +220,12 @@ public class Print extends Lua { if (ISK(b) || ISK(c)) { ps.print(" ; "); if (ISK(b)) - printConstant(f, INDEXK(b)); + printConstant(ps, f, INDEXK(b)); else ps.print("-"); ps.print(" "); if (ISK(c)) - printConstant(f, INDEXK(c)); + printConstant(ps, f, INDEXK(c)); else ps.print("-"); } @@ -272,7 +276,7 @@ public class Print extends Lua { ps.print("constants (" + n + ") for " + id(f) + ":\n"); for (i = 0; i < n; i++) { ps.print(" " + (i + 1) + " "); - printValue( f.k[i] ); + printValue( ps, f.k[i] ); ps.print( "\n"); } } @@ -355,4 +359,5 @@ public class Print extends Lua { return "Proto"; } + } diff --git a/src/script/org/luaj/jit/LuaJit.java b/src/script/org/luaj/jit/LuaJit.java index 116289c6..5dc7385c 100644 --- a/src/script/org/luaj/jit/LuaJit.java +++ b/src/script/org/luaj/jit/LuaJit.java @@ -1,19 +1,27 @@ package org.luaj.jit; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.PrintWriter; +import java.io.PrintStream; +import java.util.Arrays; +import javax.tools.Diagnostic; +import javax.tools.DiagnosticCollector; import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; import javax.tools.ToolProvider; +import javax.tools.JavaCompiler.CompilationTask; import org.luaj.compiler.LuaC; import org.luaj.debug.Print; import org.luaj.platform.J2sePlatform; import org.luaj.vm.LClosure; import org.luaj.vm.LPrototype; -import org.luaj.vm.LTable; import org.luaj.vm.LValue; import org.luaj.vm.Lua; import org.luaj.vm.LuaState; @@ -30,11 +38,8 @@ public class LuaJit extends Lua { InputStream is = new ByteArrayInputStream(program.getBytes()); LPrototype p = LuaC.compile(is, "program"); test( p ); - LPrototype q = LuaJit.compile( p ); + LPrototype q = LuaJit.jitCompile( p ); test( q ); -// JitPrototype jp = new JitPrototypeOne(); -// jp.setLuaPrototype(p); -// test( jp ); } private static void test(LPrototype p) { @@ -47,21 +52,68 @@ public class LuaJit extends Lua { e.printStackTrace(); } } + + private static int counter = 0; + + private static synchronized String filename() { + return "LuaJit"+(counter++); + } - public static LPrototype compile( LPrototype p ) { - int i, a, b, c, o, n, cb; + public static LPrototype jitCompile( LPrototype p ) { + try { + final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + if (compiler == null) { + System.err.println("no java compiler"); + return p; + } + + // write the file + String name = filename(); + File source = new File(name+JavaFileObject.Kind.SOURCE.extension); + PrintStream ps = new PrintStream(new FileOutputStream(source)); + writeSource(ps, name, p); + ps.close(); + + // set up output location + Iterable dest = Arrays.asList(new File[] { new File("bin") }); + StandardJavaFileManager fm = compiler.getStandardFileManager( null, null, null); + fm.setLocation(StandardLocation.CLASS_OUTPUT, dest); + + // compile the file + Iterable compilationUnits = fm.getJavaFileObjects(source); + CompilationTask task = compiler.getTask(null, fm, null, null, null, compilationUnits); + boolean success = task.call(); + System.out.println("Success: " + success); + + // instantiate, config and return + if (success) { + Class clazz = Class.forName(name); + Object instance = clazz.newInstance(); + JitPrototype jp = (JitPrototype) instance; + jp.setLuaPrototype(p); + return jp; + } + } catch (Exception e) { + e.printStackTrace(System.err); + } + return p; + + } + + private static void writeSource( PrintStream ps, String name, LPrototype p ) { + + int i, a, b, c, o, n, cb; LValue rkb, rkc, nvarargs, key, val; LValue i0, step, idx, limit, init, table; boolean back, body; - - PrintWriter pw = new PrintWriter( System.out ); - String name = "JitPrototypeOne"; - pw.print( - "package org.luaj.jit;\n"+ - "import org.luaj.vm.LTable;\n"+ - "import org.luaj.vm.LuaState;\n"+ + + ps.print( + "import org.luaj.vm.*;\n"+ + "import org.luaj.jit.*;\n"+ + "\n"+ "public class "+name+" extends JitPrototype {\n"+ " public void jitCall(LuaState vm, LTable env) {\n"+ + " int base = vm.base;\n"+ "" ); int[] code = p.code; @@ -72,10 +124,9 @@ public class LuaJit extends Lua { for ( int pc=0; pc