lua-users home
lua-l archive

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


Hi,

For loading (OS dependent) libraries, i believe a function like
'require(...)' would help.

Some time ago i wrote a 'requireso' function for use at home. Just like
require uses LUA_PATH (first looking at a global string and later for an
environment variable) it would use LUA_SOPATH.

http://mega.ist.utl.pt/~tngd/lua/requireso.lua
This will create a new global function 'requireso', and does not need any
change to be made to lua itself.

The syntax of the function is not different from what you proposed:
requireso(<module name:string>, <function name:string>, <load:bool>)

Basic usage would be like this:
LUA_SOPATH=/usr/lib/lua5/?.so;/home/name/lua5/?.so;./?.so

and in lua:
  local socket = requireso('socket', 'luaopen_socket', true)

If an error occours when i tell the function to be loaded right away, an
error would be raised with a minimal descritive message.

If i don't pass load with a true value, the values returned by loadlib are
returned intact.

Tiago



On Fri, 4 Jun 2004, Diego Nehab wrote:

> Hi,
>
> ...
>
> 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.
>