lua-users home
lua-l archive

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


Thank you, very interesting ... but I first have to sleep over this
info, which is new to me :). I as Lua-newbee cannot really contribute
further in this discussion, please excuse.

On Wed, Feb 23, 2022 at 2:03 AM 云风 Cloud Wu <cloudwu@gmail.com> wrote:
>
> Flyer31 Test <flyer31@googlemail.com> 于2022年2月22日周二 23:12写道:
> >
> > But if you connect "normal C userdata" to metatables, you can also do
> > this without library, as e. g. described in Roberto's Programming in
> > Lua, Caphter 31, "User defined types in C" for this new bitarray type.
> >
> > Just what you definitely seem to need here is a code in the following form:
> >
> >    luaL_newmetatable(L, "my-metatable-name");
> > ...
> >     lua_newuserdata(L, nbytes);
> >     luaL_getmetatable(L, "my-metatable-name");
> >     lua_setmetatable(L, -2);
> >
> > ... so this would connect the metatable to the userdata element
> > defined with nbytes Bytes... .
> >
> > This does NOT need any library functions, this is just standard Lua c  code... .
> >
> > But how would you do this connection in case of lightuserdata element?
>
> Anything in Lua can has a matatable. As the Lua manual says "Tables
> and full userdata have individual metatables, although multiple tables
> and userdata can share their metatables. Values of all other types
> share one single metatable per type; that is, there is one single
> metatable for all numbers, one for all strings, etc. "
>
> For lightuserdata :
>
> lua_pushlightuserdata(L, NULL);
> luaL_getmetatable(L, "my_lightuserdata_metatable");
> lua_setmetatable(L, -2);
>
> But all the lightuserdata share the same one metatable, if more than
> one library does this, only one metatable works. If Lua support
> subtype of lightuserdata would be better, and then we can do :
>
> lua_pushlightuserdata(L, NULL, "mytype");
> luaL_getmetatable(L, "mytype_metatable");
> lua_setmetatable(L, -2);
>
> --
> http://blog.codingnow.com