3

I'm aware that I can fix this problem by using rawset, but I'm just wondering why the following code causes a C stack overflow.

local mt = {
    __newindex = function(self, key, value) 
        self[key] = value 
    end
}

local x = setmetatable({}, mt)

x.y = 5
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
David
  • 693
  • 1
  • 7
  • 20

1 Answers1

4

Deep Recursion.

Inside the call to the metamethod __newindex, self[key] = value calls the metamethod __newindex again, recursively, until stack overflow.

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294