[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Macro expansion of lua_newuserdata()?
- From: "Joseph C. Sible" <josephcsible@...>
- Date: Sun, 19 Jul 2020 21:54:13 -0400
On Sun, Jul 19, 2020 at 9:48 PM Paul Ducklin <pducklin@outlook.com> wrote:
>
> Forgive my ignorance if this should be obvious, but why is the API macro...
>
> lua_newuserdata(L,size)
>
> ...expanded into...
>
> lua_newuserdatauv(L,size,1)
>
> ...and not into...
>
> lua_newuserdatauv(L,size,0)
>
> ?
>
> (Userdata “user values” are a new thing in 5.4.)
>
> The Lua sources themselves never use the macro form, always calling lua_newuserdatauv() explicitly instead. And in every such call, the last parameter (number of user values) is always explicitly set to zero, as though zero is the preferred choice for vanilla userdata allocations.
>
> From a glance at the source code I would guess it has something weird to do with garbage collection... but if so, what? And why? What’s with allocating that 1 “user value” slot for userdatas that never use it?
>
User values are not new to 5.4. They have existed since 5.2. The
change in 5.4 is that each userdata can now have an arbitrary number
of them instead of having to have exactly one of them. The reason that
lua_newuserdata uses 1 instead of 0 is to avoid breaking backwards
compatibility with old code that assumed there would be one of them.
You should use lua_newuserdatauv with 0 in your own code if it doesn't
actually need the user value.
Joseph C. Sible