@@ -3,7 +3,7 @@
3#ifdef USE_REFLEX_OPTIMIZER
3#ifdef USE_REFLEX_OPTIMIZER
4{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
4{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
6−#ifdef ghcjs_HOST_OS
6+#ifdef GHCJS_FAST_WEAK
7{-# LANGUAGE ForeignFunctionInterface #-}
7{-# LANGUAGE ForeignFunctionInterface #-}
8{-# LANGUAGE JavaScriptFFI #-}
8{-# LANGUAGE JavaScriptFFI #-}
@@ -21,7 +21,7 @@ module Data.FastWeakBag
22 -- * Internal functions
22 -- * Internal functions
23 -- These will not always be available.
23 -- These will not always be available.
24−#ifndef ghcjs_HOST_OS
24+#ifndef GHCJS_FAST_WEAK
25 , _weakBag_children --TODO: Don't export this
25 , _weakBag_children --TODO: Don't export this
@@ -29,7 +29,7 @@ module Data.FastWeakBag
29import Control.Monad hiding (forM_, mapM_)
29import Control.Monad hiding (forM_, mapM_)
30import Control.Monad.IO.Class
30import Control.Monad.IO.Class
32−#ifdef ghcjs_HOST_OS
32+#ifdef GHCJS_FAST_WEAK
33import GHCJS.Types
33import GHCJS.Types
34import Reflex.FastWeak (unsafeFromRawJSVal, unsafeToRawJSVal, js_isNull)
34import Reflex.FastWeak (unsafeFromRawJSVal, unsafeToRawJSVal, js_isNull)
@@ -46,7 +46,7 @@ import Prelude hiding (mapM_, traverse)
46-- | A @FastWeakBag a@ holds a set of values of type @a@, but does not retain them -
46-- | A @FastWeakBag a@ holds a set of values of type @a@, but does not retain them -
47-- that is, they can still be garbage-collected. As long as the @a@s remain
47-- that is, they can still be garbage-collected. As long as the @a@s remain
48-- alive, the 'FastWeakBag' will continue to refer to them.
48-- alive, the 'FastWeakBag' will continue to refer to them.
49−#ifdef ghcjs_HOST_OS
49+#ifdef GHCJS_FAST_WEAK
50newtype FastWeakBag a = FastWeakBag JSVal
50newtype FastWeakBag a = FastWeakBag JSVal
52data FastWeakBag a = FastWeakBag
52data FastWeakBag a = FastWeakBag
@@ -59,7 +59,7 @@ data FastWeakBag a = FastWeakBag
59-- the caller retains the ticket, the item is guranteed to stay in memory (and
59-- the caller retains the ticket, the item is guranteed to stay in memory (and
60-- thus in the 'FastWeakBag'). The ticket can also be used to remove the item from
60-- thus in the 'FastWeakBag'). The ticket can also be used to remove the item from
61-- the 'FastWeakBag' prematurely (i.e. while it is still alive), using 'remove'.
61-- the 'FastWeakBag' prematurely (i.e. while it is still alive), using 'remove'.
62−#ifdef ghcjs_HOST_OS
62+#ifdef GHCJS_FAST_WEAK
63newtype FastWeakBagTicket a = FastWeakBagTicket JSVal
63newtype FastWeakBagTicket a = FastWeakBagTicket JSVal
65data FastWeakBagTicket a = FastWeakBagTicket
65data FastWeakBagTicket a = FastWeakBagTicket
@@ -74,7 +74,7 @@ insert :: a -- ^ The item
74 -> FastWeakBag a -- ^ The 'FastWeakBag' to insert into
74 -> FastWeakBag a -- ^ The 'FastWeakBag' to insert into
75 -> IO (FastWeakBagTicket a) -- ^ Returns a 'FastWeakBagTicket' that ensures the item
75 -> IO (FastWeakBagTicket a) -- ^ Returns a 'FastWeakBagTicket' that ensures the item
76 -- is retained and allows the item to be removed.
76 -- is retained and allows the item to be removed.
77−#ifdef ghcjs_HOST_OS
77+#ifdef GHCJS_FAST_WEAK
78insert a wb = js_insert (unsafeToRawJSVal a) wb
78insert a wb = js_insert (unsafeToRawJSVal a) wb
79foreign import javascript unsafe "$r = new h$FastWeakBagTicket($2, $1);" js_insert :: JSVal -> FastWeakBag a -> IO (FastWeakBagTicket a)
79foreign import javascript unsafe "$r = new h$FastWeakBagTicket($2, $1);" js_insert :: JSVal -> FastWeakBag a -> IO (FastWeakBagTicket a)
@@ -93,7 +93,7 @@ insert a (FastWeakBag nextId children) = {-# SCC "insert" #-} do
93-- | Create an empty 'FastWeakBag'.
93-- | Create an empty 'FastWeakBag'.
94{-# INLINE empty #-}
94{-# INLINE empty #-}
95empty :: IO (FastWeakBag a)
95empty :: IO (FastWeakBag a)
96−#ifdef ghcjs_HOST_OS
96+#ifdef GHCJS_FAST_WEAK
97empty = js_empty
97empty = js_empty
98foreign import javascript unsafe "$r = new h$FastWeakBag();" js_empty :: IO (FastWeakBag a)
98foreign import javascript unsafe "$r = new h$FastWeakBag();" js_empty :: IO (FastWeakBag a)
@@ -110,7 +110,7 @@ empty = {-# SCC "empty" #-} do
110-- | Check whether a 'FastWeakBag' is empty.
110-- | Check whether a 'FastWeakBag' is empty.
111{-# INLINE isEmpty #-}
111{-# INLINE isEmpty #-}
112isEmpty :: FastWeakBag a -> IO Bool
112isEmpty :: FastWeakBag a -> IO Bool
113−#ifdef ghcjs_HOST_OS
113+#ifdef GHCJS_FAST_WEAK
114isEmpty = js_isEmpty
114isEmpty = js_isEmpty
115foreign 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
115foreign 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
@@ -123,7 +123,7 @@ isEmpty bag = {-# SCC "isEmpty" #-} IntMap.null <$> readIORef (_weakBag_children
123-- when the traversal began will be visited exactly once; however, no guarantee
123-- when the traversal began will be visited exactly once; however, no guarantee
124-- is made about the order of the traversal.
124-- is made about the order of the traversal.
125traverse :: forall a m. MonadIO m => FastWeakBag a -> (a -> m ()) -> m ()
125traverse :: forall a m. MonadIO m => FastWeakBag a -> (a -> m ()) -> m ()
126−#ifdef ghcjs_HOST_OS
126+#ifdef GHCJS_FAST_WEAK
127traverse wb f = do
127traverse wb f = do
128 let go cursor = when (not $ js_isNull cursor) $ do
128 let go cursor = when (not $ js_isNull cursor) $ do
129 val <- liftIO $ js_getTicketValue cursor
129 val <- liftIO $ js_getTicketValue cursor
@@ -146,7 +146,7 @@ traverse (FastWeakBag _ children) f = {-# SCC "traverse" #-} do
146-- on the same 'FastWeakBagTicket'.
146-- on the same 'FastWeakBagTicket'.
147{-# INLINE remove #-}
147{-# INLINE remove #-}
148remove :: FastWeakBagTicket a -> IO ()
148remove :: FastWeakBagTicket a -> IO ()
149−#ifdef ghcjs_HOST_OS
149+#ifdef GHCJS_FAST_WEAK
150remove = js_remove
150remove = js_remove
151foreign 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
151foreign 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