lua-users home
lua-l archive

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


Is that code thread safe? My read on this is that you are maintaining a
single global table for dynamic variables.

I agree that searching the stack is not exactly speedy, but stacks aren't
necessarily all that deep and my actual needs are akin to dynamic variables
and could be implemented using them but aren't necessarily identical. (See
my other message.)

Mark

on 12/18/03 8:04 AM, RLake@oxfam.org.pe at RLake@oxfam.org.pe wrote:

> Mark Hamburg preguntó:
> 
>> Is there a way other than wrapping things in pcall to tell what the
> maximum
>> level I can pass to getfenv is?
> 
>> I'm trying to implement pseudo-dynamic scoping by searching up the call
>> chain for the first function environment referencing a particular
> variable.
> 
> This seems like a very slow way of implementing dynamic scoping. Why don't
> you do something like this:
> 
> do
> local dynvars = {}
> setmetatable(getfenv(), {
>   __index = dynvars,
>   __newindex = function(t, k, v)
>       if dynvars[k] ~= nil then
>         dynvars[k] = v
>        else
>         rawset(t, k, v)
>       end
>     end
> }
> 
> local function propagate(dv, val, okflag, ...)
>   dynvars[dv] = val
>   if okflag then return unpack(arg)
>             else error(args[1])
>   end
> end
> 
> function with(dv, val, fn, ...)
>   local old
>   old, dynvars[dv] = dynvars[dv], val
>   return propagate(dv, old, pcall(fn, unpack(arg))}
> end
> 
> end