Bug Report LuaJIT 2.0.3 __ipairs metatable method behaviour differ from Lua __ipairs metatable method behaviour.

  • From: Eric Man <meric.au@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 13 Feb 2015 20:18:12 +1100

Hello,

I was trying to hook into __ipairs to build a ipairs-compatible linked list
type in lua.

I found the ipairs behaviour is different in LuaJIT and Lua with the same
code.

I don't know if this is an intentional optimisation made by LuaJIT to "fix"
the "self" up value when calculating __ipairs.

I'm not sure if it is specific to ipairs or references upvalues in general.

You can see the code below:

➜  Desktop  lua ipairstest.lua

1 1

2 2

3 3

➜  Desktop  luajit ipairstest.lua

1 1

2 table: 0x0004a5c8

➜  Desktop  lua -v

Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio

➜  Desktop  luajit -v

LuaJIT 2.0.3 -- Copyright (C) 2005-2014 Mike Pall. http://luajit.org/

➜  Desktop  cat ipairstest.lua

list = {

  __ipairs = function(self)

    local i = 0

    return function()

      if self then

        local obj = self[1]

        self = self[2]

        i = i + 1

        return i, obj

      end

    end

  end

}


local alist =

  setmetatable({1,

    setmetatable({2,

      setmetatable({3, nil}, list)},

    list)},

  list)



for i, v in ipairs(alist) do

  print(i, v)

end

Best Regards,

Eric

Other related posts: