gudzpoz / luajava

Lua for Java on Windows, Mac OS X, Linux, Android. 5.1, 5.2, 5.3, 5.4, LuaJ or LuaJIT.
https://gudzpoz.github.io/luajava/
Other
122 stars 14 forks source link

Require function not available #157

Closed jsorel closed 5 months ago

jsorel commented 5 months ago

Describe the bug I am working on IHO S-100 specifications, in those specifications, LUA 5.1 is the scripting language for all rendering processes. there are multiple lua files (about 200) which are loaded using the require function. Some are loaded at the begining of the file like this require 'S100Scripting' and some later on with require(feature.Code).

No lua module() are used, nowhere.

I made a small test case: To Reproduce Place the following files in the resources of the project : Friend2.lua

require 'Friend3'

function tchat()
    return 'Hello ' .. anotherHello()
end

Friend3.lua

function anotherHello()
    return 'you'
end

And in java execute :

try (final Lua lua = new Lua51()) {
    lua.setExternalLoader(new ClassPathLoader());
    Lua.LuaError error = lua.loadExternal("Friend2"); //OK
    LuaValue fct = lua.get(); //the loaded chunk as a function
    LuaValue[] result = fct.call(); //null -> so there is an error
    System.out.println(lua.status()); // OK ???
    System.out.println(lua.get().toJavaObject()); // [string "Friend2"]:1: attempt to call global 'require' (a nil value)
}

Current behavior I could not find the 'require' function, even with local require = require.

Expected behavior Is require usable in LuaJava ? Must I implement it ? how ?

Platform:

Additional context Add any other context about the problem here.

AMCON-Chenet commented 5 months ago

You are missing an 'import' of the default lua libraries. Try calling this before setting the externalLoader.

lua.openLibraries();
jsorel commented 5 months ago

I knew something wasn't right, I couldn't call any of the string functions either.

Many thanks,