Lyqyd, on 15 March 2013 - 10:14 AM, said:
Try customizing os.loadAPI. It nearly has the functionality you're looking for. getfenv() maaay be able to get you the rest of the way there.
That's what I've tried. It's setting the key for
_G[...] = tAPI that I can't get.
Here's the (rather messy) code I have at the moment:
function include(_sPath)
-- the os.loadAPI slightly modified
local sName = fs.getName( _sPath )
local tEnv = {
include = include
}
setmetatable( tEnv, { __index = _G } )
local fnAPI, err = loadfile( _sPath )
if fnAPI then
setfenv( fnAPI, tEnv )
includeStack[#includeStack + 1] = sName
print(#includeStack)
fnAPI()
else
error("Failed to include '"..sName.."'. "..err)
return false
end
local tAPI = {}
for k,v in pairs( tEnv ) do
tAPI[k] = v
end
--[[
--another failed attempt
local tTbl = _G
for _,v in ipairs( includeStack ) do
if tTbl == nil then
tTbl = {}
end
tTbl = tTbl[v]
end]]--
kStr = "_G"
for _,v in ipairs( includeStack ) do
kStr = kStr.."."..v
print(kStr)
end
tTbl = loadstring(kStr)
--this should just be replacing the load string, how do i set the key?
tTbl = tAPI
includeStack[#includeStack] = nil
return true
end