From ea6bfdb0c4c99bce09b1c0ea0361ac9864f4f1f3 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Thu, 22 Aug 2024 17:49:06 -0300 Subject: Teal: convert luarocks.core.persist --- src/luarocks/core/persist.lua | 81 ------------------------------------------- src/luarocks/core/persist.tl | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 81 deletions(-) delete mode 100644 src/luarocks/core/persist.lua create mode 100644 src/luarocks/core/persist.tl (limited to 'src') diff --git a/src/luarocks/core/persist.lua b/src/luarocks/core/persist.lua deleted file mode 100644 index 57e7b5d4..00000000 --- a/src/luarocks/core/persist.lua +++ /dev/null @@ -1,81 +0,0 @@ - -local persist = {} - -local require = nil --------------------------------------------------------------------------------- - ---- Load and run a Lua file in an environment. --- @param filename string: the name of the file. --- @param env table: the environment table. --- @return (true, any) or (nil, string, string): true and the return value --- of the file, or nil, an error message and an error code ("open", "load" --- or "run") in case of errors. -function persist.run_file(filename, env) - local fd, err = io.open(filename) - if not fd then - return nil, err, "open" - end - local str, err = fd:read("*a") - fd:close() - if not str then - return nil, err, "open" - end - str = str:gsub("^#![^\n]*\n", "") - local chunk, ran - if _VERSION == "Lua 5.1" then -- Lua 5.1 - chunk, err = loadstring(str, filename) - if chunk then - setfenv(chunk, env) - ran, err = pcall(chunk) - end - else -- Lua 5.2 - chunk, err = load(str, filename, "t", env) - if chunk then - ran, err = pcall(chunk) - end - end - if not chunk then - return nil, "Error loading file: "..err, "load" - end - if not ran then - return nil, "Error running file: "..err, "run" - end - return true, err -end - ---- Load a Lua file containing assignments, storing them in a table. --- The global environment is not propagated to the loaded file. --- @param filename string: the name of the file. --- @param tbl table or nil: if given, this table is used to store --- loaded values. --- @return (table, table) or (nil, string, string): a table with the file's assignments --- as fields and set of undefined globals accessed in file, --- or nil, an error message and an error code ("open"; couldn't open the file, --- "load"; compile-time error, or "run"; run-time error) --- in case of errors. -function persist.load_into_table(filename, tbl) - assert(type(filename) == "string") - assert(type(tbl) == "table" or not tbl) - - local result = tbl or {} - local globals = {} - local globals_mt = { - __index = function(t, 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, err, errcode - end - return result, globals -end - -return persist - diff --git a/src/luarocks/core/persist.tl b/src/luarocks/core/persist.tl new file mode 100644 index 00000000..89cac97e --- /dev/null +++ b/src/luarocks/core/persist.tl @@ -0,0 +1,71 @@ + +local record persist +end + +-------------------------------------------------------------------------------- + +--- Load and run a Lua file in an environment. +-- @param filename string: the name of the file. +-- @param env table: the environment table. +-- @return (true, any) or (nil, string, string): true and the return value +-- of the file, or nil, an error message and an error code ("open", "load" +-- or "run") in case of errors. +function persist.run_file(filename: string, env: {string:any}): boolean, any | string, string + local fd, open_err: FILE, any = io.open(filename) + if not fd then + return nil, open_err, "open" + end + local str, read_err: string, string = fd:read("*a") + fd:close() + if not str then + return nil, read_err, "open" + end + str = str:gsub("^#![^\n]*\n", "") + local chunk, ran, err: function(...: any):(any), boolean, any + 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 + +--- Load a Lua file containing assignments, storing them in a table. +-- The global environment is not propagated to the loaded file. +-- @param filename string: the name of the file. +-- @param tbl table or nil: if given, this table is used to store +-- loaded values. +-- @return (table, table) or (nil, string, string): a table with the file's assignments +-- as fields and set of undefined globals accessed in file, +-- or nil, an error message and an error code ("open"; couldn't open the file, +-- "load"; compile-time error, or "run"; run-time error) +-- in case of errors. +function persist.load_into_table(filename: string, tbl?: {string:any}) : {any: any}, {any: any} | string, string + + local result: {string:any} = tbl or {} + local globals = {} + local globals_mt = { + __index = function(_, k: string) + 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 + +return persist + -- cgit v1.2.3-55-g6feb