lua-users home
lua-l archive

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


Peter Loveday wrote:
>[replace:]
> 
> obj:FuncA(...)
> obj:FuncB(...)
> obj:FuncC(...)
> 
> with:
> 
> UsingObj(obj)
> FuncA(...)
> FuncB(...)
> FuncC(...)
> 
> I have successfully set up the handler tables such that the global
> handler queries the active 'obj', and it will return FuncA (or whatever)
> as required.  However, I cannot find a way to simulate the ':', which
> magically inserts obj as the first argument... I have tried pushing
> multiple values on to the stack from my handler, but it doesn't seem
> to help.

You have to create a new closure each time:

  function global_index(glbl, name)
    local func = find_function(name)
    return function(...) return %func(obj, unpack(arg)) end
  end

Not very efficient though...

It's faster if FuncA/B/C fetch obj by themself.

Ciao, ET.