lua-users home
lua-l archive

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

Well, the code is in pascal but should translate to C easily enough:

function lua_unrequire( L : PLua_State ) : Integer; cdecl;
  ModuleName : AnsiString;
  n : Integer;
  n := lua_gettop(L);
  if (n <> 1) or (lua_type(L, 1)<>LUA_TSTRING) then
// Get the packages table
  ModuleName := AnsiString(lua_tostring(L, 1));
  lua_pushliteral(L, 'package');
  lua_gettable(L, LUA_GLOBALSINDEX);
// Get the loaded sub table
  lua_pushliteral(L, 'loaded');
  lua_gettable(L, -2);
// Set the module name to nil within the loaded sub table
  lua_pushstring(L, PChar(ModuleName));
  lua_settable(L, -3);
// Remove unused stuff from the stack
  lua_pop(L, 2);
// Get the module pointer within the lua globals space and set it to nil
  lua_pushstring(L, PChar(ModuleName));
  lua_settable(L, LUA_GLOBALSINDEX);
// cleanup after ourselves
  lua_pop(L, 1);
  result := 0;

Thats all from the top of my head and hasn't been tested but it should work out to exactly the same thing as your Lua code.

 - Jeremy

On 9/6/07, RJP Computing <> wrote:
On 9/6/07, Daniel Collins <> wrote:
> > I was bebugging the changes in my script and noticed I letf some
> > unneeded code in my module functions, so I removed it and
> "Reloaded"
> > my main script, that includes the modules, using require().
> I think you need to unrequire the modules. See

What a coincidence! I was just about to reply to the original post when
you posted a message of mine from last year :-)

I can add that my unrequire function was slightly different to the one
in the earlier post:

function unrequire(m)
        package.loaded[m] = nil
        _G[m] = nil

What about a 'C' api way of doing this?
RJP Computing


"Help I suffer from the oxymoron Corporate Security."