lua-users home
lua-l archive

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


On 09.04.2011 14:37, Anthony Howe wrote:
Q: Essentially how to force Lua application&  library functions to
inherit the environment of the thread or caller (not the environment of
where it was defined).
Ideally I need this to work in 5.1 and 5.2.

Here are my thoughts:

1) For threads in 5.2:

Change coroutine.resume (and coroutine.wrap) to fetch a table corresponding to the thread and set it as registry[LUA_RIDX_GLOBALS].

2) In support functions you can refer to registry[LUA_RIDX_GLOBALS] when you need to manipulate a global variable.

3) For user code there's no way to do the "fork" without actually cloning closures. Take this code for example:
local was_called
function event_handler()
  if not was_called then
    -- do something
    was_called = true
  end
end

No matter what you do, it would do something only for the first thread that invoked it. Thus, I'd suggest having an explicit global variable with thread specific data.

--
Best regards,
Sergey Rozhenko                 mailto:sergroj@mail.ru