dimanche 3 juillet 2022

How can a function get the module name of the caller?

Say I have two lua modules, like this.


Filename: my/lua/app/caller.lua
Module name: my.lua.app.caller

local reflect = require('my.lib.reflect')

-- should return "my.lua.app.caller"
reflect.get_caller_module_name()

Filename: my/lib/reflect.lua
Module name: my.lib.reflect

M = {}

function M.get_caller_module_name()
   -- magic here?
   return module_name
end

return M

Now running my.lua.app.caller should allow it to get its own module name, my.lua.app.caller. Is this doable in Lua?

(I realize that I can get the module name by inserting local module_name, _ = ... as the very first line of the caller's module, but I want to be able to do this without this without needing any special modifications to the caller's module.)





Aucun commentaire:

Enregistrer un commentaire