lua-users home
lua-l archive

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

"ubq323" <> writes:

> 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?
> and does anyone know of any notable or interesting uses of this
> functionality?

One use-case is to implement lazy loading of attributes and to "cache"
values: Marshaling values can be somewhat expensive, so a large value
can be turned into a userdata object, and attributes get marshaled on
demand. Once marshaled, attributes are kept around in a uservalue table,
so they won't have to be marshaled again.

We're using this in HsLua, a Haskell-to-Lua bridge:

Albert Krewinkel
GPG: 8eed e3e2 e8c5 6f18 81fe  e836 388d c0b2 1f63 1124