lua-users home
lua-l archive

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


Hi,

I am in the process of defining how LuaSocket is to be packaged.
I read the wiki entries on the subject and felt there were some gaps.
Since I am not an authority, I decided to bother you people and see if
we can reach a consensus. My three concerns are:

    - namespace independency
    - library-name and entrypoint-name OS dependencies
    - transparent static and dynamic options

For the namespace independency, what I am doing is to return the
namespace created by the library, instead of setting it to some global.
You never know if the user will feel offended if you polutes his
globals. The require function returns whatever the chunk it loads
returned, so user code can look like this:

    local socket = require("socket")
    local http = require("http")
    local url = require("url")

I kind of like the idea, and I don't remember if that was the obvious
way or not (probably was, otherwise why would the Lua team have require
return a the value?).

For the library-name and entrypoint name OS independence, I am using two
environment variables, and a modified loadlib function:

    local _loadlib = loadlib

    LUA_LIBNAME = LUA_LIBNAME or os.getenv("LUA_LIBNAME") or "?"
    LUA_FUNCNAME = LUA_FUNCNAME or os.getenv("LUA_FUNCNAME") or "?"

    function loadlib(a, b)
        if a then a = string.gsub(LUA_LIBNAME, "%?", a) end
        if b then b = string.gsub(LUA_FUNCNAME, "%?", b) end
        return _loadlib(a, b)
    end

That way, the instalation can set these variables and loadlib will work
the same way on windows, mac os and linux at least. On mac, I use

    LUA_FUNCNAME="?" -- used to be "_?"
    LUA_LIBNAME="/usr/local/lib/lua/5.0/?.dylib"

on Windows, I use

    LUA_FUNCNAME="?"
    LUA_LIBNAME="h:\lib\lua\5.0\?.dll"

And in the smtp module, for instancce, which needs some C code, I run

    local open = assert(loadlib("smtp", "luaopen_smtp"))
    local smtp = assert(open())

which works fine on both systems. The luaopen_smtp leaves the namespace
table on the stack, so that the Lua code can receive it.

My question is: what if the user wants to link these things statically?
Both the smtp C part and the smtp Lua part? It would be cool to have
code that is independent of that.

[]s,
Diego.