aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-08-22 17:49:06 -0300
committerHisham Muhammad <hisham@gobolinux.org>2024-10-21 13:30:51 -0300
commitea6bfdb0c4c99bce09b1c0ea0361ac9864f4f1f3 (patch)
treeeaf76230bbbd09dd68e5f8c37a48c6f7ca937dad /src
parentdb62bad5a02e3dd8b44b81aae0384e632672eee1 (diff)
downloadluarocks-ea6bfdb0c4c99bce09b1c0ea0361ac9864f4f1f3.tar.gz
luarocks-ea6bfdb0c4c99bce09b1c0ea0361ac9864f4f1f3.tar.bz2
luarocks-ea6bfdb0c4c99bce09b1c0ea0361ac9864f4f1f3.zip
Teal: convert luarocks.core.persist
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/core/persist.tl (renamed from src/luarocks/core/persist.lua)44
1 files changed, 17 insertions, 27 deletions
diff --git a/src/luarocks/core/persist.lua b/src/luarocks/core/persist.tl
index 57e7b5d4..89cac97e 100644
--- a/src/luarocks/core/persist.lua
+++ b/src/luarocks/core/persist.tl
@@ -1,7 +1,7 @@
1 1
2local persist = {} 2local record persist
3end
3 4
4local require = nil
5-------------------------------------------------------------------------------- 5--------------------------------------------------------------------------------
6 6
7--- Load and run a Lua file in an environment. 7--- Load and run a Lua file in an environment.
@@ -10,35 +10,27 @@ local require = nil
10-- @return (true, any) or (nil, string, string): true and the return value 10-- @return (true, any) or (nil, string, string): true and the return value
11-- of the file, or nil, an error message and an error code ("open", "load" 11-- of the file, or nil, an error message and an error code ("open", "load"
12-- or "run") in case of errors. 12-- or "run") in case of errors.
13function persist.run_file(filename, env) 13function persist.run_file(filename: string, env: {string:any}): boolean, any | string, string
14 local fd, err = io.open(filename) 14 local fd, open_err: FILE, any = io.open(filename)
15 if not fd then 15 if not fd then
16 return nil, err, "open" 16 return nil, open_err, "open"
17 end 17 end
18 local str, err = fd:read("*a") 18 local str, read_err: string, string = fd:read("*a")
19 fd:close() 19 fd:close()
20 if not str then 20 if not str then
21 return nil, err, "open" 21 return nil, read_err, "open"
22 end 22 end
23 str = str:gsub("^#![^\n]*\n", "") 23 str = str:gsub("^#![^\n]*\n", "")
24 local chunk, ran 24 local chunk, ran, err: function(...: any):(any), boolean, any
25 if _VERSION == "Lua 5.1" then -- Lua 5.1 25 chunk, err = load(str, filename, "t", env)
26 chunk, err = loadstring(str, filename) 26 if chunk then
27 if chunk then 27 ran, err = pcall(chunk)
28 setfenv(chunk, env)
29 ran, err = pcall(chunk)
30 end
31 else -- Lua 5.2
32 chunk, err = load(str, filename, "t", env)
33 if chunk then
34 ran, err = pcall(chunk)
35 end
36 end 28 end
37 if not chunk then 29 if not chunk then
38 return nil, "Error loading file: "..err, "load" 30 return nil, "Error loading file: "..tostring(err), "load"
39 end 31 end
40 if not ran then 32 if not ran then
41 return nil, "Error running file: "..err, "run" 33 return nil, "Error running file: "..tostring(err), "run"
42 end 34 end
43 return true, err 35 return true, err
44end 36end
@@ -53,14 +45,12 @@ end
53-- or nil, an error message and an error code ("open"; couldn't open the file, 45-- or nil, an error message and an error code ("open"; couldn't open the file,
54-- "load"; compile-time error, or "run"; run-time error) 46-- "load"; compile-time error, or "run"; run-time error)
55-- in case of errors. 47-- in case of errors.
56function persist.load_into_table(filename, tbl) 48function persist.load_into_table(filename: string, tbl?: {string:any}) : {any: any}, {any: any} | string, string
57 assert(type(filename) == "string")
58 assert(type(tbl) == "table" or not tbl)
59 49
60 local result = tbl or {} 50 local result: {string:any} = tbl or {}
61 local globals = {} 51 local globals = {}
62 local globals_mt = { 52 local globals_mt = {
63 __index = function(t, k) 53 __index = function(_, k: string)
64 globals[k] = true 54 globals[k] = true
65 end 55 end
66 } 56 }
@@ -72,7 +62,7 @@ function persist.load_into_table(filename, tbl)
72 setmetatable(result, save_mt) 62 setmetatable(result, save_mt)
73 63
74 if not ok then 64 if not ok then
75 return nil, err, errcode 65 return nil, tostring(err), errcode
76 end 66 end
77 return result, globals 67 return result, globals
78end 68end