function setmt__gc(t, mt)
local prox = newproxy(true)
local mtcopy = {}
for k, v in pairs(mt) do
mtcopy[k] = v
end
getmetatable(prox).__gc =
function() mtcopy.__gc(t) end
mtcopy[prox] = true
return setmetatable(t, mtcopy)
end
local iscollected = false
function gctest(self)
iscollected = true
print("cleaning up:", self)
end
local mt2 = {__gc = gctest}
test = setmt__gc({}, mt2)
collectgarbage()
assert(not iscollected)
test = nil
collectgarbage()
assert(iscollected)
ZnVuY3Rpb24gc2V0bXRfX2djKHQsIG10KQogICAgbG9jYWwgcHJveCA9IG5ld3Byb3h5KHRydWUpCiAgICBsb2NhbCBtdGNvcHkgPSB7fQogICAgZm9yIGssIHYgaW4gcGFpcnMobXQpIGRvCiAgICAJbXRjb3B5W2tdID0gdgoJZW5kCgkKICAgIGdldG1ldGF0YWJsZShwcm94KS5fX2djID0gCiAgICAgICAgZnVuY3Rpb24oKSBtdGNvcHkuX19nYyh0KSBlbmQKICAgIG10Y29weVtwcm94XSA9IHRydWUKICAgIHJldHVybiBzZXRtZXRhdGFibGUodCwgbXRjb3B5KQplbmQKCgpsb2NhbCBpc2NvbGxlY3RlZCA9IGZhbHNlCmZ1bmN0aW9uIGdjdGVzdChzZWxmKQoJaXNjb2xsZWN0ZWQgPSB0cnVlCiAgICBwcmludCgiY2xlYW5pbmcgdXA6Iiwgc2VsZikKZW5kCgpsb2NhbCBtdDIgPSB7X19nYyA9IGdjdGVzdH0KdGVzdCA9IHNldG10X19nYyh7fSwgbXQyKQpjb2xsZWN0Z2FyYmFnZSgpCmFzc2VydChub3QgaXNjb2xsZWN0ZWQpCgp0ZXN0ID0gbmlsCmNvbGxlY3RnYXJiYWdlKCkKYXNzZXJ0KGlzY29sbGVjdGVkKQo=