Java method for compiling Lua code with the LuaJ interpreter
http://luaj.org/luaj/README.html
我正在使用Luaj在Java应用程序中运行Lua代码。 我得到的结果确实很慢,因此我想在运行代码之前尝试编译代码以计算Lua脚本的实际处理时间。
问题是-Luaj确实显示了一个示例,该示例如何通过命令提示符将Lua源代码编译为Lua或Java字节码,但没有向我展示使用Java应用程序编译Lua脚本的代码。
它仅显示如何编译和运行Lua脚本:
1 2 3 4 5 6 | import org.luaj.vm2.*; import org.luaj.vm2.lib.jse.*; String script ="examples/lua/hello.lua"; LuaValue _G = JsePlatform.standardGlobals(); _G.get("dofile").call( LuaValue.valueOf(script) ); |
我想找到只将Lua编译为Lua或Java字节码并输出字节码文件的代码。
LuaJ包含一个Lua到字节码编译器。 因此,您只需要看一下源代码即可。 我在这里提取了最相关的部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | private void processScript( InputStream script, String chunkname, OutputStream out ) throws IOException { try { // create the chunk Prototype chunk = LuaC.instance.compile(script, chunkname); // list the chunk if (list) Print.printCode(chunk); // write out the chunk if (!parseonly) { DumpState.dump(chunk, out, stripdebug, numberformat, littleendian); } } catch ( Exception e ) { e.printStackTrace( System.err ); } finally { script.close(); } } |
请记住,您只能真正依赖与生成它的Lua实现兼容的字节码。