[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: C API - lua_next traversal of "array" table
- From: Duane Leslie <parakleta@...>
- Date: Fri, 19 Aug 2016 11:57:13 +1000
> On 19 Aug 2016, at 11:22, ThePhD <jm3689@columbia.edu> wrote:
>
> I don't suppose sneaking in some kind of ridiculous key name that includes null characters and other things that are vastly improbable for a real Lua user to use (something like "__detail.do.\0.not.touch\xE2\x98\xA2.size") for when I push the table and then pop the table, and using that MAYBE as a shortcut if it's present, and otherwise falling back to iterating until I hit a `nil` might be worth it...?
No, just create a global weak proxy object and store your table lengths in there. There is never a need to store "magic" data on an allocated object itself. If you're using C code or the debug library you can put this proxy object in the registry, otherwise you can store it as an upvalue of a closure with wrapped functions, or however you like.
Something like
```lua
get_array_length, set_array_length = (function()
local proxy = setmetatable({}, { __mode = "k" })
return function(a) return proxy[a] end,
function(a,l) proxy[a] = l end
end)()
```
Regards,
Duane.