lua-users home
lua-l archive

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




On Mon, Aug 22, 2022 at 4:12 AM Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> Or is there a function in Lua I could “wrap” for this kind of tracing?

function WRAP(f)
        return function (...)
                print("WRAP in ",...)
                local t=table.pack(f(...))
                print("WRAP out",table.unpack(t))
                return table.unpack(t)
        end
end


I would suggest passing the size of the table captured by table.pack explicitly to table.unpack, that way the function being wrapped can return 'nil' as the last return value without it being dropped. The table may have multiple borders.

function WRAP(f)
    return function (...)
        print(">>", ...)
        local res = table.pack(f(...))
        print("<<", table.unpack(res, 1, res.n))
        return table.unpack(res, 1, res.n)
    end
end


 
--