lua-users home
lua-l archive

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


Samir:

On Sun, 21 Aug 2022 at 18:53, Samir Tine <samir.tine@luart.org> wrote:
> table.unpack() do not check that the first argument is a table, resulting to an error when using any other value (latest Lua 5.4.4)

Nowhere does it say it checks, and only implicitly it says first arg
needs to be a table. It is defined as equivalent to a multiple value
return, and I suspect the implementation does just this ( and is coded
in C because you need table.unpack  to implement returning a different
count of values each time in lua but not in C (maybe it could be done
by load+call) ) . Testing a few values, like u(7,1,2), u("abcd"),
u(nil,1,2) it does the expected thing if the expression in the doc is
implemented.

> The function seems to call luaL_len() without checking that the first argument is a table.

This way it can work right with non-table but table-imitating things (
like an adequeate userdata ). Testing this kind of things is normally
pointless because the best you can usually do is just to throw an
error.

> Maybe this behavior isnexpected, in which case I'm sorry for the noise :)

Not exactly that by I expected this kind of behaviour ( I would be
more surprised by this:
> table.unpack("abcd")
nil    nil    nil    nil
> table.unpack("ab",5,7)
nil    nil    nil
but not much more ). ( This is because string supports len and
indexing, but not what the semantics of other languages, indexing is
just to find string methods ).

Regards.
    Francisco Olarte.