Advertisement
Guest User

package.lua

a guest
Sep 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.88 KB | None | 0 0
  1. local package = {}
  2.  
  3. package.path = "/lib/?.lua;/usr/lib/?.lua;/home/lib/?.lua;./?.lua"
  4.  
  5. local loading = {}
  6.  
  7. local loaded = {
  8.   ["_G"] = _G,
  9.   ["bit32"] = bit32,
  10.   ["coroutine"] = coroutine,
  11.   ["math"] = math,
  12.   ["os"] = os,
  13.   ["package"] = package,
  14.   ["string"] = string,
  15.   ["table"] = table
  16. }
  17. package.loaded = loaded
  18.  
  19. local preload = {}
  20. package.preload = preload
  21.  
  22. package.searchers = {}
  23.  
  24. function package.searchpath(name, path, sep, rep)
  25.   checkArg(1, name, "string")
  26.   checkArg(2, path, "string")
  27.   sep = sep or '.'
  28.   rep = rep or '/'
  29.   sep, rep = '%' .. sep, rep
  30.   name = string.gsub(name, sep, rep)
  31.   local fs = require("filesystem")
  32.   local errorFiles = {}
  33.   for subPath in string.gmatch(path, "([^;]+)") do
  34.     subPath = string.gsub(subPath, "?", name)
  35.     if subPath:sub(1, 1) ~= "/" and os.getenv then
  36.       subPath = fs.concat(os.getenv("PWD") or "/", subPath)
  37.     end
  38.     if fs.exists(subPath) then
  39.       local file = io.open(subPath, "r")
  40.       if file then
  41.         file:close()
  42.         return subPath
  43.       end
  44.     end
  45.     table.insert(errorFiles, "\tno file '" .. subPath .. "'")
  46.   end
  47.   return nil, table.concat(errorFiles, "\n")
  48. end
  49.  
  50. local function preloadSearcher(module)
  51.   if preload[module] ~= nil then
  52.     return preload[module]
  53.   else
  54.     return "\tno field package.preload['" .. module .. "']"
  55.   end
  56. end
  57.  
  58. local function pathSearcher(module)
  59.   local filepath, reason = package.searchpath(module, package.path)
  60.   if filepath then
  61.     local loader, reason = loadfile(filepath, "bt", _G)
  62.     if loader then
  63.       return loader, filepath
  64.     else
  65.       return reason
  66.     end
  67.   else
  68.     return reason
  69.   end
  70. end
  71.  
  72. table.insert(package.searchers, preloadSearcher)
  73. table.insert(package.searchers, pathSearcher)
  74.  
  75. function require(module)
  76.   checkArg(1, module, "string")
  77.   if loaded[module] ~= nil then
  78.     return loaded[module]
  79.   elseif not loading[module] then
  80.     loading[module] = true
  81.     local loader, value, errorMsg = nil, nil, {"module '" .. module .. "' not found:"}
  82.     for i = 1, #package.searchers do
  83.       -- the pcall is mostly for out of memory errors
  84.       local ok, f, extra = pcall(package.searchers[i], module)
  85.       if not ok then
  86.         table.insert(errorMsg, "\t" .. f)
  87.       elseif f and type(f) ~= "string" then
  88.         loader = f
  89.         value = extra
  90.         break
  91.       elseif f then
  92.         table.insert(errorMsg, f)
  93.       end
  94.     end
  95.     if loader then
  96.       local success, result = pcall(loader, module, value)
  97.       loading[module] = false
  98.       if not success then
  99.         error(result, 2)
  100.       end
  101.       if result then
  102.         loaded[module] = result
  103.       elseif not loaded[module] then
  104.         loaded[module] = true
  105.       end
  106.       return loaded[module]
  107.     else
  108.       loading[module] = false
  109.       error(table.concat(errorMsg, "\n"), 2)
  110.     end
  111.   else
  112.     error("already loading: " .. module, 2)
  113.   end
  114. end
  115.  
  116. -------------------------------------------------------------------------------
  117.  
  118. return package
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement