简体   繁体   English

使用LuaJ解释器编译Lua代码的Java方法

[英]Java method for compiling Lua code with the LuaJ interpreter

http://luaj.org/luaj/README.html http://luaj.org/luaj/README.html

I'm using Luaj to run Lua code in a Java application. 我正在使用Luaj在Java应用程序中运行Lua代码。 I'm getting some really slow results, so I want to try to compile the code before running it to calculate the actual proccesing time of a Lua script. 我得到的结果确实很慢,因此我想在运行代码之前尝试编译代码以计算Lua脚本的实际处理时间。

The problem is - Luaj does show an example how to compile Lua source to Lua or Java bytecode through the command prompt, but it doesn't show me the lines to compile a Lua script with a Java application. 问题是-Luaj确实展示了如何通过命令提示符将Lua源代码编译为Lua或Java字节码的示例,但没有向我展示用Java应用程序编译Lua脚本的代码。

It only shows how to compile AND run a Lua script: 它仅显示如何编译和运行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) );

I want to find the code that would only compile Lua to Lua or Java bytecode and would output a bytecode file. 我想找到只将Lua编译为Lua或Java字节码并输出字节码文件的代码。

LuaJ contains a Lua to bytecode compiler. LuaJ包含一个Lua到字节码编译器。 So you can just look at the source code. 因此,您只需要看一下源代码即可。 I have extracted the most relevant portion here. 我在这里提取了最相关的部分。

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();
    }
}

Keep in mind that you can only really rely on byte code being compatible with the implementation of Lua that produced it. 请记住,您只能真正依赖与产生它的Lua实现兼容的字节码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM