lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- track collections |
| 2 | |
| 3 | |
| 4 | local M = {} |
| 5 | |
| 6 | -- import list |
| 7 | local stderr, collectgarbage = io.stderr, collectgarbage |
| 8 | |
| 9 | -- the debug version of setmetatable does not create any object (such as |
| 10 | -- a '__metatable' string), and so it is more appropriate to be used in |
| 11 | -- a finalizer |
| 12 | local setmetatable = require"debug".setmetatable |
| 13 | |
| 14 | global none |
| 15 | |
| 16 | local active = false |
| 17 | |
| 18 | |
| 19 | -- each time a table is collected, remark it for finalization on next |
| 20 | -- cycle |
| 21 | local mt = {} |
| 22 | function mt.__gc (o) |
| 23 | stderr:write'.' -- mark progress |
| 24 | if active then |
| 25 | setmetatable(o, mt) -- remark object for finalization |
| 26 | end |
| 27 | end |
| 28 | |
| 29 | |
| 30 | function M.start () |
| 31 | if not active then |
| 32 | active = true |
| 33 | setmetatable({}, mt) -- create initial object |
| 34 | end |
| 35 | end |
| 36 | |
| 37 | |
| 38 | function M.stop () |
| 39 | if active then |
| 40 | active = false |
| 41 | collectgarbage() -- call finalizer for the last time |
| 42 | end |
| 43 | end |
| 44 | |
| 45 | return M |