lua-users home
lua-l archive

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





On 23 September 2013 14:44, Matthias Beyer <mail@beyermatthias.de> wrote:
Hi Lua ML!

I'm confused about the terminology: What is 'light' user data? Is is
something special? If I write a C function which pops an (in C defined
element) from the stack and creates a copy, should I push the result
of the copy operation as light user data?

    Code example:

    static int config_member_copy(lua_State *L) {
        struct config_member_t *mem;
        struct config_member_t *copy;

        mem = lua_touserdata(L, 1);
        lua_pop(L, 1);

        copy = some_magic_copy_function(mem);

        lua_pushlightuserdata(L, copy);

        return 1
    } 


--
Mit freundlichen Grüßen,
Kind regards,
Matthias Beyer

Proudly sent with mutt.
Happily signed with gnupg.


A lightuserdata is a black box void pointer. It can not have an individual metatable set[1] and is not garbage collected; this is stated in the manual [2] [3]


>>Is this piece of code the correct approach?

Yes. 
It seems you are using C in your code but I am also unsure if you should be freeing "mem" here or is it stored somewhere in C? Also IIRC MSC does not allow implicit casts from a void pointer, so that is something to be aware of if you wanted the code to be cross platform.

[1] Setting a metatable on a light userdata sets it for all light userdata
[2] http://www.lua.org/manual/5.2/manual.html#2.1
[3] http://www.lua.org/manual/5.2/manual.html#lua_pushlightuserdata

--Liam