kentuckyfriedtakahe/sodium — pull request #10 by luite (2014-03-13 16:08:11+00:00)
Title: attach finalisers to IORef instead of plain heap objects

according to the base package docs:

WARNING: weak pointers to ordinary non-primitive Haskell types
are particularly fragile, because the compiler is free to optimise
away or duplicate the underlying data structure. Therefore
attempting to place a finalizer on an ordinary Haskell type may
well result in the finalizer running earlier than you expected. This
is not a problem for caches and memo tables where early finalization is benign.

http://hackage.haskell.org/package/base-4.6.0.1/docs/System-Mem-Weak.html

This was the reason the finalizers were run too early when compiled with GHCJS (GHCJS handles thunk updating differently, making a shallow copy of the object, so the original would go out of scope and the duplicate wouldn't keep the weak reference alive). I've moved the finalizers to IORefs in the objects in this patch, but used undefined in a few places where i'm not sure that it's allowed