lua-users home
lua-l archive

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


debug.getinfo, debug.setlocal, debug.setupvalue add up to a lot of power.
Maybe enough to do what you want.

2013/10/17 Elijah Frederickson <elijah.frederickson@gmail.com>:
> How can I override a local function/variable that's in a different
> function/environment?
>
> e.g. (yes, its Lua 5.1):
>
> function override(orig, replacement)
>     local x = getfenv(2)
>     for k, v in pairs(x) do
>         if v==origthen x[k] = replacement end
>     end
> end
>
> local function a() print"a" end
> local old_a = a
> override(a, function() print"overridden"; old_a() end)
> local x = 1
> override(x, 2)
> print(x) -- should print 2 now
>
>
> That code works if function 'a' is not local, but is global. I know that
> local variables are directly mapped to registers, so I don't even know if
> it's possible. Any suggestions, for 5.1 or 5.2?
>
>
>
> Thanks,
> Elijah Frederickson
> --
> ~ mlnlover11 ~
> https://github.com/mlnlover11