Posted 07 April 2016 - 03:25 AM
So I was looking a bit into setfenv and getfenv (still unsure whether or not it is applicable to my scenario) because I'm trying to set a global table for the file/function that I'm going to use, which setfenv apparently can do. However I'm making not an explicit compiler but a file that will copy the contents of other files and set their environment after some variable manipulation has occured. So far I've been able to read the file into a variable which is appended to a table (libMem in example) localized in the file of the script currently running and then from that table copy the keys and values to essentially make them as if they are accessible in the same scope as that table (here same scope a libMem). In effect it does this.
My problem however, is getting my variable manipulations (k=v) to be accessible to the other files which I will run with the exact same varName, I don't want to use os.loadAPI to load my script to the 'real' _G first and then access them as it may overwrite other _G indices and I'm not a fan. Overriding os.loadAPI may be an option if I use setfenv for the file(s) being appended but I'm not sure how this would work and need a bit of understanding of setfenv and getfenv to go along with it if possible.
Thanks :)/>/>/>/>/>
local libMem = {}
local globalize = 0
local function init()
--read all files in a directory. lets pretend returns only one file: /lib/file.lua
local libDir = fs.list('/lib')
for _,file in ipairs(libDir) do
libMem[string.gsub(tostring(file),'.lua','') = readFile(tostring('/lib'..file)) --chop off .lua extension and append to libMem
end
globalize = 1
end
local function readFile(path)
local f = fs.open(path,'r')
local contents = f.readAll()
f.close()
return contents
end
init()
if globalize == 1 then
for k,v in ipairs(libMem) do k = v end
end
My problem however, is getting my variable manipulations (k=v) to be accessible to the other files which I will run with the exact same varName, I don't want to use os.loadAPI to load my script to the 'real' _G first and then access them as it may overwrite other _G indices and I'm not a fan. Overriding os.loadAPI may be an option if I use setfenv for the file(s) being appended but I'm not sure how this would work and need a bit of understanding of setfenv and getfenv to go along with it if possible.
Thanks :)/>/>/>/>/>