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

Custom modules #165

Closed Trivaxy closed 3 months ago

Trivaxy commented 3 months ago

I'm working on integrating LuaJava to one of my projects. To keep things clean, a lot of the functionality implemented on the Java side is (meant to be) exposed to the Lua side in the form of custom modules, e.g

local geom = require("geometry")
local world = require("world")
...

I've seen setExternalLoader and loadExternal, but those don't seem to be what I need as these modules are entirely implemented on the Java side. Any advice?

gudzpoz commented 3 months ago

You may use Java method modules for that.

Trivaxy commented 3 months ago

I see. Is it possible to alias the module name to something simpler for the users? i.e require("world") instead of require("xyz.trivaxy.modules.MyModule.open")

gudzpoz commented 3 months ago

LuaJava does not provide a builtin way to do this. But you can of course rewrite the require function as you wish:

local oldRequire = require
local mapping = {
    shortname = "xyz.trivaxy.modules.MyModule.open",
}
function require(name)
    return oldRequire(mapping[name] or name)
end
Trivaxy commented 3 months ago

Yeah, that's a good way to do it. Thanks.