[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Per-thread environment
- From: Steven Johnson <steve@...>
- Date: Fri, 11 Jan 2013 15:56:25 -0600
Hi.
Not built into the language, but I do something along those lines:
https://github.com/ggcrunchy/corona-sdk-snippets/blob/master/per_coroutine_ops.lua
And registering a time function, toward the top:
https://github.com/ggcrunchy/corona-sdk-snippets/blob/master/main.lua
My own use case is "how much time left, on this frame?" for a bunch of
game AI agents. I'm not sure this repo actually demonstrates it in
action (it's excerpted from a larger private project), but basically
what you're asking is what I do.
On 1/11/13, Ivo Beltchev <ivo@ibeltchev.com> wrote:
> I now realize how silly my implementation is. It will never be called for
> existing global variables. It should be more like:
>
> __index=function(_,n)
>
> local env=threadEnvironments[coroutine.running()] -- a table that keeps
> custom environments for select threads
>
> return env and env[n]
>
> end
>
>
>
>
>
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Ivo Beltchev
> Sent: Friday, January 11, 2013 9:53 AM
> To: lua-l@lists.lua.org
> Subject: Per-thread environment
>
>
>
> Hi
>
>
>
> It looks to me from the documentation that environments are tied to a
> function. So any time a function is being called, it has access to that
> environment.
>
> Is it possible to associate an environment with a thread instead? So if a
> function is executed in that thread it will use the thread's environment.
>
>
>
> Basically I'm trying to simulate TLS. I want any access to a TLS variable
> to
> use the local environment, but when accessing a global variable (a variable
> not in the current environment), I want to access the real global variable.
>
>
>
> One way I can do this manually is to manipulate the metatable of _G:
>
> __index=function(g,n)
>
> local env=threadEnvironments[coroutine.running()] -- a table that keeps
> custom environments for select threads
>
> if env and env[n] then
>
> return env[n]
>
> end
>
> return rawget(g,n)
>
> end
>
>
>
> and something similar for __newindex
>
>
>
> Is there something like that already built into the language?
>
>
>
> Ivo
>
>