@@ -3,7 +3,7 @@
33#ifdef USE_REFLEX_OPTIMIZER
44{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
55#endif
6−#ifdef ghcjs_HOST_OS
6+#ifdef GHCJS_FAST_WEAK
77{-# LANGUAGE ForeignFunctionInterface #-}
88{-# LANGUAGE JavaScriptFFI #-}
99#endif
@@ -21,7 +21,7 @@ module Data.FastWeakBag
2121 , remove
2222 -- * Internal functions
2323 -- These will not always be available.
24−#ifndef ghcjs_HOST_OS
24+#ifndef GHCJS_FAST_WEAK
2525 , _weakBag_children --TODO: Don't export this
2626#endif
2727 ) where
@@ -29,7 +29,7 @@ module Data.FastWeakBag
2929import Control.Monad hiding (forM_, mapM_)
3030import Control.Monad.IO.Class
3131
32−#ifdef ghcjs_HOST_OS
32+#ifdef GHCJS_FAST_WEAK
3333import GHCJS.Types
3434import Reflex.FastWeak (unsafeFromRawJSVal, unsafeToRawJSVal, js_isNull)
3535#else
@@ -46,7 +46,7 @@ import Prelude hiding (mapM_, traverse)
4646-- | A @FastWeakBag a@ holds a set of values of type @a@, but does not retain them -
4747-- that is, they can still be garbage-collected. As long as the @a@s remain
4848-- alive, the 'FastWeakBag' will continue to refer to them.
49−#ifdef ghcjs_HOST_OS
49+#ifdef GHCJS_FAST_WEAK
5050newtype FastWeakBag a = FastWeakBag JSVal
5151#else
5252data FastWeakBag a = FastWeakBag
@@ -59,7 +59,7 @@ data FastWeakBag a = FastWeakBag
5959-- the caller retains the ticket, the item is guranteed to stay in memory (and
6060-- thus in the 'FastWeakBag'). The ticket can also be used to remove the item from
6161-- the 'FastWeakBag' prematurely (i.e. while it is still alive), using 'remove'.
62−#ifdef ghcjs_HOST_OS
62+#ifdef GHCJS_FAST_WEAK
6363newtype FastWeakBagTicket a = FastWeakBagTicket JSVal
6464#else
6565data FastWeakBagTicket a = FastWeakBagTicket
@@ -74,7 +74,7 @@ insert :: a -- ^ The item
7474 -> FastWeakBag a -- ^ The 'FastWeakBag' to insert into
7575 -> IO (FastWeakBagTicket a) -- ^ Returns a 'FastWeakBagTicket' that ensures the item
7676 -- is retained and allows the item to be removed.
77−#ifdef ghcjs_HOST_OS
77+#ifdef GHCJS_FAST_WEAK
7878insert a wb = js_insert (unsafeToRawJSVal a) wb
7979foreign import javascript unsafe "$r = new h$FastWeakBagTicket($2, $1);" js_insert :: JSVal -> FastWeakBag a -> IO (FastWeakBagTicket a)
8080#else
@@ -93,7 +93,7 @@ insert a (FastWeakBag nextId children) = {-# SCC "insert" #-} do
9393-- | Create an empty 'FastWeakBag'.
9494{-# INLINE empty #-}
9595empty :: IO (FastWeakBag a)
96−#ifdef ghcjs_HOST_OS
96+#ifdef GHCJS_FAST_WEAK
9797empty = js_empty
9898foreign import javascript unsafe "$r = new h$FastWeakBag();" js_empty :: IO (FastWeakBag a)
9999#else
@@ -110,7 +110,7 @@ empty = {-# SCC "empty" #-} do
110110-- | Check whether a 'FastWeakBag' is empty.
111111{-# INLINE isEmpty #-}
112112isEmpty :: FastWeakBag a -> IO Bool
113−#ifdef ghcjs_HOST_OS
113+#ifdef GHCJS_FAST_WEAK
114114isEmpty = js_isEmpty
115115foreign import javascript unsafe "(function(){ for(var i = 0; i < $1.tickets.length; i++) { if($1.tickets[i] !== null) { return false; } }; return true; })()" js_isEmpty :: FastWeakBag a -> IO Bool --TODO: Clean up as we go along so this isn't O(n) every time
116116#else
@@ -123,7 +123,7 @@ isEmpty bag = {-# SCC "isEmpty" #-} IntMap.null <$> readIORef (_weakBag_children
123123-- when the traversal began will be visited exactly once; however, no guarantee
124124-- is made about the order of the traversal.
125125traverse :: forall a m. MonadIO m => FastWeakBag a -> (a -> m ()) -> m ()
126−#ifdef ghcjs_HOST_OS
126+#ifdef GHCJS_FAST_WEAK
127127traverse wb f = do
128128 let go cursor = when (not $ js_isNull cursor) $ do
129129 val <- liftIO $ js_getTicketValue cursor
@@ -146,7 +146,7 @@ traverse (FastWeakBag _ children) f = {-# SCC "traverse" #-} do
146146-- on the same 'FastWeakBagTicket'.
147147{-# INLINE remove #-}
148148remove :: FastWeakBagTicket a -> IO ()
149−#ifdef ghcjs_HOST_OS
149+#ifdef GHCJS_FAST_WEAK
150150remove = js_remove
151151foreign import javascript unsafe "$1.bag.tickets[$1.pos] = null; $1.bag = new h$FastWeakBag(); $1.bag.tickets.push($1); $1.pos = 0;" js_remove :: FastWeakBagTicket a -> IO () --TODO: Don't bother with the new surrogate FastWeakBag; instead, make the GC check for bag === null, and then null it out here
152152#else