http://luaj.org/luaj/README.html
我正在使用 Luaj 在 Java 应用程序中运行 Lua 代码。我得到的结果非常慢,所以我想在运行代码之前尝试编译代码,以计算 Lua 脚本的实际处理时间。
问题是 - Luaj 确实展示了如何通过命令提示符将 Lua 源代码编译为 Lua 或 Java 字节码的示例,但它没有向我展示使用 Java 应用程序编译 Lua 脚本的行。
它仅展示如何编译和运行 Lua 脚本:
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 到字节码的编译器。所以你直接看源码就可以了。我在这里提取了最相关的部分。
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 实现兼容的字节代码。
关于使用LuaJ解释器编译Lua代码的Java方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15642939/