From 966b1ae8089631caec5cad752e88faac86d35cdd Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Fri, 3 May 2013 13:07:03 +0200 Subject: [PATCH] Merge dofile_protected() into dofile2() Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 60 +++++++++++++++----------------------------- generic/e2option.lua | 2 +- local/e2tool.lua | 6 ++--- 3 files changed, 24 insertions(+), 44 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index f2b7e01..99e3225 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -867,7 +867,7 @@ function e2lib.read_global_config(e2_config_file) 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 @@ -924,7 +924,7 @@ function e2lib.read_extension_config() 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 @@ -1173,57 +1173,37 @@ function e2lib.callcmd_log(cmd, loglevel) 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 diff --git a/generic/e2option.lua b/generic/e2option.lua index c482f19..c04dcbc 100644 --- a/generic/e2option.lua +++ b/generic/e2option.lua @@ -229,7 +229,7 @@ local function userdefaultoptions(opts) end local e2rc = {} - local rc, e = e2lib.dofile_protected(file, { e2rc = function(t) e2rc = t end }) + local rc, e = e2lib.dofile2(file, { e2rc = function(t) e2rc = t end }, false) if not rc then return false, e end diff --git a/local/e2tool.lua b/local/e2tool.lua index 58024e7..764de1c 100644 --- a/local/e2tool.lua +++ b/local/e2tool.lua @@ -284,7 +284,7 @@ local function load_user_config(info, path, dest, index, var) 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 @@ -357,7 +357,7 @@ local function load_user_config2(info, path, types) 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 @@ -836,7 +836,7 @@ local function load_env_config(info, file) 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 -- 2.39.5