512mb-bot/libraries/import.lua

48 lines
1.5 KiB
Lua
Raw Permalink Normal View History

2021-11-26 16:38:17 +00:00
--Luvit's deadly sin - a library that fixes the dumbest problem in luvit
--That is, unability to load core modules from the files that were required
return function(reqfunc)
2022-05-20 18:20:49 +00:00
local function import(path)
local paths = {}
package.path:gsub("[^;]+",function(path)
table.insert(paths,path)
end)
local filename = path:gsub("%.","/")
local file = io.open(filename..".lua","r")
local iterator = 0
local last_filename = ""
while not file do
iterator = iterator + 1
if paths[iterator] then
file = io.open(paths[iterator]:gsub("%?",filename),"r")
last_filename = paths[iterator]
else
break
end
end
if not file then
return reqfunc(path)
else
content = file:read("*a")
local f,err = load(content,"import: "..filename,nil,setmetatable({
require = reqfunc,
import = import,
},{__index = _G}))
if err then
2022-05-20 21:21:23 +00:00
error("[import: "..filename.."] "..tostring(err))
2022-05-20 18:20:49 +00:00
end
return f()
end
2021-11-26 16:38:17 +00:00
end
2022-05-20 18:20:49 +00:00
return import
2021-11-26 16:38:17 +00:00
end
--[[
Usage:
import = require("import")(require)
file = import("file")
yes, THAT easy. moreover, once you have imported the import function, it will be passed
to the loaded libraries.
how hard is that to implement this but with the luvit's require, eh?
]]