2{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE ScopedTypeVariables #-}
5{-# LANGUAGE ForeignFunctionInterface #-}
6{-# LANGUAGE JavaScriptFFI #-}
13-- 'FastWeak' is a weak pointer to some value, and 'FastWeakTicket' ensures the value
14-- referred to by a 'FastWeak' stays live while the ticket is held (live).
16-- On GHC or GHCJS when not built with the @fast-weak@ cabal flag, 'FastWeak' is a wrapper
17-- around the simple version of 'System.Mem.Weak.Weak' where the key and value are the same.
19-- On GHCJS when built with the @fast-weak@ cabal flag, 'FastWeak' is implemented directly
20-- in JS using @h$FastWeak@ and @h$FastWeakTicket@ which are a nonstandard part of the GHCJS RTS.
25 , getFastWeakTicketValue
26 , getFastWeakTicketWeak
31 --TODO: Move these elsewhere
44import Control.Exception (evaluate)
45import System.IO.Unsafe
51-- | A 'FastWeak' which has been promoted to a strong reference. 'getFastWeakTicketValue'
52-- can be used to get the referred to value without fear of @Nothing,
53-- and 'getFastWeakTicketWeak' can be used to get the weak version.
55-- Implemented by way of special support in the GHCJS RTS, @h$FastWeakTicket@.
56newtype FastWeakTicket a = FastWeakTicket JSVal
58-- | A reference to some value which can be garbage collected if there are only
59-- weak references to the value left.
61-- 'getFastWeakValue' can be used to try and obtain a strong reference to the value.
63-- The value in a @FastWeak@ can also be kept alive by obtaining a 'FastWeakTicket' using
64-- 'getFastWeakTicket' if the value hasn't been collected yet.
66-- Implemented by way of special support in the GHCJS RTS, @h$FastWeak@.
67newtype FastWeak a = FastWeak JSVal
69-- Just designed to mirror JSVal, so that we can unsafeCoerce between the two
70data Val a = Val { unVal :: a }
72-- | Coerce a JSVal that represents the heap object of a value of type @a@ into a value of type @a@
73unsafeFromRawJSVal :: JSVal -> a
74unsafeFromRawJSVal v = unVal (unsafeCoerce v)
76-- | Coerce a heap object of type @a@ into a 'JSVal' which represents that object.
77unsafeToRawJSVal :: a -> JSVal
78unsafeToRawJSVal v = unsafeCoerce (Val v)
80-- | A 'FastWeak' which has been promoted to a strong reference. 'getFastWeakTicketValue'
81-- can be used to get the referred to value without fear of @Nothing@,
82-- and 'getFastWeakTicketWeak' can be used to get the weak version.
83data FastWeakTicket a = FastWeakTicket
84 { _fastWeakTicket_val :: !a
85 , _fastWeakTicket_weak :: {-# UNPACK #-} !(Weak a)
88-- | A reference to some value which can be garbage collected if there are only weak references to the value left.
90-- 'getFastWeakValue' can be used to try and obtain a strong reference to the value.
92-- The value in a @FastWeak@ can also be kept alive by obtaining a 'FastWeakTicket' using 'getFastWeakTicket'
93-- if the value hasn't been collected yet.
95-- Synonymous with 'Weak'.
96type FastWeak a = Weak a
99-- | Return the @a@ kept alive by the given 'FastWeakTicket'.
101-- This needs to be in IO so we know that we've relinquished the ticket.
102getFastWeakTicketValue :: FastWeakTicket a -> IO a
103#ifdef GHCJS_FAST_WEAK
104getFastWeakTicketValue t = do
106 return $ unsafeFromRawJSVal v
108foreign import javascript unsafe "$r = $1.val;" js_ticketVal :: FastWeakTicket a -> IO JSVal
110getFastWeakTicketValue = return . _fastWeakTicket_val
113-- | Get the value referred to by a 'FastWeak' if it hasn't yet been collected,
114-- or @Nothing@ if it has been collected.
115getFastWeakValue :: FastWeak a -> IO (Maybe a)
116#ifdef GHCJS_FAST_WEAK
117getFastWeakValue w = do
120 True -> return Nothing
121 False -> return $ Just $ unsafeFromRawJSVal r
123foreign import javascript unsafe "$1 === null" js_isNull :: JSVal -> Bool
125foreign import javascript unsafe "$r = ($1.ticket === null) ? null : $1.ticket.val;" js_weakVal :: FastWeak a -> IO JSVal
127getFastWeakValue = deRefWeak
130-- | Try to create a 'FastWeakTicket' for the given 'FastWeak' which will ensure the value referred
131-- remains alive. Returns @Just@ if the value hasn't been collected
132-- and a ticket can therefore be obtained, @Nothing@ if it's been collected.
133getFastWeakTicket :: forall a. FastWeak a -> IO (Maybe (FastWeakTicket a))
134#ifdef GHCJS_FAST_WEAK
135getFastWeakTicket w = do
138 True -> return Nothing
139 False -> return $ Just $ FastWeakTicket r
141foreign import javascript unsafe "$r = $1.ticket;" js_weakTicket :: FastWeak a -> IO JSVal
143getFastWeakTicket w = do
144 deRefWeak w >>= \case
145 Nothing -> return Nothing
146 Just v -> return $ Just $ FastWeakTicket
147 { _fastWeakTicket_val = v
148 , _fastWeakTicket_weak = w
152-- | Create a 'FastWeakTicket' directly from a value, creating a 'FastWeak' in the process
153-- which can be obtained with 'getFastWeakTicketValue'.
154mkFastWeakTicket :: a -> IO (FastWeakTicket a)
155-- I think it's fine if this is lazy - it'll retain the 'a', but so would the output; we just need to make sure it's forced before we start relying on the
156-- associated FastWeak to actually be weak
157#ifdef GHCJS_FAST_WEAK
158mkFastWeakTicket v = js_fastWeakTicket (unsafeToRawJSVal v)
160foreign import javascript unsafe "$r = new h$FastWeakTicket($1);" js_fastWeakTicket :: JSVal -> IO (FastWeakTicket a)
162mkFastWeakTicket v = do
164 w <- mkWeakPtr v' Nothing
165 return $ FastWeakTicket
166 { _fastWeakTicket_val = v'
167 , _fastWeakTicket_weak = w
171-- | Demote a 'FastWeakTicket'; which ensures the value is alive, to a 'FastWeak' which doesn't.
172-- Note that unless the ticket for the same 'FastWeak' is held in some other way
173-- the value might be collected immediately.
174getFastWeakTicketWeak :: FastWeakTicket a -> IO (FastWeak a)
175-- Needs IO so that it can force the value - otherwise, could end up with a reference to the Ticket, which would retain the value
176#ifdef GHCJS_FAST_WEAK
177foreign import javascript unsafe "$r = $1.weak;" getFastWeakTicketWeak' :: FastWeakTicket a -> IO (FastWeak a)
178{-# INLINE getFastWeakTicketWeak #-}
179getFastWeakTicketWeak = getFastWeakTicketWeak'
181getFastWeakTicketWeak = return . _fastWeakTicket_weak
184-- | A weak reference that is always empty
185emptyFastWeak :: FastWeak a
186emptyFastWeak = unsafeCoerce w
187 where w :: FastWeak Any
188#ifdef GHCJS_FAST_WEAK
191 w = unsafePerformIO $ do
192 w' <- mkWeakPtr undefined Nothing
196{-# NOINLINE emptyFastWeak #-}
198#ifdef GHCJS_FAST_WEAK
199foreign import javascript unsafe "$r = new h$FastWeak(null);" js_emptyWeak :: FastWeak Any
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list