Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does LuaJIT support __gc for tables?

Tags:

lua

luajit

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

like image 841
Niccolo M. Avatar asked Oct 21 '13 07:10

Niccolo M.


People also ask

What are weak tables in Lua?

Weak tables are the mechanism that you use to tell Lua that a reference should not prevent the reclamation of an object. A weak reference is a reference to an object that is not considered by the garbage collector.

Does Lua have garbage collection?

Lua uses a garbage collector that runs from time to time to collect dead objects when they are no longer accessible from the Lua program. All objects including tables, userdata, functions, thread, string and so on are subject to automatic memory management.


1 Answers

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.

like image 109
catwell Avatar answered Oct 25 '22 12:10

catwell