lua-users home
lua-l archive

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

Thanks. I'll try it.

On Tue, Feb 21, 2012 at 3:48 PM, Dimiter 'malkia' Stanev <> wrote:
       I think Mike added support for them, if you make compile with -DLUAJIT_ENABLE_LUA52COMPAT=1


/* Metamethods. ORDER MM */
#define MMDEF_52(_) _(pairs) _(ipairs)
#define MMDEF_52(_)

 |.ffunc_1 pairs
 |  mov TAB:RB, [BASE]
 |  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback
 |  cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
 |  mov CFUNC:RB, [BASE-8]
 |  mov CFUNC:RD, CFUNC:RB->upvalue[0]
 |  mov PC, [BASE-4]
 |  mov dword [BASE-4], LJ_TFUNC
 |  mov [BASE-8], CFUNC:RD
 |  mov dword [BASE+12], LJ_TNIL
 |  mov RD, 1+3
 |  jmp ->fff_res

On 2/21/2012 10:56 AM, Axel Kittenberger wrote:
This needs Lua 5.2, with 5.1 or LuaJIT you're mostly out of luck (as
far I know). Also note this will make a copy of original at the moment
of write, not of the moment made the COW, so if you change the
original before, it changes in background. But I suppose you consider
that one immutable.

function makeCOW(o)
    return setmetatable({}, {
        __index= o,
        __pairs = function() return pairs(o) end,
        __ipairs = function() return ipairs(o) end,
        __newindex =
            function(t, k, v)
                setmetatable(t, nil)
                for ck, cv in pairs(o) do t[ck] = cv end
                t[k] = v

local org = {1,2,3,4,5,6,7,8,9}
local cpy = makeCOW(org);

for k, v in ipairs(cpy) do print(k, v) end
cpy[2] = 5
for k, v in ipairs(cpy) do print(k, v) end