aboutsummaryrefslogtreecommitdiff
path: root/src/compat53/file_mt.lua
diff options
context:
space:
mode:
authorTobiasz Laskowski <tobil4sk@outlook.com>2025-02-24 21:20:56 +0000
committerHisham Muhammad <hisham@gobolinux.org>2025-02-24 20:11:55 -0300
commit71b7dd641892dd302cf37c270c472248b24ef8a4 (patch)
treebe59dcd6d92e0e3f8b354a17a6781ba03a295614 /src/compat53/file_mt.lua
parentc71174f1c20c2b57265a5cedfdccda559445e55c (diff)
downloadluarocks-71b7dd641892dd302cf37c270c472248b24ef8a4.tar.gz
luarocks-71b7dd641892dd302cf37c270c472248b24ef8a4.tar.bz2
luarocks-71b7dd641892dd302cf37c270c472248b24ef8a4.zip
Add vendored lua compat53 v0.14.4
Diffstat (limited to 'src/compat53/file_mt.lua')
-rw-r--r--src/compat53/file_mt.lua71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/compat53/file_mt.lua b/src/compat53/file_mt.lua
new file mode 100644
index 00000000..6433619d
--- /dev/null
+++ b/src/compat53/file_mt.lua
@@ -0,0 +1,71 @@
1local lua_version = _VERSION:sub(-3)
2
3local M = {}
4
5local unpack = lua_version == "5.1" and unpack or table.unpack
6
7local function addasterisk(fmt)
8 if type(fmt) == "string" and fmt:sub(1, 1) ~= "*" then
9 return "*"..fmt
10 else
11 return fmt
12 end
13end
14
15function M.update_file_meta(file_meta, is_luajit52)
16
17 -- make '*' optional for file:read and file:lines
18
19 local file_lines = file_meta.__index.lines
20 file_meta.__index.lines = function(self, ...)
21 local n = select('#', ...)
22 for i = 1, n do
23 local a = select(i, ...)
24 local b = addasterisk(a)
25 -- as an optimization we only allocate a table for the
26 -- modified format arguments when we have a '*' somewhere
27 if a ~= b then
28 local args = { ... }
29 args[i] = b
30 for j = i+1, n do
31 args[j] = addasterisk(args[j])
32 end
33 return file_lines(self, unpack(args, 1, n))
34 end
35 end
36 return file_lines(self, ...)
37 end
38
39 local file_read = file_meta.__index.read
40 file_meta.__index.read = function(self, ...)
41 local n = select('#', ...)
42 for i = 1, n do
43 local a = select(i, ...)
44 local b = addasterisk(a)
45 -- as an optimization we only allocate a table for the
46 -- modified format arguments when we have a '*' somewhere
47 if a ~= b then
48 local args = { ... }
49 args[i] = b
50 for j = i+1, n do
51 args[j] = addasterisk(args[j])
52 end
53 return file_read(self, unpack(args, 1, n))
54 end
55 end
56 return file_read(self, ...)
57 end
58
59 if not is_luajit52 then
60 local file_write = file_meta.__index.write
61 file_meta.__index.write = function(self, ...)
62 local ret, err = file_write(self, ...)
63 if ret then
64 return self
65 end
66 return ret, err
67 end
68 end
69end
70
71return M