lua-users home
lua-l archive

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


Hi Steve,

Is there a way to do it for shared upvalues? Just compile all the
functions that share those upvalues together? Is there a way to detect
that upvalues are shared?

do
  local up = 0
  function plus()
    up = up + 1
    print("plus", up)
  end
  -- local up = 0
  function minus()
    up = up - 1
    print("minus", up)
  end
end

What if these upvalues have the same name, but refer to different
variables (as happens if you uncomment the commented line)?

It seems like this may be possible, but is likely to require some
source code parsing/analysis.

Paul.

On Thu, Jul 26, 2012 at 10:05 PM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> On Thu, Jul 26, 2012 at 10:21 PM, Pierre-Yves Gérardy <pygy79@gmail.com> wrote:
>> Does that approach handle mutable upvalues?
>
> Yes, but slowly, since some metamethod/debug magic is needed to set
> them.  For immutables, it's much easier.
>
> local boo = 1
> local M = {}
> ...
> function M.foo() return boo end
> ..
>
> OK, so to recompile M.foo we wrap it so:
>
> local _v = ...; local boo, M = _v.boo,_v.M
> function M.foo() return boo end
>
> and pass this chunk a table extracted from the values of its current upvalues.
>
> It can't of course acquire new upvalues without a complete
> recompilation of the module. That's fast enough, true, but the issue
> here is preserving module state as long as possible.
>
> steve d.
>