lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Oct 14, 2021 at 6:37 PM Flyer31 Test <flyer31@googlemail.com> wrote:
So I want also to avoid, that some user by some accident writes:
local SBtmp= stringbuf.new(100)

... as soon as the user then would program then any strbuf operation
(leading to my strbuf __newindex / __index / __concat...), then an
error will fire, as any such function will check whether _sb_SB... is
available in the _G table... . So it will recognize nicely any "local
misusing", and also if some user by some accident tries to create some
other name (e. g. sbtmp or tmp or something else...).

You can't set metamethods for local variables at all because locals are not stored in a table at all. Locals are stored directly in stack slots preassigned at compile time and there is no way to alter or intercept their runtime behavior. The only way to even dynamically inspect a local is through the debug library, which requires an index rather than a name.

So no, there's no way to prevent a user from creating a strbuf in a local variable.