How to organize Lua code when 'require' not allowed?

Post Reply
User avatar
xeranas
Member
Posts: 162
Joined: Fri Feb 05, 2016 11:06

How to organize Lua code when 'require' not allowed?

by xeranas » Post

It seems that by default when I use 'require' I get:

Code: Select all

ERROR[Main]: require() is disabled when mod security is on.
That basically blocker for moding with require. Asking to disable security is simple not solution.

My goal is separate functionality into separate modules and do not expose too much into global scope.
In my case "get_modpath" is useless as it does not allow invoke local functions.

Code: Select all

dofile(minetest.get_modpath("modname") .. "/luaB.lua")
How minetest moders deal with this problem?

sfan5
Moderator
Posts: 4095
Joined: Wed Aug 24, 2011 09:44
GitHub: sfan5
IRC: sfan5
Location: Germany

Re: How to organize Lua code when 'require' not allowed?

by sfan5 » Post

Mods: Mesecons | WorldEdit | Nuke & Minetest builds for Windows (32-bit & 64-bit)

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: How to organize Lua code when 'require' not allowed?

by rubenwardy » Post

and also tables to expose a local only API
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
Linuxdirk
Member
Posts: 3219
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: How to organize Lua code when 'require' not allowed?

by Linuxdirk » Post

xeranas wrote:My goal is separate functionality into separate modules and do not expose too much into global scope.
init.lua

Code: Select all

mymodname = {}

dofile(minetest.get_modpath("modname") .. "/luaB.lua")

mymodname.foo()
luaB.lua

Code: Select all

mymodname.foo = function ()
    -- do something here
end
Now all you expose to the global scope is a table named after your mod.

User avatar
xeranas
Member
Posts: 162
Joined: Fri Feb 05, 2016 11:06

Re: How to organize Lua code when 'require' not allowed?

by xeranas » Post

Linuxdirk wrote:
xeranas wrote:My goal is separate functionality into separate modules and do not expose too much into global scope.
init.lua

Code: Select all

mymodname = {}

dofile(minetest.get_modpath("modname") .. "/luaB.lua")

mymodname.foo()
luaB.lua

Code: Select all

mymodname.foo = function ()
    -- do something here
end
Now all you expose to the global scope is a table named after your mod.
"mymodname.foo" becomes public which I do want to avoid.
rubenwardy wrote:and also tables to expose a local only API
no point of calling it 'local' if it can be invoked from any other mod.
Last edited by xeranas on Wed Oct 25, 2017 16:04, edited 1 time in total.

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: How to organize Lua code when 'require' not allowed?

by rubenwardy » Post

This is what sfan5 and I were referring to

Code: Select all

local foo = {}

function foo.bar()

end

return foo
then in caller:

Code: Select all

local foo = dofile(...)

foo.bar()
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
xeranas
Member
Posts: 162
Joined: Fri Feb 05, 2016 11:06

Re: How to organize Lua code when 'require' not allowed?

by xeranas » Post

rubenwardy, thanks seems dotfile does the job.

User avatar
Linuxdirk
Member
Posts: 3219
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: How to organize Lua code when 'require' not allowed?

by Linuxdirk » Post

xeranas wrote:"mymodname.foo" becomes public which I do want to avoid.
Yes, that's unfortunate, but Minetest does not have mod namespaces and thus anything that isn't local will be global. This can't be fixed until one of the devs starts caring about mod namespaces.

But with this approach at least you have only one thing (the table) in the global namespace and you can place anything that should be used in several places of your mod there withuot polluting the global namespace even more.

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: How to organize Lua code when 'require' not allowed?

by rubenwardy » Post

Linuxdirk wrote:
xeranas wrote:"mymodname.foo" becomes public which I do want to avoid.
This can't be fixed until one of the devs starts caring about mod namespaces.
We simply care more about mod backwards compatibility than mod namespacing. The engine at least warns if you write to a new global that isn't the name of the mod.

Also, this can be fixed by the mod. See my other response
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 3 guests