Rewrite the native-function refcounting to use the standard system.

This is simpler, more consistent, and gives way faster generated code.