github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/lua/tables.quotas.lua (about)

     1  -- Filling a table consumes memory
     2  local t = {}
     3  print(runtime.callcontext({kill={memory=1000}}, function()
     4      local i = 1
     5      while true do
     6          t[i] = i
     7          i = i + 1
     8      end
     9  end))
    10  --> =killed
    11  
    12  print(#t > 10, #t < 100)
    13  --> =true	true
    14  
    15  -- Replacing scalar elements in a table doesn't consume memory
    16  local t = {1}
    17  
    18  local ctx = runtime.callcontext(
    19      {kill={memory=10000, cpu=10000}},
    20      function()
    21          while true do
    22              t[1] = t[1] + 1
    23          end
    24      end
    25  )
    26  
    27  print(ctx.status)
    28  --> =killed
    29  
    30  -- Check we didn't run out of memory
    31  print(ctx.used.memory < 1000)
    32  --> =true
    33  
    34  -- Check we ran out of cpu
    35  print(ctx.used.cpu >= ctx.kill.cpu - 50)
    36  --> =true
    37  
    38  -- Check we did a number of iterations
    39  print(t[1] > 100)
    40  --> =true