Advertisement
goldfiction

require.lua

Aug 10th, 2023
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. -- -*- coding: utf8 -*-
  2. -- Copyright (c) 2014 Odd Straaboe <oddstr13 at openshell dot no>
  3. -- License: MIT - http://opensource.org/licenses/MIT
  4. -- Filename: require.lua
  5.  
  6. _G.package = {}
  7.  
  8. _G.package.cpath = ""
  9. _G.package.loaded = {}
  10. _G.package.loadlib = function() error("NotImplemented: package.loadlib") end
  11. _G.package.path = table.concat({
  12.     "?",
  13.     "?.lua",
  14.     "?/init.lua",
  15.     "/lib/?",
  16.     "/lib/?.lua",
  17.     "/lib/?/init.lua",
  18.     "/rom/apis/?",
  19.     "/rom/apis/?.lua",
  20.     "/rom/apis/?/init.lua",
  21.     "/rom/apis/turtle/?",
  22.     "/rom/apis/turtle/?.lua",
  23.     "/rom/apis/turtle/?/init.lua",
  24.     "/rom/apis/command/?",
  25.     "/rom/apis/command/?.lua",
  26.     "/rom/apis/command/?/init.lua",
  27. }, ";")
  28. _G.package.preload = {}
  29. _G.package.seeall = function(module) error("NotImplemented: package.seeall") end
  30. _G.module = function(m) error("NotImplemented: module") end
  31.  
  32. local _package_path_loader = function(name)
  33.    
  34.     local fname = name:gsub("%.", "/")
  35.    
  36.     for pattern in package.path:gmatch("[^;]+") do
  37.        
  38.         local fpath = pattern:gsub("%?", fname)
  39.        
  40.         if fs.exists(fpath) and not fs.isDir(fpath) then
  41.            
  42.             local apienv = {}
  43.             setmetatable(apienv, {__index = _G})
  44.            
  45.             local apifunc, err = loadfile(fpath)
  46.             local ok
  47.            
  48.             if apifunc then
  49.                 setfenv(apifunc, apienv)
  50.                 ok, err = pcall(apifunc)
  51.             end
  52.            
  53.             if not apifunc or not ok then
  54.                 error("error loading module '" .. name .. "' from file '" .. fpath .. "'\n\t" .. err)
  55.             end
  56.            
  57.             local api = {}
  58.             if type(err) == "table" then
  59.               api = err
  60.             end
  61.             for k,v in pairs( apienv ) do
  62.                 api[k] = v
  63.             end
  64.            
  65.             return api
  66.         end
  67.     end
  68. end
  69.  
  70. _G.package.loaders = {
  71.     function(name)
  72.         if package.preload[name] then
  73.             return package.preload[name]
  74.         else
  75.             return "\tno field package.preload['" .. name .. "']"
  76.         end
  77.     end,
  78.    
  79.     function(name)
  80.         local _errors = {}
  81.        
  82.         local fname = name:gsub("%.", "/")
  83.        
  84.         for pattern in package.path:gmatch("[^;]+") do
  85.            
  86.             local fpath = pattern:gsub("%?", fname)
  87.             if fs.exists(fpath) and not fs.isDir(fpath) then
  88.                 return _package_path_loader
  89.             else
  90.                 table.insert(_errors, "\tno file '" .. fpath .. "'")
  91.             end
  92.         end
  93.        
  94.         return table.concat(_errors, "\n")
  95.     end
  96. }
  97.  
  98. _G.require = function(name)
  99.     if package.loaded[name] then
  100.         return package.loaded[name]
  101.     end
  102.    
  103.     local _errors = {}
  104.    
  105.     for _, searcher in pairs(package.loaders) do
  106.         local loader = searcher(name)
  107.         if type(loader) == "function" then
  108.             local res = loader(name)
  109.             if res ~= nil then
  110.                 package.loaded[name] = res
  111.             end
  112.            
  113.             if package.loaded[name] == nil then
  114.                 package.loaded[name] = true
  115.             end
  116.            
  117.             return package.loaded[name]
  118.         elseif type(loader) == "string" then
  119.             table.insert(_errors, loader)
  120.         end
  121.     end
  122.    
  123.     error("module '" .. name .. "' not found:\n" .. table.concat(_errors, "\n"))
  124. end
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement