Skip to content

Updates tablex readonly to assert on non-iterable/indexable data - #500

Open
jonsaun-annapurna wants to merge 1 commit into
lunarmodules:masterfrom
jonsaun-annapurna:patch-1
Open

Updates tablex readonly to assert on non-iterable/indexable data#500
jonsaun-annapurna wants to merge 1 commit into
lunarmodules:masterfrom
jonsaun-annapurna:patch-1

Conversation

@jonsaun-annapurna

Copy link
Copy Markdown

Consider the case where non-indexable/non-iterable data is provided to readonly. We accomplish this by creating an empty table to return and manipulating its metatable. We make the original data the __index of the empty table, have __pairs/__ipairs call pairs/ipairs on the original data, and then we obfuscate via __metatable to restrict access to the underlying data. However if we have non-indexable or non-iterable data for __index, like a number or a string, we cannot access the data at all without extraordinary measures. ipairs/pairs will not give us anything fruitful and attempting to poke the value directly via key/index will result in nothing because the underlying data is not indexable/iterable. The only way to access the data is via debug where the obfuscated metatable can be obtained, and the readonly property is invalidated by accessing the underlying table via the __index property.

Consider the case where non-indexable/non-iterable data is provided to readonly. We accomplish this by creating an empty table and using metatable manipulation. We make the original data the index of the empty table, attempt to use pairs/ipairs on the original data, and then we obfuscate the metatable to restrict access to the underlying data. However if we have non-indexable or non-iterable data like a number or a string, we cannot access the data at all. ipairs/pairs will not give us anything fruitful and attempting to poke the value directly via key/index will result in nothing because the underlying data is not indexable/iterable. The only way to access the data is via debug where the obfuscated metatable can be obtained, and the readonly property invalidated by accessing the underlying table via the __index property.

Adding this assert prevents this invalid and intuitive use of readonly that people coming from languages that interface with Lua (C/C++) might confuse with something like const.
@jonsaun-annapurna

Copy link
Copy Markdown
Author

I understand this PR may not be desired due to intentions to support a world where readonly is used with non-iterable/non-indexable data. This would create an environment where "readonly" numbers/strings could be used, albeit with specific APIs to be used to modify and access data. Why someone would want that behavior via tablex.readonly personally puzzles me, but I can chalk it up to differences of opinion. This PR was low effort as I have been using a wrapper to enforce this behavior for some time in other projects due to the non-intuitive nature required to access readonly numbers/strings. Can continue doing so if this PR is rejected.

@Tieske

Tieske commented Oct 22, 2025

Copy link
Copy Markdown
Member

the change makes sense to me. It is in tablex hence applying it on anything else than a table doesn't make sense to me.

@Tieske

Tieske commented Oct 22, 2025

Copy link
Copy Markdown
Member

any chance you can add some tests?

@jonsaun-annapurna

Copy link
Copy Markdown
Author

Yeah. I'll get some tests added and update the PR later this week :)

@Tieske

Tieske commented Jan 9, 2026

Copy link
Copy Markdown
Member

@jonsaun-annapurna ping

ps. you might want to rebase on master which got some CI and Lua 5.5 updates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants