问题标签 [luaj]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
2306 浏览

lua - LuaJ: Unable to call 'require' function in Lua script

There is a really good chance that I am doing something bizarre that is causing this error.

The following simple example fails:

and

When thingy.lua is executed, there are no problems. When test.lua is executed, I see the following error:

Both of these files exist in the same directory, and I can run both scripts with no error using SciTE (which is running Lua 5.1). It seems to be a path issue, so I tried setting package.path to be the absolute path to the source files.

Note: I set the path, rather than appending so that I could make certain that SciTE wasn't succeeding because of the existing relative path "?.lua".

I tested in both LauJ (using my own program) and in SciTE, finding that SciTE is able to execute test.lua, and LuaJ still unable, yielding the same error as always.

Is there something I should be doing (or not doing) in the Java code that might be capable of causing this? I have had success accessing Java from Lua scripts, just not other Lua scripts. I can access global variables and functions in LuaJ as long as I have manually run the scripts that contain them.

Just for good measure, here is the Java code I use to execute a script.

The 'filename' variable used in both functions is created in the constructor of the housing class.

Update: I have found that, whatever the problem is, it exists in LuaJ version 3.0 (I'm using the JSE package). Having replaced the 3.0-alpha2 JAR file with an older 2.03 JAR, the problem is no more. While I am satisfied that I can now move ahead with the older version of LuaJ, I would still prefer to be using the most updated version.

There is something in the LuaJ Readme found here that says the following:

When require() is called, it will first attempt to load the module as a Java class that implements LuaFunction.

and under the Release Notes section:

3.0-alpha2
Supply environment as second argument to LibFunction when loading via require()

I was strongly suspecting that it has something to do with this since it was added in version 3.0-alpha2, so I downloaded version 3.0-alpha1 (was using 3.0-alpha2), expecting it to work, but it did not.

0 投票
4 回答
3019 浏览

java - Lua / Java / LuaJ - 处理或中断无限循环和线程

我正在使用 LuaJ 在 Java 中运行用户创建的 Lua 脚本。但是,运行从不返回的 Lua 脚本会导致 Java 线程冻结。这也使线程不可中断。我运行 Lua 脚本:

badscript.lua包含while true do end.

我希望能够自动终止卡在不屈不挠的循环中的脚本,并允许用户在运行时手动终止他们的 Lua 脚本。我读过关于debug.sethookpcall的内容,但我不确定如何正确使用它们来达到我的目的。我还听说沙盒是一个更好的选择,尽管这有点超出我的能力范围。

这个问题也可以单独扩展到 Java 线程。我还没有找到任何关于中断卡在while (true);.

在线Lua 演示非常前途,但似乎“坏”脚本的检测和终止是在 CGI 脚本中完成的,而不是在 Lua 中完成的。我可以使用 Java 调用 CGI 脚本,然后再调用 Lua 脚本吗?不过,我不确定这是否会允许用户手动终止他们的脚本。我丢失了 Lua 演示源代码的链接,但我手头有它。这是魔术线:

有人可以指出我正确的方向吗?

一些资料来源:

0 投票
1 回答
470 浏览

android - NoClassDefFoundError 与 LuaJ

我正在尝试在 Android Studio (V0.2.3) 项目中使用 LuaJ。

我将其添加luaj-jme-3.0-beta.jar到 libs 文件夹中。

在“项目结构”库下,我添加了项目库luaj-jme-3.0-beta.jar

在“依赖项”下的 build.gradle 文件中,我添加了这一行:compile files('libs/luaj-jme-3.0-beta1.jar')

在我的 MainActivty 中,我调用 luaj:

编译成功,然后将apk文件上传到目标设备后,我收到以下消息:

我使用 apkananyser.jar 来查看 apk 文件。看起来 lua 库不包含在 apk 文件中。

我是 gradle 新手,那么如何将 lua lib 添加到 apk 文件中?我已经在谷歌上搜索了两天,但我找不到解决方案。

我该如何解决这个错误?

0 投票
1 回答
464 浏览

java - 如何通过反射 luaj 传递类 - java

我使用 LuaJ,它包含一个库:luajava,它允许通过反射将一个类传递给 lua 环境。你知道怎么做吗?我注意到有一种方法可以传递编译类:

未编译的类:*.java 怎么样?如何通过它?

0 投票
2 回答
1290 浏览

scala - 在 Scala 中使用 LuaJ

我正在尝试将 LuaJ 与 Scala 一起使用。大多数事情都可以工作(实际上,如果你做对了,所有事情都可以工作!)但是由于 Scala 的 setter 实现,设置对象值的简单任务变得异常复杂。

斯卡拉:

卢阿:

如果我执行包含此 Lua 函数的脚本或行并将 TestObject 的强制实例传递给 myTestFunction,这会导致 LuaJ 中的错误。LuaJ 正在尝试直接写入该值,而 Scala 要求您通过隐式定义的 setter(使用可怕的名称 x_=,这不是有效的 Lua,因此即使尝试将其作为函数调用也会使您的 Lua 无法解析) .

正如我所说,有一些解决方法,例如定义您自己的设置器或使用@BeanProperty 标记。他们只是使应该易于编写的代码变得更加复杂:

卢阿:

有谁知道让 luaj 隐式调用 setter 进行此类分配的方法?或者我可能会在 luaj 源代码中查看以实现这样的事情?

谢谢!

0 投票
1 回答
648 浏览

java - LuaJ 在 Java 中添加 lua 函数

我知道这种方法:

如何向 _G 添加使用 Luaj 运行 java 代码的函数?

但是该链接允许您添加诸如math.abs(...). 我只想添加没有库或必须使用的功能require,您可以直接使用myfunction()

我该怎么做呢?

0 投票
2 回答
447 浏览

lua - 在 luajava 中强制使用整数类型

有没有办法使用 LuaJava 强制整数类型?我正在尝试在 LuaJ 3.0 beta 1(luaj-jse-3.0-beta1.jar)中创建一个java.awt.Color. 考虑到构造函数 ( JavaAPI )的 Java API 可用性,您不会认为这是一个问题。我对将整数强制放入构造函数感兴趣的原因是Color可用的构造函数采用intor的三个和四个参数float。由于 Lua 的数字类型是double,因此 LuaJ(或 LuaJava)调用float构造函数的版本。

起初,我认为这不会是一个问题,但在尝试使用它后,我注意到我收到了一个错误的Color. 这些值没有被正确地传递给构造函数,并且Color对象基本上被破坏了,没有明显的错误(它没有崩溃任何东西,只是没有显示)。

这是一个简单的例子:

如果我将r = 1.0, g = 0.2,传递b = 0.2给这个构造函数,然后 print j_color,我会看到

在输出中。事实上,我可以使用任何我想要的东西rg只要b = 0,没有问题,但否则,它会导致痛苦和痛苦。

我有点好奇是否有其他人遇到过这个问题。如果不能解决,我会联系 LuaJ 项目的 PI 并报告。

附带说明:构建一个解决方案是一件简单的事情,我使用定制的 Java 函数来负责创建Color. 我更担心这个问题的存在,以及是否有某种方法可以指定要使用的构造函数,或者明确告诉 LuaJava 使用整数,而不是双精度数。

更新

我也尝试使用四浮点构造函数,并且出现了同样的问题。如果我传入以下值之一:alpha=0, 0.5, 1构造函数正常运行。其他任何事情都会产生上述结果。

0 投票
1 回答
1183 浏览

java - LuaJ 在 java 中需要 json 模块

我试图在 Java 代码中加载 json 模块。文档中的示例显示它应该像这样,但它不起作用。它要求模块在项目中还是从 LUA_PATH 加载它。有没有人有这个问题的经验?

线程“主”org.luaj.vm2.LuaError 中的异常:找不到模块“json”:json 没有字段 package.preload['json'] json.lua 没有类“json”

看起来它只是在 LuaJ 包中尝试。

0 投票
2 回答
1077 浏览

lua - LuaJ loading two functions with the same name from two different LuaScripts

I have two Lua Scripts containing functions with the same name:

luaScriptA:

luaScriptB:

I would like to load both these functions using LuaJ into the globals environnment, for one script I usually do it as follows:

This will load the function init() into globals and I can execute this function from java with:

The problem comes in when I load a second script, this will overwrite all functions with the same name previously declared. Is there any way I can prevent this and easily distinguish between the two functions? For example something like:

Please note that the script contains other functions as well and my goal is to run individual functions within the script, not the complete script at once.Working on the JME platform.

0 投票
1 回答
2389 浏览

java - Luaj 尝试索引?(一个函数值)

我正在尝试编译具有两个要调用并从中获取一些信息的函数的 Lua 代码,但是当我在 LuaValue 对象上使用 invokemethod 时,出现此错误

LuaError:尝试索引?(一个函数值)

代码位于我为方便而创建的 LuaScript 类中

首先调用该方法编译文件

然后这用于getSameTiles从我的 lua 脚本中调用该函数

错误LuaError: attempt to index ? (a function value)发生在return compiledcode.invokemethod(func);作为"getSameTiles"字符串传递的行func

这是我的 Lua 代码