lua-users home
lua-l archive

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

On 22 Aug 2006, at 02:25, D Burgess wrote:

OK, David Jones was right (thank you) and I made an incredibly
silly assumption. If I might restate the problem:

I have a number of Lua threads that each have their own environment,
I have *a* function which will execute in each of the threads,
I wish to set the function environment (lua_setfenv()) of the function
to the thread environment.
How do I do this without doing a lua_load() for *each* thread?
Is there anyway (from C) having initially loaded the function to
create a separate closure for each thread?

I was going to spend some more time looking at this, but I see you've made some progress before I found the time.

If the function you want to execute _is_ the entire script loaded by lua_load then you can't do what you want. You must use lua_load to get a fresh instance of the function (this is a small drawback in Lua I think).

However, I think wrapping your scripts should work:
return function(...)
-- original script goes here

Then, you can lua_load the script once and lua_call it each time you want a fresh function instance. That'll be dead fast.

If you don't want to edit all your scripts, you should be able to use a custom loader to add the "return function(...)" prefix and the "end" suffix.


David Burgess

David Jones wrote:
> Isn't this explained by there only being one function that is shared
> by all your threads?