# HG changeset patch # User jbe # Date 1424608968 -3600 # Node ID db106eb273ea37d1934c2358b18fa1a9e52815e3 # Parent 15c9de7832cc6d11dd8b2c3fb1dfb4c99a842be0 Simplified autoloader in mcp.lua diff -r 15c9de7832cc -r db106eb273ea framework/bin/mcp.lua --- a/framework/bin/mcp.lua Sun Feb 22 13:03:34 2015 +0100 +++ b/framework/bin/mcp.lua Sun Feb 22 13:42:48 2015 +0100 @@ -80,9 +80,9 @@ local autoloader_category = setmetatable({}, weakkey_mt) local autoloader_path = setmetatable({}, weakkey_mt) local autoloader_mt = {} - local function install_autoloader(self, category, path) + local function install_autoloader(self, category, path_fragment) autoloader_category[self] = category - autoloader_path[self] = path + autoloader_path[self] = path_fragment setmetatable(self, autoloader_mt) end local function try_exec(filename) @@ -101,13 +101,6 @@ return false end end - local function compose_path_string(base, path, key) - if #path == 0 then - return base .. key - else - return base .. table.concat(path, "/") .. "/" .. key - end - end function autoloader_mt.__index(self, key) local category, base_path, merge_base_path, file_key local merge = false @@ -139,39 +132,34 @@ end local required_category = autoloader_category[self] if required_category and required_category ~= category then return end - local path = autoloader_path[self] - local path_string = compose_path_string(base_path, path, file_key) - local merge_path_string + local path_fragment = autoloader_path[self] + local path = base_path .. path_fragment .. file_key + local merge_path if merge then - merge_path_string = compose_path_string( - merge_base_path, path, file_key - ) + merge_path = merge_base_path .. path_fragment .. file_key end local function try_dir(dirname) local dir = io.open(dirname) if dir then io.close(dir) local obj = {} - local sub_path = {} - for i = 1, #path do sub_path[i] = path[i] end - sub_path[#path+1] = file_key - install_autoloader(obj, category, sub_path) + install_autoloader(obj, category, path_fragment .. file_key .. "/") rawset(self, key, obj) - try_exec(path_string .. "/__init.lua") - if merge then try_exec(merge_path_string .. "/__init.lua") end + try_exec(path .. "/__init.lua") + if merge then try_exec(merge_path .. "/__init.lua") end return true else return false end end - if merge and try_exec(merge_path_string .. ".lua") then - elseif merge and try_dir(merge_path_string .. "/") then - elseif try_exec(path_string .. ".lua") then - elseif try_dir(path_string .. "/") then + if merge and try_exec(merge_path .. ".lua") then + elseif merge and try_dir(merge_path .. "/") then + elseif try_exec(path .. ".lua") then + elseif try_dir(path .. "/") then else end return rawget(self, key) end - install_autoloader(_G, nil, {}) + install_autoloader(_G, nil, "") try_exec(WEBMCP_FRAMEWORK_PATH .. "env/__init.lua") try_exec(WEBMCP_BASE_PATH .. "env/__init.lua") end