Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack Overflow on Lua metatable

Tags:

lua

lua-5.4

I used to have a construct that worked with luajit:

mytbl = setmetatable({1}, {__index = function(tbl,idx) return tbl[idx - 1] + 1 end})

Now with plain Lua 5.4 this gives me a stack overflow:

> mytbl[1000]
stdin:1: C stack overflow
stack traceback:
    stdin:1: in metamethod 'index'
    ....

The goal is to have a table where the default is to return the index itself:

mytbl[10] 

should return 10. But when I say

mytbl[3] = 5

the value of

mytbl[10]

should be 12 (the values from 1 now yield 1,2,5,6,7,8,9,10,11,12,...)

Is there a way to get this in Lua 5.4 without the stack overflow? Or should I create another function for it?

like image 353
topskip Avatar asked Sep 10 '25 12:09

topskip


1 Answers

You are accessing the table within the __index. That causes another __index to be called and so on.

Use rawget when you want to access the tbl itself.

If you want to get a custom logic that does not rely on existence of elements, write your function in a way that allows for trailing recursion or write it iteratively without any recursion at all:

__index=function(tbl, idx)
  local acc = 0
  for i=idx-1, 1, -1 do
    local th = rawget(tbl, i)
    if th then
      return acc + th + 1
    else
      acc = acc + 1
    end
  end
  return acc
end
like image 144
Green Avatar answered Sep 13 '25 15:09

Green



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!