8

Lua 5.2 (in contrast to 5.1) supports __gc for tables.

Has LuaJIT borrowed this nice feature?

(I did a google search, and examined LuaJIT's Change History but couldn't figure out the answer.)

Niccolo M.
  • 3,363
  • 2
  • 22
  • 39

1 Answers1

10

Just try it:

-- test.lua
do
  local x = setmetatable({},{
    __gc = function() print("works") end
  })
end
collectgarbage("collect")
collectgarbage("collect")

.

$ lua51 -v
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
$ lua51 test.lua
$ lua52 -v
Lua 5.2.2  Copyright (C) 1994-2013 Lua.org, PUC-Rio
$ lua52 test.lua
works
$ luajit -v
LuaJIT 2.0.2 -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org/
$ luajit test.lua
$

So the short answer is no.

catwell
  • 6,770
  • 1
  • 23
  • 21
  • 1
    It's possible that LuaJIT just doesn't bother running a GC cycle when shutting down. A longer, more allocation heavy script might be a better test. –  Oct 22 '13 at 19:15
  • 2
    @delnan This is not what happens but I edited my answer to make it clear (calling `collectgarbage` twice ensures all finalizers are called). – catwell Oct 22 '13 at 21:01
  • 1
    Thanks for settling it. –  Oct 22 '13 at 21:02
  • 2
    Your amendment to the code still doesn't settle it because you didn't wrap the "local x" with "do ... end" so the "x" variable is still living and collectgarbage() won't dispose of this table. – Niccolo M. Oct 23 '13 at 23:17