Does LuaJIT support __gc for tables?
Asked Answered
N

1

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.)

Neuro answered 21/10, 2013 at 7:48 Comment(0)
E
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.

Eros answered 21/10, 2013 at 9:18 Comment(4)
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.Hammered
@delnan This is not what happens but I edited my answer to make it clear (calling collectgarbage twice ensures all finalizers are called).Eros
Thanks for settling it.Hammered
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.Neuro

© 2022 - 2024 — McMap. All rights reserved.