Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm impressed. It seems to be in total working order when trying out collectgarbage, __gc and __mode[0].

Code:

  print(_VERSION)
  local a = setmetatable({}, {__mode = 'v'})
  local b = setmetatable({}, {__gc = function(x) print("Deleting: " .. tostring(x)) end})
  a[1] = b
  a[2] = 2
  print(table.unpack(a))
  print(collectgarbage'count')
  b = nil
  collectgarbage()
  print(table.unpack(a))
Result:

  Lua 5.3
  table: 0x50a748 2
  21.4033203125
  Deleting: table: 0x50a748
  nil 2
[0] - 3 features tied to the Lua GC, to simplify, they basically are (respectively): forcefully running the GC, finalizers that run when object is collected and weak refs that don't hold the object alive on their own.

Perhaps http://www.lua.org/demo.html could be replaced with that for users who have JS enabled.

Again - just wow, that's really nice, Lua in a browser.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: