I recently learned the existence of methatables in lua, and i was toying around with them until an idea came to my mind: would it be possible to use those to try and avoid "duplicates" in a table? I searched and searched and so far couldn't find what i'm looking for, so here i am.
- So here's what i'd want to be able to do, and the purpose:
It's to be used in WoW addons programming. I want to make a tool that would prompt a warning whenever creating a variable or function in the global scope (to avoid using it, because of possible naming conflicts that could happen with other addons). Another thing i could want to do from there would be to redirect all transit from and to the _G table. So when the user creates a variable or function in the global scope, the tool would catch that, store it in a table instead of _G, and whenever the users would try to access something from _G, the tool would first look for it in said table; and use _G only as a fallback. That way, the user would be able not to worry about proper encapsulation or namings, the tool would take care of it all for him.
- What i already managed to do:
I'm setting a __newindex metamethod on _G to catch the global scoped variables and functions, and removing the metamethod at the end of the addon's loading, to avoid it being used by other addons. For the "indirection of _G transit", i already know how i could use __index to try and give the value stored in another table instead before trying to use _G.
- Issue i'm having:
This works well, but only with variables and functions that don't already exist in _G. Whenever assigning a value to a key that is already in the _G table, it doesn't work (for obvious reasons). I would like to indeed be able to catch those cases, and basically make it impossible to actually overwrite content of _G, and instead using a sort of "overload" (but without the user having to even know that).
- What i tried:
I tried to hook rawset, to see if it was called automatically, and it appears it is not.
I haven't been able to find much documentation about the _G table in lua, because of the short name mostly. I'm sure something must exist somewhere, and i could probably use the information to get things done the way i want, but currently i'm just kinda lost and running out of ideas. So yeah i'd like to know if there was any way to "catch" all the "implicit calls to rawset" in order to make some checks before letting it do its stuff. I gathered that apparently there is no metamethod for __existingindex or something, so do you know any way to do it please?
_G
, while setting__newindex
and__index
to functions that search the original_G
table, which is captured in some manner. Your__newindex
function should not allow redefining existing keys. I've never done such a thing, so I can't tell you how stable such a solution would be. It would certainly be very slow. I can do a full write up if you wish. EDIT: Look at implementations ofstrict.lua
for inspiration. – ktba=1
looks like_G['a']=1
, thus the first comment is the solution. Also, it is a repetition of the information that is provided here lua.org/pil/13.4.4.html . Also, the referenced book (maybe except C part), it holds answers to your questions about _G. You probably should read it whole. – Dimitry