lua-users home
lua-l archive

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




On 06/07/16 04:02 PM, Tim Hill wrote:
On Jul 6, 2016, at 9:06 AM, Soni L. <fakedme@gmail.com> wrote:



On 06/07/16 05:15 AM, steve donovan wrote:
On Wed, Jul 6, 2016 at 8:25 AM, steve donovan <steve.j.donovan@gmail.com> wrote:
in place then the `n` field becomes an implementation detail. The
table functions start respecting this, and nirvana is achieved.
Alas, but freedom from the wheel of death and rebirth is always harder
than it first seems.

The table returned from table.pack() now has a metatable (for __len)
and inevitably it will be passed to Lua code which doesn't respect
this. For example, pl.List will take a table (_assumed_ to be a
sequence) and slap its own metatable on it, rebranding it as a List
[0]. The carefully-crafted __len is lost in translation.

[0] the name is the result of initial Python-nostalgia, which has faded.

table.list(t) -> setmetatable(t, listmt); local i = 0; for k,v in pairs(t) do if math.type(k) == "integer" and k > i then i = k end end t._N = i --[[ alternatively, put it in the registry in a weak table, to avoid conflicts with user keys ]]
table.remove(t) -> if debug.getmetatable(t) == listmt then t._N = t._N-1 end --[[ remove key and move everything over 1 ]]
table.insert(t) -> if debug.getmetatable(t) == listmt then t._N = t._N+1 end --[[ move everything over 1 and add key ]]
listmt -> {__newindex=--[[function to update _N if the new key is bigger than current _N]], __len=function(t) return t._N end, __metatable="list"} --[[ this would be stored in the registry, not sure if there'd be a way to un-listify a list ]]

This would probably be the easiest way to do it such that some tables would act like arraylists, it'd also replace that module you're talking about. However, for performance reasons, we'd need a way to mass-remove and mass-insert keys. (as in _N = _N-count instead of for i=1,count do _N=_N-1 end)

All this just to fix sequences? As I noted before, the root cause here is that a sequence should be able to contain nil values. If it did, none of these work-arounds would be needed.
Without bloating the table type too much. It's better to add this to the table library instead of adding it to the table type. It'll also be easier to handle on the C side of things as you won't have to redesign the whole Lua VM for a completely redesigned table type.

Actually, if the whole Lua VM were to be redesigned, it'd stop being Lua 5.x, instead becoming Lua 6.x. And it'd be slower and heavier, because then you'd track size for all tables, not just the tables that need it.

—Tim




--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.