Commit d31ac01c authored by simonm's avatar simonm

[project @ 1999-02-02 14:19:02 by simonm]

- Add Weak.finalise
- Move some functions from PrelWeak to Weak.
parent 011886e4
......@@ -2,18 +2,16 @@
% (c) The AQUA Project, Glasgow University, 1998
%
\section[Weak]{Module @PrelWeak@}
\section[Weak]{Module @Weak@}
\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}
module Weak (
Weak, -- abstract
-- instance Eq (Weak v)
mkWeak, -- :: k -> v -> IO () -> IO (Weak v)
deRefWeak, -- :: Weak v -> IO (Maybe v)
-- finalise -- :: Weak v -> IO ()
finalise, -- :: Weak v -> IO ()
-- replaceFinaliser -- :: Weak v -> IO () -> IO ()
mkWeakNoFinaliser, -- :: k -> v -> IO (Weak v)
......@@ -23,6 +21,28 @@ module Weak (
addForeignFinaliser -- :: ForeignObj -> IO () -> IO ()
) where
import PrelBase
import PrelIOBase
import PrelWeak
import Foreign
deRefWeak :: Weak v -> IO (Maybe v)
deRefWeak (Weak w) = IO $ \s ->
case deRefWeak# w s of
(# s1, flag, p #) -> case flag of
0# -> (# s1, Nothing #)
_ -> (# s1, Just p #)
mkWeakNoFinaliser key val = IO $ \s ->
-- zero is a valid finaliser argument to mkWeak#, and means "no finaliser"
case mkWeak# key val (unsafeCoerce# 0#) s of { (# s1, w #) ->
(# s1, Weak w #) }
mkWeakPair :: k -> v -> IO () -> IO (Weak (k,v))
mkWeakPair key val finaliser = mkWeak key (key,val) finaliser
finalise :: Weak v -> IO ()
finalise (Weak w) = IO $ \s ->
case finaliseWeak# w s of s1 -> (# s1, () #)
\end{code}
......@@ -10,7 +10,6 @@
module PrelWeak where
import PrelGHC
import PrelMaybe
import PrelBase
import PrelIOBase
import PrelForeign
......@@ -26,24 +25,9 @@ mkWeak key val finaliser = IO $ \s ->
case mkWeak# key val finaliser s of { (# s1, w #) ->
(# s1, Weak w #) }
mkWeakNoFinaliser key val = IO $ \s ->
-- zero is a valid finaliser argument to mkWeak#, and means "no finaliser"
case mkWeak# key val (unsafeCoerce# 0#) s of { (# s1, w #) ->
(# s1, Weak w #) }
deRefWeak :: Weak v -> IO (Maybe v)
deRefWeak (Weak w) = IO $ \s ->
case deRefWeak# w s of
(# s1, flag, p #) -> case flag of
0# -> (# s1, Nothing #)
_ -> (# s1, Just p #)
mkWeakPtr :: k -> IO () -> IO (Weak k)
mkWeakPtr key finaliser = mkWeak key key finaliser
mkWeakPair :: k -> v -> IO () -> IO (Weak (k,v))
mkWeakPair key val finaliser = mkWeak key (key,val) finaliser
addFinaliser :: key -> IO () -> IO ()
addFinaliser key finaliser = do
mkWeakPtr key finaliser -- throw it away
......@@ -53,9 +37,6 @@ addForeignFinaliser :: ForeignObj -> IO () -> IO ()
addForeignFinaliser (ForeignObj fo) finaliser = addFinaliser fo finaliser
{-
finalise :: Weak v -> IO ()
finalise (Weak w) = finaliseWeak# w
instance Eq (Weak v) where
(Weak w1) == (Weak w2) = w1 `sameWeak#` w2
-}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment