我正在学习Luaj库,我正在尝试在单元测试中实现双曲线示例:
@Test
public void testHyperbolicLuaScriptExample() throws Exception {
URL luaScriptUrl = Thread.currentThread().getContextClassLoader().getResource("hyperbolic.lua");
Assert.assertNotNull(luaScriptUrl);
String luaScriptUrlPath = luaScriptUrl.getPath();
File luaScriptFile = new File(luaScriptUrlPath);
FileInputStream luaScriptFileInputStream = new FileInputStream(luaScriptFile);
Prototype luaScriptPrototype = LuaC.instance.compile(luaScriptFileInputStream, "");
Globals luaScriptStandardGlobals = JsePlatform.standardGlobals();
LuaClosure luaClosure = new LuaClosure(luaScriptPrototype, luaScriptStandardGlobals);
LuaValue luaValue = luaClosure.call();
}
hyperbolic.java
按照the example
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.*;
public class hyperbolic extends TwoArgFunction {
public hyperbolic() {}
public LuaValue call(LuaValue moduleName, LuaValue environment) {
LuaValue library = tableOf();
library.set("sinh", new sinh());
library.set("cosh", new cosh());
environment.set("com.apple.aide.lua.hyperbolic", library);
return library;
}
static class sinh extends OneArgFunction {
public LuaValue call(LuaValue x) {
return LuaValue.valueOf(Math.sinh(x.checkdouble()));
}
}
static class cosh extends OneArgFunction {
public LuaValue call(LuaValue x) {
return LuaValue.valueOf(Math.cosh(x.checkdouble()));
}
}
}
in hyberbolic.lua
require 'hyperbolic'
return {"x", hyperbolic.sinh(0.5), "y", hyperbolic.cosh(0.5)}
但是测试会产生以下错误
org.luaj.vm2.LuaError: @hyperbolic.lua:3 loop or previous error loading module 'hyperbolic'
at org.luaj.vm2.LuaValue.error(Unknown Source)
at org.luaj.vm2.lib.PackageLib$require.call(Unknown Source)
at org.luaj.vm2.LuaClosure.execute(Unknown Source)
at org.luaj.vm2.LuaClosure.call(Unknown Source)
at org.luaj.vm2.lib.PackageLib$require.call(Unknown Source)
at org.luaj.vm2.LuaClosure.execute(Unknown Source)
at org.luaj.vm2.LuaClosure.call(Unknown Source)
at com.example.LuaScriptExecutionTest.testHyperbolicLuaScriptExample(LuaScriptExecutionTest.java:52)
这个错误是什么意思,我该如何解决?
答案 0 :(得分:1)
in hyberbolic.lua(原文如此,应该是双曲线的)
require 'hyperbolic'
您需要一个与require
发生的文件同名的模块,这会导致循环(这就是错误消息的内容)。只需重命名当前文件(hyperbolic.lua
),错误就会消失。