java - Luaj:如何导入或需要一个 Lua 函数库

标签 java lua luaj

在 Java 中 LuaJ library我想知道如何在另一个由 lua 闭包通过 Java 调用的 lua 脚本中要求或导入函数的 lua 脚本。例如,这不起作用:

public static LuaValue runInputStreamLua(InputStream inputStream) throws Exception {
    Prototype luaScriptPrototype = LuaC.instance.compile(inputStream, "");
    Globals luaScriptStandardGlobals = JsePlatform.standardGlobals();
    luaScriptStandardGlobals.loadfile("mycoolmathfunctions.lua");
    LuaClosure luaClosure = new LuaClosure(luaScriptPrototype, luaScriptStandardGlobals);
    return luaClosure.call();
}

而这里的输入流是指另一个lua的内容:

import 'mycoolmathfunctions'
-- or maybe require mycoolmathfunctions ?

return sum({1, 2, 3})
-- or maybe mycoolmathfunctions.sum({1, 2, 3}) ?

我该怎么做?

最佳答案

In the Java LuaJ library I would like to know how to require or import a lua script of functions in another lua script called by a lua closure through Java.

您可以将您的 Lua 库作为资源放置在您的 Java 包中。然后在需要另一个 lua 脚本的 lua 脚本上,你 require它们相对于您的包路径。

这是一个例子:

enter image description here

这是我们的 import-me.lua :

-- make our sample module table global
my_imported = {}

function my_imported.printHello()
    print "Hello!"
end

return my_imported

然后在我们的 sample-that-imports.lua 中导入:

require "com.example.import-me"

my_imported.printHello()

然后我们运行我们的 sample-that-imports.lua在我们的 SampleMain Java类:

package com.example;
...
public class SampleMain {

    public static void main(String[] args) {
        Globals globals = JsePlatform.standardGlobals();

        // Again, we load the lua scripts relative to our package path
        LuaValue chunk = globals.loadfile("com/example/sample-that-imports.lua");
        chunk.call();

        // We could even use our imported library here
        chunk = globals.load("my_imported.printHello()");
        chunk.call();
    }
}

现在回答你的其他问题,

For example this does not work…

我注意到在您的 Java 代码中您假定调用 loadfile()会自动运行你的lua脚本。此外,您假设 loadfile()用于加载你的 lua 模块。但是,这不是它应该的使用方式。

你的 loadfile()应该能够返回 LuaValue你需要 call()运行脚本本身。您甚至可以安全地将其转换为 LuaClosure因为这就是loadfile()实际返回。

要修复上面的 Java 代码,您可以使用它,

public static LuaValue runInputStreamLua(InputStream inputStream) throws Exception {
    Prototype luaScriptPrototype = LuaC.instance.compile(inputStream, "");
    Globals globals = JsePlatform.standardGlobals();
    LuaClosure luaClosure = new LuaClosure(luaScriptPrototype, globals);
    return luaClosure.call();
}

我将在上面的代码中假设您已经在使用 requireInputStream (包含一个lua脚本)你用上面的方法传递的。如果没有,您可以进行以下更改:

public static LuaValue runInputStreamLua(InputStream inputStream) throws Exception {
    Prototype luaScriptPrototype = LuaC.instance.compile(inputStream, "");
    Globals globals = JsePlatform.standardGlobals();

    LuaValue chunk = globals.load("require 'com.example.import-me';");
    chunk.call();

    LuaClosure luaClosure = new LuaClosure(luaScriptPrototype, globals);
    return luaClosure.call();
}

在上述更改中,我假设您的 lua 模块(在我们的示例中为 import-me.lua)自动为自己创建一个全局空间(在我们的示例中为 my_imported 表)。如果没有,您可以做最后的润色:

...
LuaValue chunk = globals.load("my_imported = require 'com.example.import-me';");
...


您还应该重复使用您的 Globals (由 JsePlatform.standardGlobals() 返回)除非你真的想创建一个新的 Globals每次你调用你的方法时。此外,如果您真的不需要 InputStream ,并且只想从文件路径(或 Java 包路径中的资源路径)加载文件本身,您可以将所有内容简化为:

public static LuaValue runLuaFile(Globals globals, String luafile) {
    return globals.loadfile(luafile).call();
}

或者确保我们的 lua 模块总是被我们的 lua 脚本导入(或者已经被 require 'd),

public static LuaValue runLuaFile(Globals globals, String luafile) {
    LuaValue chunk = globals.load("require 'com.example.import-me';");
    chunk.call();
    chunk = globals.loadfile(luafile);
    return chunk.call();
}

同样,您必须为我们的 lua 文件指定完整的资源路径。下面是使用我们上面的简化方法的示例 Java 代码段:

Globals globals = JsePlatform.standardGlobals();
runLuaFile(globals, "com/example/sample-that-imports.lua");

希望对您有所帮助!


编辑:

您在评论中提到您需要从 InputStream 导入 lua 模块秒。有两种方法可以实现:

  1. 第一个是加载和运行你需要的 lua 模块,比如简单的 lua 脚本——如果你需要的 lua 模块只与 lua 的 require 兼容。机制,你将面临很多问题。
  2. 第二种最简单、最有效的方法是简单地加载模块,将它放在 lua 表中 package.preload ,以键作为其名称映射(由 require 使用)。

我们将使用上面的第二种方法,因为这正是 lua 的 require机制确实有意。以下是使用 LuaJ 实现它的方法:

public static void preloadLuaModule(Globals globals, String modname, InputStream is) {
    LuaValue module = globals.load(is, modname, "bt", globals);
    globals.get("package").get("preload").set(modname, module);
}

上面的实用方法预加载了一个 InputStreamrequire 使用.这是一个示例用法:

在一切开始的某个地方,我们初始化东西:

...
preloadLuaModule(globals, "sample_module", sampleModuleInputStream);
...

还有我们的sampleModuleInputStream上面是一个 lua 模块,内容如下:

-- make our sample module table global
sample_module = {}

function sample_module.printHi()
    print "Hi!"
end

return sample_module

然后我们可以简单地使用 require "sample_module"我们喜欢的任何地方,无论是在 Lua 脚本中还是在使用 LuaJ 的 Java 中:

globals.get("require").call("sample_module");

关于java - Luaj:如何导入或需要一个 Lua 函数库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597675/

相关文章:

java - SQLite Android 根据今天是哪一天获取数据库信息

java - 使用synchronizedList时输入安全警告

java - 多个类的扩展

java - 在这种情况下,JMS 主题是否足够?或者我应该去别处看看?

lua - 在Lua中模拟并行

lua - Lua 中的函数可以在调用之间存储本地值吗?

lua - 在 Lua Torch 中,两个零矩阵的乘积有 nan 项

android - 在 Android 上使用 LuaJ 从 Lua 脚本中请求其他 lua 脚本

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

java - 在Lua中打开数组或ArrayList(将数组转换为表)