Skip to content

Commit 3a2d67d

Browse files
API.copy_table: now dereferences readonly table references
The copies are not readonly.
1 parent 78264e6 commit 3a2d67d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

file-browser.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ API.coroutine = {}
374374
local ABORT_ERROR = {
375375
msg = "browser is no longer waiting for list - aborting parse"
376376
}
377+
local newindex = function(t, k, v) error(("attempted to assign `%s` to key `%s` in read-only %s"):format(v, k, t), 2) end
377378

378379
--implements table.pack if on lua 5.1
379380
if not table.pack then
@@ -685,8 +686,13 @@ local function copy_table_recursive(t, references, depth)
685686
if type(t) ~= "table" or depth == 0 then return t end
686687
if references[t] then return references[t] end
687688

689+
--bypasses the proxy protecting read-only tables
690+
local mt = getmetatable(t)
691+
local original_t
692+
if mt and mt.__newindex == newindex then original_t = mt.__index end
693+
688694
local copy = setmetatable({}, { __original = t })
689-
references[t] = copy
695+
references[original_t or t] = copy
690696

691697
for key, value in pairs(t) do
692698
key = copy_table_recursive(key, references, depth - 1)
@@ -704,7 +710,6 @@ end
704710
--handles the read-only table logic
705711
do
706712
local references = setmetatable({}, { __mode = 'k' })
707-
local newindex = function(t, k, v) error(("attempted to assign `%s` to key `%s` in read-only %s"):format(v, k, t)) end
708713

709714
--returns a read-only reference to the table t
710715
function API.read_only(t)

0 commit comments

Comments
 (0)