Let custom typecasters access global state. Fixes #395