On Tue, Feb 21, 2012 at 3:48 PM, Dimiter 'malkia' Stanev
<malkia@gmail.com> wrote:
I think Mike added support for them, if you make compile with -DLUAJIT_ENABLE_LUA52COMPAT=1
src/lj_obj.h:
/* Metamethods. ORDER MM */
#ifdef LUAJIT_ENABLE_LUA52COMPAT
#define MMDEF_52(_) _(pairs) _(ipairs)
#else
#define MMDEF_52(_)
#endif
E:\p\luajit\src\buildvm_x86.dasc
|
|.ffunc_1 pairs
| mov TAB:RB, [BASE]
| cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
#ifdef LUAJIT_ENABLE_LUA52COMPAT
| cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
#endif
| 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
end,
})
end
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
print('----')
cpy[2] = 5
for k, v in ipairs(cpy) do print(k, v) end