local rc = e2util.exists(path)
if rc then
e2lib.logf(3, "using global config file: %s", path)
- rc, re = e2lib.dofile_protected(path, c, true)
+ rc, re = e2lib.dofile2(path, c, true)
if not rc then
return false, re
end
c.extensions = function(x)
c.data = x
end
- local rc, re = e2lib.dofile_protected(e2lib.globals.extension_config, c, true)
+ local rc, re = e2lib.dofile2(e2lib.globals.extension_config, c, true)
if not rc then
return false, e:cat(re)
end
return rc, e
end
---- Protected execution of Lua code.
--- Runs the code in the Lua file at path with a restricted global environment.
--- gtable contains a table with the initial global environment. If allownewdefs
--- is given and true, then the code may define new global variables.
--- This function aborts on error.
--- XXX: This looks like a more restricted version of dofile2 with problematic
--- error handling. Merge the two and fix usage.
--- @param path Filename to load lua code from (string).
--- @param gtable Environment (table) that is used instead of the global _G.
--- @param allownewdefs Allow adding new definitions to gtable (boolean).
--- @return True on success.
--- @see dofile2
-function e2lib.dofile_protected(path, gtable, allownewdefs)
+--- Executes Lua code loaded from path.
+--@param path Filename to load lua code from (string).
+--@param gtable Environment (table) that is used instead of the global _G.
+--@param allownewdefs Boolean indicating whether new variables may be defined
+-- and undefined ones read.
+--@return True on success, false on error.
+--@return Error object on failure.
+function e2lib.dofile2(path, gtable, allownewdefs)
+ local e = err.new("error loading config file: %s", path)
local chunk, msg = loadfile(path)
if not chunk then
- return false, msg
+ return false, e:cat(msg)
end
- local t = gtable
- -- t._G = t
+
local function checkread(t, k)
local x = rawget(t, k)
- if x then return x
- else e2lib.abort(path, ": attempt to reference undefined global variable '",
- k, "'")
+ if x then
+ return x
+ else
+ e2lib.abort(path, ": attempt to reference undefined global variable '", k, "'")
end
end
+
local function checkwrite(t, k, v)
e2lib.abort(path, ": attempt to set new global variable `", k, "' to ", v)
end
+
if not allownewdefs then
- setmetatable(t, { __newindex = checkwrite, __index = checkread })
- end
- setfenv(chunk, t)
- local s, msg = pcall(chunk)
- if not s then
- e2lib.abort(msg)
+ setmetatable(gtable, { __newindex = checkwrite, __index = checkread })
end
- return true, nil
-end
---- Executes Lua code loaded from path.
---@param path Filename to load lua code from (string).
---@param gtable Environment (table) that is used instead of the global _G.
---@return True on success, false on error.
---@return Error object on failure.
-function e2lib.dofile2(path, gtable)
- local e = err.new("error loading config file: %s", path)
- local chunk, msg = loadfile(path)
- if not chunk then
- return false, e:cat(msg)
- end
setfenv(chunk, gtable)
local s, msg = pcall(chunk)
if not s then
local function func(table)
dest[index] = table
end
- local rc, re = e2lib.dofile_protected(path, { [var] = func, env = info.env, string=string })
+ local rc, re = e2lib.dofile2(path, { [var] = func, env = info.env, string=string }, false)
if not rc then
return false, e:cat(re)
end
g[type] = f[type] -- and some config functions
end
- rc, re = e2lib.dofile2(path, g)
+ rc, re = e2lib.dofile2(path, g, true)
if not rc then
return nil, e:cat(re)
end
g.e2env = info.env -- env as built up so far
g.string = string -- string
g.env = mergeenv
- rc, re = e2lib.dofile2(path, g)
+ rc, re = e2lib.dofile2(path, g, true)
if not rc then
return false, e:cat(re)
end