lua-users home
lua-l archive

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


Thanks to Luiz, Roberto and co for the update.

On the loadlib() front, I have done some WIndows testing and the question
I have is whether require() and loadlib() should parse and lower case
the name before
putting the name in the registry. Windows (and others) is case insensitive for
filenames so 

require 'mylib'
and
require 'Mylib' 

both refer to the same library yet will cause 2 different LoadLibrary()s.

loadlib('.\\ltest','luaopen_test')
and
loadlib('ltest','luaopen_test')

have a similar effect on the handles in the handles table.

Methinks that the saved library handles would be better in a registry table
than as prefixed names in the registry. As a separate table the table can
be iterated.

The code I used (fragment) is

#define LIBHANDLE "_LOADLIBH"

static const char* pushfname(lua_State* L, const char* path)
{
    char lwrname[MAX_PATH];
    const char* name = strrchr(path, '\\');
    if (!name) name = path; else name++;
    strcpy(lwrname, name);
    strlwr(lwrname);
    lua_pushlstring(L,name, strcspn(lwrname,"\\,./"));
    return lua_tostring(L,-1);
}

/* this function adds 1 to the stack */
static void **ll_register (lua_State *L, const char *path) {
  void **plib;
  lua_pushliteral(L, LIBHANDLE);
  lua_rawget(L,LUA_REGISTRYINDEX);
  if (!lua_istable(L,-1)) {lua_pop(L,1);return NULL;}
  path=pushfname(L, path);
  lua_pushvalue(L, -1);
  lua_gettable(L, -3);   /* check library in registry? */
  if (!lua_isnil(L, -1)) { /* is there an entry? */
    plib = (void **)lua_touserdata(L, -1);
    lua_replace(L,-3);
    lua_pop(L,1);
  }
  else {  /* no entry yet; create one */
    lua_pop(L, 1);
    plib = (void **)lua_newuserdata(L, sizeof(const void *));
    *plib = NULL;
    luaL_getmetatable(L, "_LOADLIB");
    lua_setmetatable(L, -2);
	lua_pushvalue(L,-1);
	lua_insert(L,-4);
	lua_rawset(L, -3);
    lua_pop(L, 1);
  }
  return plib;
}


DB