Skip to content

Commit 3d96c52

Browse files
committed
Rearrange things to avoid dodgy FFI dependency
1 parent e73f9f5 commit 3d96c52

File tree

6 files changed

+61
-50
lines changed

6 files changed

+61
-50
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
"purescript-console": "^1.0.0",
2121
"purescript-exceptions": "^1.0.0",
2222
"purescript-functions": "^1.0.0",
23+
"purescript-parallel": "^1.0.0",
2324
"purescript-transformers": "^1.0.0",
24-
"purescript-parallel": "^1.0.0"
25+
"purescript-unsafe-coerce": "^1.0.0"
2526
},
2627
"devDependencies": {
2728
"purescript-partial": "^1.1.2"

src/Control/Monad/Aff.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,3 @@ exports._tailRecM = function (isLeft, f, a) {
320320
}(a);
321321
};
322322
};
323-
324-
325-
var avar = require("../Control.Monad.Aff.AVar/foreign.js");
326-
327-
exports._makeVar = avar._makeVar;
328-
exports._takeVar = avar._takeVar;
329-
exports._putVar = avar._putVar;
330-
exports._killVar = avar._killVar;

src/Control/Monad/Aff.purs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Prelude
2424

2525
import Control.Alt (class Alt)
2626
import Control.Alternative (class Alternative)
27+
import Control.Monad.Aff.Internal (AVBox, AVar, _killVar, _putVar, _takeVar, _makeVar)
2728
import Control.Monad.Cont.Class (class MonadCont)
2829
import Control.Monad.Eff (Eff)
2930
import Control.Monad.Eff.Class (class MonadEff)
@@ -39,6 +40,8 @@ import Data.Foldable (class Foldable, foldl)
3940
import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
4041
import Data.Monoid (class Monoid, mempty)
4142

43+
import Unsafe.Coerce (unsafeCoerce)
44+
4245
-- | An asynchronous computation with effects `e`. The computation either
4346
-- | errors or produces a value of type `a`.
4447
-- |
@@ -57,7 +60,7 @@ type PureAff a = forall e. Aff e a
5760
newtype Canceler e = Canceler (Error -> Aff e Boolean)
5861

5962
-- | Unwraps the canceler function from the newtype that wraps it.
60-
cancel :: forall e. Canceler e -> Error -> Aff e Boolean
63+
cancel :: forall e. Canceler e -> Error -> Aff e Boolean
6164
cancel (Canceler f) = f
6265

6366
-- | This function allows you to attach a custom canceler to an asynchronous
@@ -207,8 +210,8 @@ instance monoidCanceler :: Monoid (Canceler e) where
207210

208211
instance monadParAff :: MonadPar (Aff e) where
209212
par f ma mb = do
210-
va <- _makeVar nonCanceler
211-
vb <- _makeVar nonCanceler
213+
va <- makeVar
214+
vb <- makeVar
212215
c1 <- forkAff (putOrKill va =<< attempt ma)
213216
c2 <- forkAff (putOrKill vb =<< attempt mb)
214217
f <$> (takeVar va) <*> (takeVar vb)
@@ -219,8 +222,8 @@ instance monadParAff :: MonadPar (Aff e) where
219222
instance monadRaceAff :: MonadRace (Aff e) where
220223
stall = throwError $ error "Stalled"
221224
race a1 a2 = do
222-
va <- _makeVar nonCanceler -- the `a` value
223-
ve <- _makeVar nonCanceler -- the error count (starts at 0)
225+
va <- makeVar -- the `a` value
226+
ve <- makeVar -- the error count (starts at 0)
224227
putVar ve 0
225228
c1 <- forkAff $ either (maybeKill va ve) (putVar va) =<< attempt a1
226229
c2 <- forkAff $ either (maybeKill va ve) (putVar va) =<< attempt a2
@@ -232,28 +235,20 @@ instance monadRaceAff :: MonadRace (Aff e) where
232235
if e == 1 then killVar va err else pure unit
233236
putVar ve (e + 1)
234237

235-
--------------------------------
236-
237-
foreign import data AVar :: * -> *
238+
makeVar :: forall e a. Aff e (AVar a)
239+
makeVar = fromAVBox $ _makeVar nonCanceler
238240

239241
takeVar :: forall e a. AVar a -> Aff e a
240-
takeVar q = runFn2 _takeVar nonCanceler q
242+
takeVar q = fromAVBox $ runFn2 _takeVar nonCanceler q
241243

242244
putVar :: forall e a. AVar a -> a -> Aff e Unit
243-
putVar q a = runFn3 _putVar nonCanceler q a
245+
putVar q a = fromAVBox $ runFn3 _putVar nonCanceler q a
244246

245247
killVar :: forall e a. AVar a -> Error -> Aff e Unit
246-
killVar q e = runFn3 _killVar nonCanceler q e
247-
248-
foreign import _makeVar :: forall e a. Canceler e -> Aff e (AVar a)
249-
250-
foreign import _takeVar :: forall e a. Fn2 (Canceler e) (AVar a) (Aff e a)
251-
252-
foreign import _putVar :: forall e a. Fn3 (Canceler e) (AVar a) a (Aff e Unit)
253-
254-
foreign import _killVar :: forall e a. Fn3 (Canceler e) (AVar a) Error (Aff e Unit)
248+
killVar q e = fromAVBox $ runFn3 _killVar nonCanceler q e
255249

256-
--------------------------------
250+
fromAVBox :: forall a e. AVBox a -> Aff e a
251+
fromAVBox = unsafeCoerce
257252

258253
foreign import _cancelWith :: forall e a. Fn3 (Canceler e) (Aff e a) (Canceler e) (Aff e a)
259254

src/Control/Monad/Aff/AVar.purs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
-- | A low-level primitive for building asynchronous code.
22
module Control.Monad.Aff.AVar
3-
( AffAVar()
4-
, AVar()
5-
, AVAR()
6-
, killVar
3+
( AffAVar
4+
, AVAR
75
, makeVar
86
, makeVar'
9-
, modifyVar
10-
, putVar
117
, takeVar
8+
, putVar
9+
, modifyVar
10+
, killVar
11+
, module Exports
1212
) where
1313

1414
import Prelude
1515

16-
import Control.Monad.Aff (Aff(), Canceler(), nonCanceler)
16+
import Control.Monad.Aff (Aff, nonCanceler)
17+
import Control.Monad.Aff.Internal (AVar) as Exports
18+
import Control.Monad.Aff.Internal (AVBox, AVar, _killVar, _putVar, _takeVar, _makeVar)
1719
import Control.Monad.Eff.Exception (Error())
1820

19-
import Data.Function.Uncurried (Fn2(), Fn3(), runFn2, runFn3)
21+
import Data.Function.Uncurried (runFn3, runFn2)
2022

21-
foreign import data AVAR :: !
23+
import Unsafe.Coerce (unsafeCoerce)
2224

23-
foreign import data AVar :: * -> *
25+
foreign import data AVAR :: !
2426

2527
type AffAVar e a = Aff (avar :: AVAR | e) a
2628

2729
-- | Makes a new asynchronous avar.
2830
makeVar :: forall e a. AffAVar e (AVar a)
29-
makeVar = _makeVar nonCanceler
31+
makeVar = fromAVBox $ _makeVar nonCanceler
3032

3133
-- | Makes a avar and sets it to some value.
3234
makeVar' :: forall e a. a -> AffAVar e (AVar a)
@@ -37,25 +39,20 @@ makeVar' a = do
3739

3840
-- | Takes the next value from the asynchronous avar.
3941
takeVar :: forall e a. AVar a -> AffAVar e a
40-
takeVar q = runFn2 _takeVar nonCanceler q
42+
takeVar q = fromAVBox $ runFn2 _takeVar nonCanceler q
4143

4244
-- | Puts a new value into the asynchronous avar. If the avar has
4345
-- | been killed, this will result in an error.
4446
putVar :: forall e a. AVar a -> a -> AffAVar e Unit
45-
putVar q a = runFn3 _putVar nonCanceler q a
47+
putVar q a = fromAVBox $ runFn3 _putVar nonCanceler q a
4648

4749
-- | Modifies the value at the head of the avar (will suspend until one is available).
4850
modifyVar :: forall e a. (a -> a) -> AVar a -> AffAVar e Unit
4951
modifyVar f v = takeVar v >>= (f >>> putVar v)
5052

5153
-- | Kills an asynchronous avar.
5254
killVar :: forall e a. AVar a -> Error -> AffAVar e Unit
53-
killVar q e = runFn3 _killVar nonCanceler q e
54-
55-
foreign import _makeVar :: forall e a. Canceler e -> AffAVar e (AVar a)
56-
57-
foreign import _takeVar :: forall e a. Fn2 (Canceler e) (AVar a) (AffAVar e a)
58-
59-
foreign import _putVar :: forall e a. Fn3 (Canceler e) (AVar a) a (AffAVar e Unit)
55+
killVar q e = fromAVBox $ runFn3 _killVar nonCanceler q e
6056

61-
foreign import _killVar :: forall e a. Fn3 (Canceler e) (AVar a) Error (AffAVar e Unit)
57+
fromAVBox :: forall a e. AVBox a -> AffAVar e a
58+
fromAVBox = unsafeCoerce
File renamed without changes.

src/Control/Monad/Aff/Internal.purs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Control.Monad.Aff.Internal
2+
( AVBox
3+
, AVar
4+
, _makeVar
5+
, _takeVar
6+
, _putVar
7+
, _killVar
8+
) where
9+
10+
import Prelude
11+
12+
import Control.Monad.Eff.Exception (Error)
13+
14+
import Data.Function.Uncurried (Fn2, Fn3)
15+
16+
foreign import data AVar :: * -> *
17+
18+
foreign import data AVBox :: * -> *
19+
20+
foreign import _makeVar :: forall c a. c -> AVBox (AVar a)
21+
22+
foreign import _takeVar :: forall c a. Fn2 c (AVar a) (AVBox a)
23+
24+
foreign import _putVar :: forall c a. Fn3 c (AVar a) a (AVBox Unit)
25+
26+
foreign import _killVar :: forall c a. Fn3 c (AVar a) Error (AVBox Unit)

0 commit comments

Comments
 (0)