http://lua-users.org/wiki/ObjectProperties - one question

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

http://lua-users.org/wiki/ObjectProperties - one question

Milan Obuch
Hi,

I used this page for inspiration and tried something OOP-like in C
calling Lua environment. While it basically works (in some simple
scenario), there is a question - compiling this with Lua 5.2 gives

test-lua.c:135:2: warning: implicit declaration of function 'luaL_openlib' is invalid in C99 [-Wimplicit-function-declaration]

and I can't find luaL_openlib function in Lua 5.2 Reference manual. How
did this happen?

Regards,
Milan
Reply | Threaded
Open this post in threaded view
|

Re: http://lua-users.org/wiki/ObjectProperties - one question

Luiz Henrique de Figueiredo
> test-lua.c:135:2: warning: implicit declaration of function 'luaL_openlib' is invalid in C99 [-Wimplicit-function-declaration]

Compile your program with -DLUA_COMPAT_MODULE
Reply | Threaded
Open this post in threaded view
|

Re: http://lua-users.org/wiki/ObjectProperties - one question

Milan Obuch
On Tue, 9 Jun 2020 10:21:33 -0300, Luiz Henrique de Figueiredo
<[hidden email]> wrote:

> > test-lua.c:135:2: warning: implicit declaration of function
> > 'luaL_openlib' is invalid in C99 [-Wimplicit-function-declaration]  
>
> Compile your program with -DLUA_COMPAT_MODULE

Thank you, this works. In lauxlib.h I see this is for compatibility
with old module system, is there some replacement in new Lua world
without module?

Regards,
Milan
Reply | Threaded
Open this post in threaded view
|

Re: http://lua-users.org/wiki/ObjectProperties - one question

Milan Obuch
On Tue, 9 Jun 2020 15:47:13 +0200, Milan Obuch <[hidden email]> wrote:

> On Tue, 9 Jun 2020 10:21:33 -0300, Luiz Henrique de Figueiredo
> <[hidden email]> wrote:
>
> > > test-lua.c:135:2: warning: implicit declaration of function
> > > 'luaL_openlib' is invalid in C99
> > > [-Wimplicit-function-declaration]    
> >
> > Compile your program with -DLUA_COMPAT_MODULE  
>
> Thank you, this works. In lauxlib.h I see this is for compatibility
> with old module system, is there some replacement in new Lua world
> without module?
>

No one?

Also, additional question - following definition

#define ARRAY_REGID  "22d3fa81-aef3-4335-be43-6ff037daf78e"

used later in

luaL_newmetatable(L, ARRAY_REGID);

Looking in Reference Manual, second argument is just char pointer, so I
could basically use any name, it does not need to be such special
string as used in cited example, right? Even meaningfull ones as 'dog',
'cat' etc. yes?

Also, there is mention of a registry, where new metatable is being put,
but I did not find any definition of this registry. Maybe I did not
look into the right place, but where could definition of registry be
found?

Regards,
Milan
Reply | Threaded
Open this post in threaded view
|

Re: http://lua-users.org/wiki/ObjectProperties - one question

Andrew Gierth
In reply to this post by Milan Obuch
>>>>> "Milan" == Milan Obuch <[hidden email]> writes:

 >> > test-lua.c:135:2: warning: implicit declaration of function
 >> > 'luaL_openlib' is invalid in C99 [-Wimplicit-function-declaration]  
 >>
 >> Compile your program with -DLUA_COMPAT_MODULE

 Milan> Thank you, this works. In lauxlib.h I see this is for
 Milan> compatibility with old module system, is there some replacement
 Milan> in new Lua world without module?

See luaL_newlib or luaL_setfuncs. It is not expected any more for
modules to either manipulate their environment or add themselves into
any global table; it's the responsibility of require() to add the module
to package.loaded, the module's luaopen_* function should just return a
value on the stack.

--
Andrew.
Reply | Threaded
Open this post in threaded view
|

Re: http://lua-users.org/wiki/ObjectProperties - one question

Andrew Gierth
In reply to this post by Milan Obuch
>>>>> "Milan" == Milan Obuch <[hidden email]> writes:

 Milan> Also, additional question - following definition

 Milan> #define ARRAY_REGID  "22d3fa81-aef3-4335-be43-6ff037daf78e"

 Milan> used later in

 Milan> luaL_newmetatable(L, ARRAY_REGID);

See, this is why I don't like luaL_newmetatable / luaL_checkudata. (I
prefer to use userdata keys rather than string keys where uniqueness
matters.)

 Milan> Looking in Reference Manual, second argument is just char
 Milan> pointer, so I could basically use any name, it does not need to
 Milan> be such special string as used in cited example, right? Even
 Milan> meaningfull ones as 'dog', 'cat' etc. yes?

It can be anything that does not collide with any value that some other
module might use. Since you don't know what other modules might use, you
have to guess; in this case the author obviously decided that a UUID
would be the safest bet.

 Milan> Also, there is mention of a registry, where new metatable is
 Milan> being put, but I did not find any definition of this registry.
 Milan> Maybe I did not look into the right place, but where could
 Milan> definition of registry be found?

The registry is a Lua table associated with the lua state that holds
data that is of interest at the C level but which shouldn't normally be
exposed to Lua code. You can get at it from Lua only with
debug.getregistry(), but C code can access it at any time using the
pseudo-index LUA_REGISTRYINDEX.

Typical stuff in the registry is:

  - the original global table reference

  - the thread object for the main thread

  - metatables for built-in objects such as LUA_FILEHANDLE, and also
    for any objects that use luaL_newmetatable / luaL_checkudata

  - references created with luaL_ref

  - the _LOADED table, which is also visible by default at
    package.loaded, but it's the registry reference which is actually
    definitive

  - a table of dynamically loaded objects, on platforms that support
    those

  - anything that any C module decided to put there (which can often
    be quite extensive)

See  https://www.lua.org/manual/5.3/manual.html#4.5

--
Andrew.
Reply | Threaded
Open this post in threaded view
|

Re: http://lua-users.org/wiki/ObjectProperties - one question

Milan Obuch
In reply to this post by Andrew Gierth
On Wed, 10 Jun 2020 09:38:22 +0100, Andrew Gierth
<[hidden email]> wrote:

> >>>>> "Milan" == Milan Obuch <[hidden email]> writes:  
>
>  >> > test-lua.c:135:2: warning: implicit declaration of function
>  >> > 'luaL_openlib' is invalid in C99
>  >> > [-Wimplicit-function-declaration]    
>  >>
>  >> Compile your program with -DLUA_COMPAT_MODULE  
>
>  Milan> Thank you, this works. In lauxlib.h I see this is for
>  Milan> compatibility with old module system, is there some
>  Milan> replacement in new Lua world without module?  
>
> See luaL_newlib or luaL_setfuncs. It is not expected any more for
> modules to either manipulate their environment or add themselves into
> any global table; it's the responsibility of require() to add the
> module to package.loaded, the module's luaopen_* function should just
> return a value on the stack.
>

Currently I am creating metatable with some method like this:

luaL_newmetatable(L,TABLE_ID);
luaL_openlib(L,NULL,table_instance_methods,0);
lua_pop(L,1);

I can then associate this metatable with some variable (I use light
userdata) and this could be used for simple OOP-like programming.

I just could not achive the same effect with luaL_newlibtable and
luaL_setfuncs... I can live with older compatibility solution for now,
but if this example could be seen elsewhere done in new way, I'd like
to test it.

I am still learning Lua, but it is interesting and it looks to me like
quite well designed and engineered language.

Regards,
Milan
Reply | Threaded
Open this post in threaded view
|

Re: http://lua-users.org/wiki/ObjectProperties - one question

Andrew Gierth
>>>>> "Milan" == Milan Obuch <[hidden email]> writes:

 Milan> Currently I am creating metatable with some method like this:

 Milan> luaL_newmetatable(L,TABLE_ID);
 Milan> luaL_openlib(L,NULL,table_instance_methods,0);
 Milan> lua_pop(L,1);

 Milan> I can then associate this metatable with some variable (I use
 Milan> light userdata) and this could be used for simple OOP-like
 Milan> programming.

 Milan> I just could not achive the same effect with luaL_newlibtable
 Milan> and luaL_setfuncs...

    luaL_newmetatable(L, TABLE_ID);
    luaL_setfuncs(L, table_instance_methods, 0);
    lua_pop(L, 1);

--
Andrew.
Reply | Threaded
Open this post in threaded view
|

Re: http://lua-users.org/wiki/ObjectProperties - one question

Milan Obuch
On Wed, 10 Jun 2020 13:35:31 +0100, Andrew Gierth
<[hidden email]> wrote:

> >>>>> "Milan" == Milan Obuch <[hidden email]> writes:  
>
>  Milan> Currently I am creating metatable with some method like this:
>  Milan>  
>
>  Milan> luaL_newmetatable(L,TABLE_ID);
>  Milan> luaL_openlib(L,NULL,table_instance_methods,0);
>  Milan> lua_pop(L,1);  
>
>  Milan> I can then associate this metatable with some variable (I use
>  Milan> light userdata) and this could be used for simple OOP-like
>  Milan> programming.  
>
>  Milan> I just could not achive the same effect with luaL_newlibtable
>  Milan> and luaL_setfuncs...  
>
>     luaL_newmetatable(L, TABLE_ID);
>     luaL_setfuncs(L, table_instance_methods, 0);
>     lua_pop(L, 1);
>

Thank you. Seems to be working. I think this should be put onto wiki as
more current solution, taking into account the change is actually
minimal, as I see it now.

Regards,
Milan