lua-users home
lua-l archive

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


One question, for which I am sure the answer is in the archives.

Why did you not do?

bc = loadlib(library_path);

i.e. so that loadlib returns the function table rather than 
internally allocates it. And why not have a designated initialization
function name?

Just curious.


+++++++++++++++++++++++++++++++++
>>Do we have a convention for naming the init() function with loadlib()?
>>
>>e.g. fnc=loadlib(mylib,initfunction)
>>
>>whats a good name for 'initfunction' ?
>
>The convention used in the standard Lua libraries is "luaopen_xxx" where
>"xxx" identifies the library, that is, is the name of the table it exports.
>
>Here is a few names of the libraries I'm working on now (my own projects,
>not official Lua libraries):
>
> alarm/lalarm.c  LUALIB_API int luaopen_alarm (lua_State *L)
> bc/lbc.c        LUALIB_API int luaopen_bc (lua_State *L)
> gdbm/lgdbm.c    LUALIB_API int luaopen_gdbm (lua_State *L)
> mapm/lmapm.c    LUALIB_API int luaopen_mapm (lua_State *L)
> pdf/lpdf.c      LUALIB_API int luaopen_pdf (lua_State *L)
> posix/lposix.c  LUALIB_API int luaopen_posix (lua_State *L)
>
>Note that the library is named lxxx.so. Moreover, I'm using a xxx.lua
>file that loads the .so and adds support code if necessary. This reads
>nicely with -l in the standalone interpreter. Here is a sample:
>
> -- bc.lua
> -- support code for bc library
> -- usage lua -lbc ...
>
> local function so(x)
>	 local SOPATH= os.getenv"LUA_SOPATH" or "./"
>	 assert(loadlib(SOPATH.."l"..x..".so","luaopen_"..x))()
> end
>
> so"bc"
> do
>  local T=bc.metatable
>  T.__add=bc.add
>  T.__sub=bc.sub
>  T.__mul=bc.mul
>  T.__div=bc.div
>  T.__pow=bc.pow
>  T.__lt=function (a,b) return bc.compare(a,b)<0 end
>  T.__eq=function (a,b) return bc.compare(a,b)==0 end
>  T.__unm=function (a) return bc.sub(0,a) end
>  T.__tostring=bc.tostring
> end
>
>--lhf