Skip to content

Commit 2ddce62

Browse files
implement API.read_only function
this will be what enforces immutability at runtime
1 parent ba35d81 commit 2ddce62

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

file-browser.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,21 @@ function API.copy_table(t, depth)
626626
return copy_table_recursive(t, {}, depth or math.huge)
627627
end
628628

629+
--handles the read-only table logic
630+
do
631+
local references = setmetatable({}, { __mode = 'k' })
632+
local newindex = function(t, k, v) error(("attempted to assign %s to key %s in read-only table %s"):format(v, k, t)) end
633+
634+
--returns a read-only reference to the table t
635+
function API.read_only(t)
636+
if references[t] then return references[t] end
637+
638+
local ro = setmetatable({}, { __index = t, __newindex = newindex })
639+
references[t] = ro
640+
return ro
641+
end
642+
end
643+
629644

630645

631646
--------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)