lua-users home
lua-l archive

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

On Tue, 27 Sept 2022 at 02:34, ubq323 <> wrote:
> what is the purpose of the lua_setuservalue and lua_getuservalue
> (or in lua 5.4, getiuservalue and setiuservalue) functions?
> what's the intended use case of being able to associate arbitrary lua
> values with full userdata?

I doubt there is a intended use case, it's an enabler in case you need it.

> and does anyone know of any notable or interesting uses of this
> functionality?

I do not know if they are notable, but I use them a lot to keep
C-objects wrapped in userdata referenced.

I.e., in my curl-wrapper I use
  const int I_REQUEST_MASTER = 1; // Set while submitted
  const int I_REQUEST_CALLBACK = 2; // Optional
  const int I_REQUEST_RESPONSE = 3; // Set while performing ( sync or
async ).
  const int I_REQUEST_PARENT = 4; // Optional, if cloned.
  const int I_REQUEST_URL = 5; // string, curlu or nil
  const int I_REQUEST_SHARE = 6; // Optional
  const int I_REQUEST_HEADERS = 7; // SLIST, optional
7 uservalues in the request objects, some like callback are lua
values, the rest are userdatas which need to be kept alive. The caster
class has a uservalue which is a table of the requests ( keyed by
their address as light userdata ).

I also have a string class ( wrapping std:string ), and regex classes
which reference them, and regex match classes which do too.

It is not strictly needed, but it simplifies coding a lot when you
have objects which depend on others.

Francisco Olarte.