[LuCI - Lua] Controller Order loading

Hi,
I have a question on LUA/LuCI. I want to add mymodule at the end of index table in the function createindex.

In this code:

local controllers = { }
	local base = "%s/controller/" % util.libpath()
	local _, path

	for path in (fs.glob("%s*.lua" % base) or function() end) do
		controllers[#controllers+1] = path
	end

	for path in (fs.glob("%s*/*.lua" % base) or function() end) do
		controllers[#controllers+1] = path
	end

It looks in these directories lua files and populates controller table, then in this code:

index = {}

	for _, path in ipairs(controllers) do
		local modname = "luci.controller." .. path:sub(#base+1, #path-4):gsub("/", ".")
		local mod = require(modname)
		assert(mod ~= true,
		       "Invalid controller file found\n" ..
		       "The file '" .. path .. "' contains an invalid module line.\n" ..
		       "Please verify whether the module name is set to '" .. modname ..
		       "' - It must correspond to the file path!")

		local idx = mod.index
		assert(type(idx) == "function",
		       "Invalid controller file found\n" ..
		       "The file '" .. path .. "' contains no index() function.\n" ..
		       "Please make sure that the controller contains a valid " ..
		       "index function and verify the spelling!")

		index[modname] = idx
	end

It requires the modules in right order (if I print modname inside the loop):

luci.controller.firewall
luci.controller.opkg
luci.controller.admin.filebrowser
luci.controller.admin.index
luci.controller.admin.network
luci.controller.admin.status
luci.controller.admin.system
luci.controller.admin.uci
luci.controller.myapp.mymodule

but it sets index table using strings for keys (eg. "luci.controller.admin.index").

THE PROBLEM
If I print index table in the loop this is the result:

1: {"luci.controller.firewall":null}
2: {"luci.controller.firewall":null,"luci.controller.opkg":null}
3: {"luci.controller.firewall":null,"luci.controller.admin.filebrowser":null,"luci.controller.opkg":null}
4: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.admin.filebrowser":null,"luci.controller.opkg":null}
5: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.opkg":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.network":null}
6: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.opkg":null,"luci.controller.admin.status":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.network":null}
7: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.opkg":null,"luci.controller.admin.status":null,"luci.controller.admin.system":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.network":null}
8: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.admin.uci":null,"luci.controller.opkg":null,"luci.controller.admin.status":null,"luci.controller.admin.system":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.network":null}
9: {"luci.controller.admin.uci":null,"luci.controller.admin.system":null,"luci.controller.myapp.mymodule":null,"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.admin.network":null,"luci.controller.opkg":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.status":null}

Why?? Why does it do this?

I want to insert mymodule at the last position, but I wouldn't change dispatcher.lua (I expect that it maintains the insert order)

** * UPDATE * **
I discovered that if I change the name at mymodule and move it in right folder (eg. luci.controller.admin.test or luci.controller.a.a), my module will be uploaded last.

It's a escamotage, but I would like to know what's the criteria that Lua uses

1 Like