You might find this one easier to backport from 5.1: http://lua-users.org/wiki/EmergencyGarbageCollector