【问题标题】:Java method for compiling Lua code with the LuaJ interpreter用 LuaJ 解释器编译 Lua 代码的 Java 方法
【发布时间】:2013-03-16 14:16:37
【问题描述】:

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 字节码并输出字节码文件的代码。

【问题讨论】:

    标签: java lua luaj


    【解决方案1】:

    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 实现兼容的字节码。

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 2014-08-14
      相关资源
      最近更新 更多