lua-users home
lua-l archive

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


Issac Trotts wrote:



On 10/20/05, *Brian Murphy* <brian@murphy.dk <mailto:brian@murphy.dk>> wrote:

    Issac Trotts wrote:

    > Hi all,
    >
    > The installation instructions for luasocket describe how to
    install it
    > as a dynamic library.  For my application, it would be preferable to
    > just compile luasocket in statically, but this only half
    working.  I
    > had to change a couple of headers:
    >
    Try not changing anything and putting this in your startup code:

        /* package.preload["lsocket"] = luaopen_lsocket */
        lua_pushstring(L, "lsocket");
        lua_pushcfunction(L, luaopen_lsocket);
        lua_settable(L, -3);

        /* package.preload["lmime"] = luaopen_lmime */
        lua_pushstring(L, "lmime");
        lua_pushcfunction(L, luaopen_lmime);
        lua_settable(L, -3);

    this gets the C initialization functions called at the right stage of
    the module
    loading process(after the lua code). If you the "C" functions directly
    then the later require"socket" fails because the module globals are
    already defined and this
    is what the package system uses to signal that the module is
    already loaded.


OK, thanks, I'll give that a try.

Issac



Nobody found my deliberate mistake ;).
Here is some code that will actually work:

   lua_newtable(L);
   lua_pushstring(L, "package");
   lua_pushvalue(L, -2);
   lua_settable(L, LUA_GLOBALSINDEX); /* set the global "package" to be
                                         the new table */
   lua_newtable(L);
   lua_pushstring(L, "preload");
   lua_pushvalue(L, -2); /* copy new table */
   lua_settable(L, -4); /* set package.preload to the new table */
   lua_remove(L, -2); /* remove "package" table */

   /* package.preload["lsocket"] = luaopen_lsocket */
   lua_pushstring(L, "lsocket");
   lua_pushcfunction(L, luaopen_lsocket);
   lua_settable(L, -3);

   /* package.preload["lmime"] = luaopen_lmime */
   lua_pushstring(L, "lmime");
   lua_pushcfunction(L, luaopen_lmime);
   lua_settable(L, -3);

sorry for the inconvenience :).

/Brian