webmcp
changeset 219:db106eb273ea
Simplified autoloader in mcp.lua
author | jbe |
---|---|
date | Sun Feb 22 13:42:48 2015 +0100 (2015-02-22) |
parents | 15c9de7832cc |
children | e69251d4ba0e |
files | framework/bin/mcp.lua |
line diff
1.1 --- a/framework/bin/mcp.lua Sun Feb 22 13:03:34 2015 +0100 1.2 +++ b/framework/bin/mcp.lua Sun Feb 22 13:42:48 2015 +0100 1.3 @@ -80,9 +80,9 @@ 1.4 local autoloader_category = setmetatable({}, weakkey_mt) 1.5 local autoloader_path = setmetatable({}, weakkey_mt) 1.6 local autoloader_mt = {} 1.7 - local function install_autoloader(self, category, path) 1.8 + local function install_autoloader(self, category, path_fragment) 1.9 autoloader_category[self] = category 1.10 - autoloader_path[self] = path 1.11 + autoloader_path[self] = path_fragment 1.12 setmetatable(self, autoloader_mt) 1.13 end 1.14 local function try_exec(filename) 1.15 @@ -101,13 +101,6 @@ 1.16 return false 1.17 end 1.18 end 1.19 - local function compose_path_string(base, path, key) 1.20 - if #path == 0 then 1.21 - return base .. key 1.22 - else 1.23 - return base .. table.concat(path, "/") .. "/" .. key 1.24 - end 1.25 - end 1.26 function autoloader_mt.__index(self, key) 1.27 local category, base_path, merge_base_path, file_key 1.28 local merge = false 1.29 @@ -139,39 +132,34 @@ 1.30 end 1.31 local required_category = autoloader_category[self] 1.32 if required_category and required_category ~= category then return end 1.33 - local path = autoloader_path[self] 1.34 - local path_string = compose_path_string(base_path, path, file_key) 1.35 - local merge_path_string 1.36 + local path_fragment = autoloader_path[self] 1.37 + local path = base_path .. path_fragment .. file_key 1.38 + local merge_path 1.39 if merge then 1.40 - merge_path_string = compose_path_string( 1.41 - merge_base_path, path, file_key 1.42 - ) 1.43 + merge_path = merge_base_path .. path_fragment .. file_key 1.44 end 1.45 local function try_dir(dirname) 1.46 local dir = io.open(dirname) 1.47 if dir then 1.48 io.close(dir) 1.49 local obj = {} 1.50 - local sub_path = {} 1.51 - for i = 1, #path do sub_path[i] = path[i] end 1.52 - sub_path[#path+1] = file_key 1.53 - install_autoloader(obj, category, sub_path) 1.54 + install_autoloader(obj, category, path_fragment .. file_key .. "/") 1.55 rawset(self, key, obj) 1.56 - try_exec(path_string .. "/__init.lua") 1.57 - if merge then try_exec(merge_path_string .. "/__init.lua") end 1.58 + try_exec(path .. "/__init.lua") 1.59 + if merge then try_exec(merge_path .. "/__init.lua") end 1.60 return true 1.61 else 1.62 return false 1.63 end 1.64 end 1.65 - if merge and try_exec(merge_path_string .. ".lua") then 1.66 - elseif merge and try_dir(merge_path_string .. "/") then 1.67 - elseif try_exec(path_string .. ".lua") then 1.68 - elseif try_dir(path_string .. "/") then 1.69 + if merge and try_exec(merge_path .. ".lua") then 1.70 + elseif merge and try_dir(merge_path .. "/") then 1.71 + elseif try_exec(path .. ".lua") then 1.72 + elseif try_dir(path .. "/") then 1.73 else end 1.74 return rawget(self, key) 1.75 end 1.76 - install_autoloader(_G, nil, {}) 1.77 + install_autoloader(_G, nil, "") 1.78 try_exec(WEBMCP_FRAMEWORK_PATH .. "env/__init.lua") 1.79 try_exec(WEBMCP_BASE_PATH .. "env/__init.lua") 1.80 end