lua-users home
lua-l archive

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

> -----Original Message-----
> From: [] On Behalf Of Tony Finch
> Sent: Thursday, May 19, 2011 2:49 AM
> To: Lua mailing list
> Subject: Re: Flushing global variables
> > I'm working on an application that's multi-threaded, and to avoid a
> > newstate-load-pcall-close series for every time I want to invoke a
> > script, I'm thinking of allocating a pool of states and just pulling one
> > out when it's time to run, which leaves me with just load-pcall every
> > time.
> Why not cache the parsed scripts, so it's just a pcall each time?

I do, actually; there's a call to lua_load() and then one to lua_dump() to get the compiled form back out.  Then when it's time to run the script, I lua_load() the compiled form and then use lua_pcall() to run it.  Thus, it only gets parsed once.

> > One thing I'm not certain about with that design: Will the garbage
> > collector purge any global variables that exist in the state on
> > completion of the pcall, or will they linger around for the next
> > load-pcall on the same state object?  If the latter, can I use some
> > getfenv/setfenv magic to reset things for the next iteration?  Or do I
> > have to bite the bullet and do it the more expensive way?
> Yes, it sounds like you want some kind of setfenv sandbox. You can use a
> __index metatable entry on the temporary environment to pull in the
> persistent global functions efficiently.

Thanks, I'll check into this (and the other suggestions).