Getting metatable's name.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting metatable's name.

Laurent FAILLIE
Hello,

I'm associating a metatable to user data using following code

-----
static const struct luaL_Reg SelTimedColLib [] = {
    {"Create", stwcol_create},
    {NULL, NULL}
};

static const struct luaL_Reg SelTimedColM [] = {
    {"Push", stwcol_push},
    {"MinMax", stwcol_minmax},
    {NULL, NULL}
};


libSel_objFuncs( L, "SelTimedWindowCollection", SelTimedColM);
libSel_libFuncs( L, "SelTimedWindowCollection", SelTimedColLib );
-----

with libSel_libFuncs() like that

-----
int libSel_objFuncs( lua_State *L, const char *name, const struct luaL_Reg *funcs){
    luaL_newmetatable(L, name);
    lua_pushstring(L, "__index");
    lua_pushvalue(L, -2);
    lua_settable(L, -3);    /* metatable.__index = metatable */

    if(funcs){    /* May be NULL if we're creating an empty metatable */
#if LUA_VERSION_NUM > 501
        luaL_setfuncs( L, funcs, 0);
#else
        luaL_register(L, NULL, funcs);
#endif
    }

    return 1;
}
----

and then

----
    struct SelTimedWindowCollection *col = (struct SelTimedWindowCollection *)lua_newuserdata(L, sizeof(struct SelTimedWindowCollection));

    luaL_getmetatable(L, "SelTimedWindowCollection");
    lua_setmetatable(L, -2);
----


So, now, from Lua's side,  how to retrieve metatable's name ???? (in my case, "SelTimedWindowCollection")

I tried to test against the metatable itself but it doesn't work :

print( getmetatable(col.getCollection()), SelTimedWindowCollection )
-> table: 0xb79650d8       table: 0xb7965100

Thanks for your help

Laurent


Reply | Threaded
Open this post in threaded view
|

Re: Getting metatable's name.

Sean Conner
It was thus said that the Great Laurent FAILLIE once stated:
>
> So, now, from Lua's side,  how to retrieve metatable's name ???? (in my case, "SelTimedWindowCollection")
> I tried to test against the metatable itself but it doesn't work :
> print( getmetatable(col.getCollection()), SelTimedWindowCollection )-> table: 0xb79650d8       table: 0xb7965100

  Starting with Lua 5.3, the metatable will have a __name field, as
mentioned in the documentation for luaL_newmetatable().  For earlier
versions of Lua, you will have to call debug.getregistry() and manually scan
it for the name.

        mt = getmetatable(thing)
        reg = debug.getregistry()
        for name,value in pairs(reg) do
          if value == mt then
            print(name)
            break
          end
        end

  -spc
Reply | Threaded
Open this post in threaded view
|

Re: Getting metatable's name.

Luiz Henrique de Figueiredo
In reply to this post by Laurent FAILLIE
> So, now, from Lua's side,  how to retrieve metatable's name ???? (in my case, "SelTimedWindowCollection")

If the metatable was created with luaL_newmetatable in Lua 5.3+, then
use luaL_getmetafield(L, arg, "__name").
Reply | Threaded
Open this post in threaded view
|

Re: Getting metatable's name.

Laurent FAILLIE
In reply to this post by Sean Conner

Thanks for your reply.
Any issue if in the metatable creation code, I'm adding manually a field __name when under < 5.3 ?

So the remaining of the code will be the same  whatever Lua's version :)


Le dimanche 28 février 2021 à 00:48:51 UTC+1, Sean Conner <[hidden email]> a écrit :


It was thus said that the Great Laurent FAILLIE once stated:
>
> So, now, from Lua's side,  how to retrieve metatable's name ???? (in my case, "SelTimedWindowCollection")
> I tried to test against the metatable itself but it doesn't work :
> print( getmetatable(col.getCollection()), SelTimedWindowCollection )-> table: 0xb79650d8       table: 0xb7965100

  Starting with Lua 5.3, the metatable will have a __name field, as
mentioned in the documentation for luaL_newmetatable().  For earlier
versions of Lua, you will have to call debug.getregistry() and manually scan
it for the name.

    mt = getmetatable(thing)
    reg = debug.getregistry()
    for name,value in pairs(reg) do
      if value == mt then
        print(name)
        break

      end
    end


  -spc

Reply | Threaded
Open this post in threaded view
|

Re: Getting metatable's name.

Luiz Henrique de Figueiredo
> Any issue if in the metatable creation code, I'm adding manually a field __name when under < 5.3 ?
>
> So the remaining of the code will be the same  whate

Just adding  __name to metatables won't have any effect if no code uses it.
In particular, see luaL_typeerror in 5.4 or typeerror in 5.3,
luaL_tolstring, and luaT_objtypename.

It'll be fine to change your lauxlib.c to handle __name, though,
because lauxlib.c is a userland library.