fork(4) download
  1. function setmt__gc(t, mt)
  2. local prox = newproxy(true)
  3. local mtcopy = {}
  4. for k, v in pairs(mt) do
  5. mtcopy[k] = v
  6. end
  7.  
  8. getmetatable(prox).__gc =
  9. function() mtcopy.__gc(t) end
  10. mtcopy[prox] = true
  11. return setmetatable(t, mtcopy)
  12. end
  13.  
  14.  
  15. local iscollected = false
  16. function gctest(self)
  17. iscollected = true
  18. print("cleaning up:", self)
  19. end
  20.  
  21. local mt2 = {__gc = gctest}
  22. test = setmt__gc({}, mt2)
  23. collectgarbage()
  24. assert(not iscollected)
  25.  
  26. test = nil
  27. collectgarbage()
  28. assert(iscollected)
  29.  
Success #stdin #stdout 0.02s 2540KB
stdin
Standard input is empty
stdout
cleaning up:	table: 0x8568f50