- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 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
它们相对于您的包路径。
这是一个例子:
这是我们的 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();
}
我将在上面的代码中假设您已经在使用 require
在InputStream
(包含一个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 模块秒。有两种方法可以实现:
require
兼容。机制,你将面临很多问题。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);
}
上面的实用方法预加载了一个 InputStream
由 require
使用.这是一个示例用法:
在一切开始的某个地方,我们初始化东西:
...
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/
如何从 a.lua 传递值至 b.lua ? 让我们说在我的 a.lua我有这个变量代码。 local value = "Hello WOrld!" director:changeScene ("b"
我有一个使用命令行解释器运行的 lua 脚本,该脚本需要从文件加载表。 该表存储如下: create_object_action = { reflexive = true, which
我通过静态链接的方式在我的项目中嵌入了 Win32 上的 Lua(不,我不能切换到 DLL)。我想捆绑更多使用 native 代码的 Lua 扩展 - 而不仅仅是纯 .lua 文件。具体来说,我想捆绑
我需要一些帮助来解析 lua 文件的命令行。我正在执行一个 lua 文件,该 lua 文件有一个命令“dofile(2nd.lua-file)”,但是,我想通过第一个 lua 文件将一些参数传递给第二
这是我的代码示例: listOfPeople = {} listOfPeople["test"] = "hello" listOfPeople = nil “hello”字符串是否丢失并形成内存泄漏?
在一些源代码中,我看到了“Underscore.lua”模块的用法。 _ = require 'underscore' 描述如下: Underscore.lua is a Lua library th
在一些源代码中,我看到了“Underscore.lua”模块的用法。 _ = require 'underscore' 描述如下: Underscore.lua is a Lua library th
我一直在编程 io.write("How many languages do you speak?\n") answer = io.read() if (answer == 1) then io.wr
这个问题在这里已经有了答案: Getting multiple values from a function without creating a variables in LUA (2 个答案)
在阅读 Lua manual 时我遇到了这部分: 函数调用和赋值都可以以左括号开头。这种可能性导致了 Lua 语法中的歧义。考虑以下片段: a = b + c (print or io.write)(
假设我有以下循环: for name in poll() do if name == "quit" then return 0 end end "quit" 字符串是否
Pandoc 通过其 --lua-filter 参数原生支持 lua 过滤器。 但是,我想知道它使用的是什么版本的 lua,以及是否可以将 lua 模块(我相信它依赖于 C 代码)导入其中。 这是我调
这种语言是面向对象的语言吗? 它经常用作OO语言吗? 最佳答案 Lua 完全有能力 prototype-based类似于 JavaScript 的面向对象编程。 Prototype-based pro
我想从 C++ 传递一个 Lua 脚本(Lua 解释器可以处理的代码)并取回结果。 我在网上查看,但找不到任何可以帮助我的示例。我可以从 C++ 调用 Lua 函数,但这需要您使用 Lua 函数创建一
我正在阅读“在 Lua 中编程”,但我不明白这段代码中 Lua 中函数的行为: function newCounter () local i = 0 return function () --
我最近一直在查找 Lua 中的链表,并有一个简单的问题,到目前为止我还没有找到答案 local head = nil head = {next = head, value = "d"} head =
我知道有tonumber()函数,但是问题是我需要转换较大的数字,例如1000100110100011111010101001001001001100100101。我可以自己写,但是有没有办法将其集成
是否可以在 Lua 中对多个值执行算术运算。 我在 Windows 5.1.4 上使用 Lua。 目前我必须将多个值放入一个表中,然后解压缩它们,我希望能够跳过这一步。 是否可以。 这是我目前拥有的:
有什么区别吗 local splitPathFileExtension = function (res) end 和 function splitPathFileExtension(res) end
在下面的代码中,谁能解释一下 b,a = a,b 内部是如何工作的? -- Variable definition: local a, b -- Initialization a = 10 b = 3
我是一名优秀的程序员,十分优秀!