Add a weak reference type, written largely by Alex Rønne Petersen.