File size: 2,018 Bytes
d7c5875 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local load = _tl_compat and _tl_compat.load or load; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string
local persist = {}
local json = require("luarocks.vendor.dkjson")
function persist.run_file(filename, env)
local fd, open_err = io.open(filename)
if not fd then
return nil, open_err, "open"
end
local str, read_err = fd:read("*a")
fd:close()
if not str then
return nil, read_err, "open"
end
str = str:gsub("^#![^\n]*\n", "")
local chunk, ran, err
chunk, err = load(str, filename, "t", env)
if chunk then
ran, err = pcall(chunk)
end
if not chunk then
return nil, "Error loading file: " .. tostring(err), "load"
end
if not ran then
return nil, "Error running file: " .. tostring(err), "run"
end
return true, err
end
function persist.load_into_table(filename, tbl)
local result = tbl or {}
local globals = {}
local globals_mt = {
__index = function(_, k)
globals[k] = true
end,
}
local save_mt = getmetatable(result)
setmetatable(result, globals_mt)
local ok, err, errcode = persist.run_file(filename, result)
setmetatable(result, save_mt)
if not ok then
return nil, tostring(err), errcode
end
return result, globals
end
function persist.load_json_into_table(filename)
local fd, open_err = io.open(filename)
if not fd then
return nil, open_err, "open"
end
local str, read_err = fd:read("*a")
fd:close()
if not str then
return nil, read_err, "open"
end
local manifest, _, err = json.decode(str)
if not manifest then
return nil, "Failed decode manifest: " .. err, "load"
end
return manifest, {}
end
return persist
|