[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to comprehend that Lua automatically removes whatever is in the stack below the results after the function returned?
- From: Eretnek Hippi Messiás <hippi777@...>
- Date: Sat, 26 Sep 2020 12:49:41 +0000
hi all! :)
孙世龙 sunshilong:
> 1. How to pre-allocate a fixed size table in Lua?
1st:
get a fake `nil`, if too obvious, then jump to "2nd:" :D
so lets say
`NULL={}`
or
```
NULL=setmetatable(
{},
{ __newindex=function() end,
__metatable=true})
```
or
`NULL=function() end`
the point is to have a unique identity that differs from everything else (recently (in the version detection hack topic?) it was shown that in some cases u can get the same function more than once if it is cached and identical and i think if it comes from the same place, like a generator; and much earlier someone told that the plain empty table approach can leak data in a sandbox, and there was the suggestion for an empty function, but i think the metatable approach is the best for all cases...)
2nd:
make a `table.new()` (or anything like) that prefills ur table with these `NULL`s for the keys u want (with a hash part that has unknown keys, it is harder, but when they are known, then this initializer can get the keys). and give it a metatable where `__newindex` will insert a `NULL` instead of `nil`s and where `__index` will give back `nil`s instead of `NULL`s.
probably some implementation details could matter a bit (like the actual size could vary, while still being constant), but if either u dont change the number of the elements in the hash part or u dont cross a boundary for the size (to trigger a shrink/grow), then it shouldnt change. the former is more questionable if theres a way at all for removing and adding a key, in other words, change one to something else, either to consume a dummy placeholder or just to reuse a key slot...
bests, have fun! :)
Sent with ProtonMail Secure Email.