lualove2dmetatablemeta-method

Why doesn't love2d use my custom-defined pairs metamethod?


I have the following code in the file main.lua:

local table = {data={a=1,b=2,c=3}}
setmetatable(table, table)

function table:__pairs()
    return pairs(self.data)
end

function table:__tostring()
    return "a table"
end

print(table)

for e in pairs(table) do
    print(e)
end

When I run lua main.lua I get the output

a table
a
b
c

When I run love ~/path/to/project I get the output

a table
__tostring
data
__pairs

Why does love use other metamethods correctly, but not pairs?

I have LOVE 11.3 (Mysterious Mysteries) and Lua 5.3.5


Solution

  • Love2D uses LuaJIT as its default interpreter, which is fixed to Lua 5.1. And while you can rebuild Love2D for the standard Lua 5.1 interpreter, making it use modern versions of the standard Lua interpreter would require substantial code hacking, since 5.2+ aren't backwards compatible.

    And Lua 5.1 doesn't have the pairs metamethod.