Updates tablex readonly to assert on non-iterable/indexable data - #500
Updates tablex readonly to assert on non-iterable/indexable data#500jonsaun-annapurna wants to merge 1 commit into
Conversation
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.
|
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. |
|
the change makes sense to me. It is in |
|
any chance you can add some tests? |
|
Yeah. I'll get some tests added and update the PR later this week :) |
|
@jonsaun-annapurna ping ps. you might want to rebase on |
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.