gpt4 book ai didi

java - Luaj/Java : org. luaj.vm2.LuaError:加载模块循环或上一个错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:55 26 4
gpt4 key购买 nike

我正在学习 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()));
}
}
}

以及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)

此错误意味着什么以及如何修复它?

最佳答案

And the in hyberbolic.lua (sic, should be hyperbolic)

require 'hyperbolic'

您需要一个与发生 require 的文件同名的模块,这会导致循环(这就是错误消息的含义)。只需重命名当前文件 (hyperbolic.lua),错误就会消失。

关于java - Luaj/Java : org. luaj.vm2.LuaError:加载模块循环或上一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28098273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com