lua-users home
lua-l archive

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

Hi Diego and list,

> You can use the package.preload table for this.
> Make sure you add an entry package.preload["socket.core"],
> whose value is the C function (use lua_pushcfunction)
> luaopen_socket_core.
> Do the same with package.preload["mime.core"] and the function
> luaopen_mime_core. When you call require(), it will not try to
> load the shared library.

Hmm - in the build script that I'm
using ( I
invoke patch to change the array lualibs[] in lua.c to this:

  static const luaL_reg lualibs[] = {
    {"base", luaopen_base},
    {"table", luaopen_table},
    {"io", luaopen_io},
    {"string", luaopen_string},
    {"math", luaopen_math},
    {"debug", luaopen_debug},
    {"loadlib", luaopen_loadlib},
    /* Edrx: */
    {"mime", luaopen_mime_core},
    {"socket", luaopen_socket_core},
    /* add your libraries here */
    {NULL, NULL}

(actually I call the modified lua.c luasocket.c). So my guess in
that it should be enough to add no-op preload functions to the
package.preload table, like this:

  function vluasocket-e () {
    LUA_INIT="@$HOME/.lua50/compat-5.1.lua" \
     LUA_PATH="$HOME/.lua50/?.lua;?.lua"    \
    LUA_CPATH="$HOME/.lua50/?.dll;?.dll"    \
    ~/usrc/lua-5.0.2/bin/luasocket -e "$1"

  vluasocket-e '
    package.preload["socket.core"] = function () end
    package.preload["mime.core"]   = function () end
    socket = require "socket"
    for k,v in socket do print(k, v) end
    print("socket.try:", socket.try)

However the output that I get is this (modulo indentation!),
indicating that the binary part of socket.c is in and has been
run, but socket.lua hasn't been loaded...

  protect   function: 20011548
  dns       table: 20011738
  gettime   function: 20011588
  udp       function: 20013608
  newtry    function: 200115c8
  _VERSION  LuaSocket 2.0
  __unload  function: 20011508
  select    function: 20013ae8
  tcp       function: 20011b48
  skip      function: 20010fa8
  sleep     function: 200116f8
  socket.try:  nil

I'll try to work more on that after lunch, but I though that this
was interesting enough to deserve an e-mail... Any further hints?
Maybe a clean way to replace `require "socket"' by a call to
`loadfile', with adjustments to the "package" tables?


P.S.: I know much less about packages and modules than I