LuaJ:无法在Lua脚本中调用"require"功能 [英] LuaJ: Unable to call 'require' function in Lua script

查看:280
本文介绍了LuaJ:无法在Lua脚本中调用"require"功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所做的事情很奇怪,很可能导致此错误.

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

以下简单示例失败:

--> thingy.lua
function doThing()
  print( "Thing has been done." );
end

--> test.lua
require( "thingy" );

执行thingy.lua时,没有问题.执行test.lua时,出现以下错误:

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

script:2 module 'thingy' not found: thingy
no field package.preload['thingy']
thingy.lua
no class 'thingy'

这两个文件都存在于同一目录中,并且我可以使用SciTE(正在运行Lua 5.1)运行两个脚本而没有错误.这似乎是路径问题,因此我尝试将 package.path 设置为源文件的绝对路径.

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.

注意:我设置路径,而不是附加,以便可以确定由于现有的相对路径?.lua",SciTE没有成功

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".

我在LauJ(使用我自己的程序)和SciTE中进行了测试,发现SciTE能够执行test.lua,而LuaJ仍然无法执行,并产生与以往相同的错误.

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.

在Java代码中我是否应该做(或不做)可能引起这种情况的事情?我已经成功地从Lua脚本访问Java,但不是 other Lua脚本.只要手动运行包含它们的脚本,我就可以在LuaJ中访问全局变量和函数.

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.

为了很好,这是我用来执行脚本的Java代码.

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

// some fancy Java code
public void execute() throws ScriptException, LuaError
{
    try
    {
        FileReader reader = new FileReader( filename );
        Script_Engine.eval( reader );
        reader.close();
    }
    catch( FileNotFoundException fnfe )
    {
        fnfe.printStackTrace();
    }
    catch( IOException ioe )
    {
        ioe.printStackTrace();
    }
}

public void callFunction( String functionName, Object[] args ) throws Exception
{
    File scriptFile = new File( filename );
    FileReader reader = new FileReader( scriptFile );

    CompiledScript script = ((Compilable)Script_Engine).compile( reader );
    script.eval( Script_Bindings );

    LuaFunction lua_function = (LuaFunction)Script_Bindings.get( functionName );
    LuaValue[] vals = new LuaValue[args.length];
    for( int i = 0; i < args.length; i++ )
    {
        vals[i] = CoerceJavaToLua.coerce( args[i] );
    }
    lua_function.invoke( vals );
    reader.close();
}

两个函数中使用的'filename'变量是在外壳类的构造函数中创建的.

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

更新: 我发现,无论问题是什么,它都存在于LuaJ 3.0版中(我正在使用JSE包).用较旧的2.03 JAR替换了3.0-alpha2 JAR文件后,问题不再存在.我对现在可以继续使用旧版本的LuaJ感到满意,但我仍然希望使用最新版本.

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.

在LuaJ自述文件中此处中发现了以下内容:

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

调用require()时,它将首先尝试将模块加载为实现LuaFunction的Java类.

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
通过require()加载时,将环境作为LibFunction的第二个参数

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

我强烈怀疑这与它有关,因为它是在3.0-alpha2版本中添加的,所以我下载了3.0-alpha1版本(正在使用3.0-alpha2),希望它能正常工作,但没有成功.

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.

推荐答案

与LuaJ的创建者进行了一些交谈之后,我们确定问题出在从3.0-alpha1版本开始的更改中,该版本在lua的程序包中进行了更改通过 require 加载脚本时,.path 被忽略.这意味着require仅会在路径"中查找.搜索脚本时.如果有从子目录中调用的脚本,则将其命名为"place",然后 require 可以通过使用点运算符加载这些脚本来找到这些脚本:

After some conversations with the creator of LuaJ, we determined that the problem came from a change that was made starting in version 3.0-alpha1 where lua's package.path was being ignored when loading a script via require. What this means is that require will only look in the path "." when searching for scripts. If there are scripts called from a sub-directory, call it "place" then require could find those scripts by loading them with the dot operator:

require( "place.thingy" );

我怀疑这个 package.path 问题的数量在社区中有点稀缺,是因为有一种方法可以设置从Java方面起作用的路径在LuaJ v3.0的早期版本中. (一旦我确定了正确的方法,我将发布更新的正确方法,因为我对该过程尚不清楚.)

The reason, I suspect, that the number of issues with this package.path issue have been somewhat scarce in the community is because there is a way to set the path from the Java side that works in previous releases of LuaJ v3.0. (I will post an update on the proper way to do this once I have figured it out, as I am still unclear on the process.)

总的来说,总之是应该在不久的将来就应该有一个LuaJ v3.0-alpha3,它可以从lua设置 package.path .

The long and short of the whole situation is that there should be a LuaJ v3.0-alpha3 on the way soon which allows the package.path to be set from lua.

再次感谢吉姆·罗斯伯勒(Jim Roseborough)与我一起解决问题.

Thanks again to Jim Roseborough for working with me to resolve the issue.

吉姆·罗斯伯勒(Jim Roseborough)的注释:正如内森(Nathan)所言,这确实是luaj-3.0-alpha2及之前版本中的错误.此问题已修复,可以在luaj-3.0-beta1及更高版本中按预期工作,现在可以使用.

Note from Jim Roseborough: As Nathan mentioned, this was indeed a bug in luaj-3.0-alpha2 and before. This has been fixed and should work as expected in luaj-3.0-beta1 and later which is now available.

这篇关于LuaJ:无法在Lua脚本中调用"require"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆