在Java LuaJ库,我想知道如何要求或通过Java中的LUA关闭另一个名为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();
}
Run Code Online (Sandbox Code Playgroud)
这里的输入流是指另一个lua的内容:
import 'mycoolmathfunctions'
-- or maybe require mycoolmathfunctions ?
return sum({1, 2, 3})
-- or maybe mycoolmathfunctions.sum({1, 2, 3}) ?
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
Jas*_*arc 12
在Java LuaJ库中,我想知道如何通过Java在lua闭包调用的另一个lua脚本中要求或导入函数的lua脚本.
您可以将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
Run Code Online (Sandbox Code Playgroud)
然后导入我们的sample-that-imports.lua:
require "com.example.import-me"
my_imported.printHello()
Run Code Online (Sandbox Code Playgroud)
然后我们sample-that-imports.lua在SampleMainJava类中运行:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
现在回答你的其他问题,
例如,这不起作用......
我已经注意到你的Java代码,你已经假设调用loadfile()会自动运行你的lua脚本.此外,您假设loadfile()用于加载您的lua模块.但是,这不应该如何使用.
你loadfile()应该能够返回LuaValue,你需要call()运行该脚本本身.您甚至可以安全地将其转换为a,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();
}
Run Code Online (Sandbox Code Playgroud)
我将在上面的代码中假设您已经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();
}
Run Code Online (Sandbox Code Playgroud)
在上面的更改中,我假设您的lua模块(在我们的示例中import-me.lua)自动为自己创建一个全局空间(在我们的示例中,my_imported表).如果没有,你可以做最后的接触:
...
LuaValue chunk = globals.load("my_imported = require 'com.example.import-me';");
...
Run Code Online (Sandbox Code Playgroud)
除非你真的想在每次调用方法时创建一个新表,否则你
也应该重用你的Globals(返回JsePlatform.standardGlobals())Globals.此外,如果您真的不需要InputStream,只是想从文件路径(或Java包路径中的资源路径)加载文件本身,您可以将所有内容简化为:
public static LuaValue runLuaFile(Globals globals, String luafile) {
return globals.loadfile(luafile).call();
}
Run Code Online (Sandbox Code Playgroud)
或者为了确保我们的lua模块总是require通过我们的lua脚本导入(或者已经是'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();
}
Run Code Online (Sandbox Code Playgroud)
同样,您必须为我们的lua文件指定完整的资源路径.以下是使用上述简化方法的示例Java代码段:
Globals globals = JsePlatform.standardGlobals();
runLuaFile(globals, "com/example/sample-that-imports.lua");
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助!
您在评论中提到需要从InputStreams 导入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);
}
Run Code Online (Sandbox Code Playgroud)
上述实用方法预加载InputStream要使用的require.这是一个示例用法:
在一切开始的某个地方,我们初始化东西:
...
preloadLuaModule(globals, "sample_module", sampleModuleInputStream);
...
Run Code Online (Sandbox Code Playgroud)
而我们的sampleModuleInputStream上面是一个具有以下内容的LUA模块:
-- make our sample module table global
sample_module = {}
function sample_module.printHi()
print "Hi!"
end
return sample_module
Run Code Online (Sandbox Code Playgroud)
然后我们可以简单地使用require "sample_module"我们喜欢的任何地方,无论是在Lua脚本中还是在使用LuaJ的Java中:
globals.get("require").call("sample_module");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2727 次 |
| 最近记录: |