Skip to content

Commit 972f241

Browse files
Allow pairs, ipairs, and next to work with read-only tables
1 parent 5505d42 commit 972f241

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

file-browser.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ API.coroutine = {}
378378
local ABORT_ERROR = {
379379
msg = "browser is no longer waiting for list - aborting parse"
380380
}
381-
local newindex = function(t, k, v) error(("attempted to assign `%s` to key `%s` in read-only %s"):format(v, k, t), 2) end
381+
local readonly_newindex = function(t, k, v) error(("attempted to assign `%s` to key `%s` in read-only %s"):format(v, k, t), 2) end
382382

383383
--implements table.pack if on lua 5.1
384384
if not table.pack then
@@ -730,7 +730,7 @@ do
730730
if references[t] then return references[t] end
731731

732732
local ro = setmetatable({}, {
733-
__newindex = newindex,
733+
__newindex = readonly_newindex,
734734
mutable = t,
735735
__index = function(_, k)
736736
return API.read_only( t[k] )
@@ -743,6 +743,19 @@ do
743743
end
744744
end
745745

746+
--returns read-only references to all given values
747+
function API.read_only_values(...)
748+
local vals = table.pack(...)
749+
for i, v in ipairs(vals) do vals[i] = API.read_only(v) end
750+
return table.unpack(vals)
751+
end
752+
753+
--returns true if the given tale is read-only
754+
function API.is_read_only(t)
755+
local mt = getmetatable(t)
756+
return mt and mt.__newindex == readonly_newindex
757+
end
758+
746759

747760

748761
--------------------------------------------------------------------------------------------------------
@@ -1042,7 +1055,7 @@ end
10421055
--select all items in the list
10431056
local function select_all()
10441057
local selection = {}
1045-
for i,_ in ipairs(state.list) do
1058+
for i in ipairs(state.list) do
10461059
selection[i] = true
10471060
end
10481061

0 commit comments

Comments
 (0)