lua-users home
lua-l archive

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


On Tue, 7 May 2013 17:41:27 -0700 (PDT)
Are Leistad <aleistad@telia.com> wrote:

> Hi!
> 
> I'm just tried compiling some ols Lua libraries. I understand that
> luaL_openlib has been deprecated for a while, and that luaL_newlib is
> now the preferred quick method for registering libraries. I've got my
> old libraries to compile using LUA_COMPAT_MODULE, so I'm good. 
> 
> However, I haven't found out why luaL_openlib and luaL_register is
> out of the API now. These functions let us register a set of library
> functions with a prefix in a simple way, conveniently giving the
> "mylibprefix.myfunc" syntax. I've spent some time searching and
> browsing, trying to find out why they were deprecated and how to
> achieve the same result now, but nothing obvious has popped up yet.
> Thus I have some questions: 
> - why is are these functions now gone? 
> - is there a simple way to register a library with the
> "mylibprefix.myfunc" syntax, i.e. something equivalent of
> luaL_register?
> - are there any reasons to avoid LUA_COMPAT_MODULE?
> 
> I've may very well have missed the obvious here...
> 
> TIA!
> 
> Are
> --
> 
> 
> 
> --
> View this message in context:
> http://lua.2524044.n2.nabble.com/Why-did-luaL-openlib-luaL-register-go-tp7649023.html
> Sent from the Lua-l mailing list archive at Nabble.com.
> 

luaL_register is gone because it encouraged developers to place their
libraries in the global namespace, which tends to be a bad idea.
module() was removed for the same reasons. The convention that Lua
library developers are moving towards is to return the library table
from the routine, so you may assign it to a variable in the calling
environment.  For example:

    /* in C land */
    int luaopen_mylib(lua_State *L)
    {
       lua_newtable(L);
       /* load it up with stuff */
       return 1;
    }

    -- in Lua land
    local mylib = require 'mylib'
    mylib.hello()

-Rob

Attachment: signature.asc
Description: PGP signature