lua-users home
lua-l archive

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


I have an application that executes Lua script using an environment that
is a temporary table. This environment has lifetime of one or two
transactions and then is destroyed.

In lua code (the actual app is in C), before we execute code we something like:

  env ={}
  env.obj = create_an_object()
  env.obj2 = create_an_object2()
  ...
  setfenv(1, setmetatable(env, {_index = _G }))

So we end up with an environment that has lots of userdatas that
get used for subsequent Lua code.

My problem is this:

if the code that I am executing does a require"somemodule" then
the environment of the functions in the module are set at require()
time, that is when the code is loaded.  I have no obvious way
of resetting the environment of the required()ed functions as my
transient environment changes.

One option would be to use (in the require()ed code something like
setting the environment to have __index and __newindex to
functions that somehow dynamically find the environment of the
caller.

  setfenv(1, setmetatable(env, {_index = function_that_finds_env()}))

Anyone care to help?

David B.