Skip to content

Commit 041813b

Browse files
committed
Move FFI declarations into their own files, for 0.7
1 parent 6247163 commit 041813b

File tree

6 files changed

+118
-112
lines changed

6 files changed

+118
-112
lines changed

src/Node/FS/Async.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* global require */
2+
/* global exports */
3+
"use strict";
4+
5+
// module Node.FS.Async
6+
7+
exports.fs = require('fs');
8+
9+
exports.handleCallbackImpl = function (left, right, f) {
10+
return function (err, value) {
11+
if (err) {
12+
f(left(err))();
13+
} else {
14+
f(right(value))();
15+
}
16+
};
17+
};

src/Node/FS/Async.purs

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,16 @@ foreign import data Nullable :: * -> *
4545

4646
type JSCallback a = Fn2 (Nullable Error) a Unit
4747

48-
foreign import handleCallbackImpl
49-
"function handleCallbackImpl(left, right, f) {\
50-
\ return function(err, value) {\
51-
\ if (err) f(left(err))();\
52-
\ else f(right(value))();\
53-
\ };\
54-
\}" :: forall eff a. Fn3 (Error -> Either Error a)
55-
(a -> Either Error a)
56-
(Callback eff a)
57-
(JSCallback a)
48+
foreign import handleCallbackImpl ::
49+
forall eff a. Fn3 (Error -> Either Error a)
50+
(a -> Either Error a)
51+
(Callback eff a)
52+
(JSCallback a)
5853

5954
handleCallback :: forall eff a b. (Callback eff a) -> JSCallback a
6055
handleCallback cb = runFn3 handleCallbackImpl Left Right cb
6156

62-
foreign import fs "var fs = require('fs');" ::
57+
foreign import fs ::
6358
{ rename :: Fn3 FilePath FilePath (JSCallback Unit) Unit
6459
, truncate :: Fn3 FilePath Number (JSCallback Unit) Unit
6560
, chown :: Fn4 FilePath Number Number (JSCallback Unit) Unit
@@ -72,19 +67,14 @@ foreign import fs "var fs = require('fs');" ::
7267
, unlink :: Fn2 FilePath (JSCallback Unit) Unit
7368
, rmdir :: Fn2 FilePath (JSCallback Unit) Unit
7469
, mkdir :: Fn3 FilePath String (JSCallback Unit) Unit
75-
, readdir :: Fn2 FilePath (JSCallback [FilePath]) Unit
70+
, readdir :: Fn2 FilePath (JSCallback (Array FilePath)) Unit
7671
, utimes :: Fn4 FilePath Number Number (JSCallback Unit) Unit
7772
, readFile :: forall a opts. Fn3 FilePath { | opts } (JSCallback a) Unit
7873
, writeFile :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
7974
, appendFile :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
8075
, exists :: forall a. Fn2 FilePath (Boolean -> a) Unit
8176
}
8277

83-
foreign import mkEff
84-
"function mkEff(action) {\
85-
\ return action;\
86-
\}" :: forall eff a. (Unit -> a) -> Eff eff a
87-
8878
-- |
8979
-- Type synonym for callback functions.
9080
--
@@ -97,8 +87,7 @@ rename :: forall eff. FilePath
9787
-> FilePath
9888
-> Callback eff Unit
9989
-> Eff (fs :: FS | eff) Unit
100-
101-
rename oldFile newFile cb = mkEff $ \_ -> runFn3
90+
rename oldFile newFile cb = return $ runFn3
10291
fs.rename oldFile newFile (handleCallback cb)
10392

10493
-- |
@@ -109,7 +98,7 @@ truncate :: forall eff. FilePath
10998
-> Callback eff Unit
11099
-> Eff (fs :: FS | eff) Unit
111100

112-
truncate file len cb = mkEff $ \_ -> runFn3
101+
truncate file len cb = return $ runFn3
113102
fs.truncate file len (handleCallback cb)
114103

115104
-- |
@@ -121,7 +110,7 @@ chown :: forall eff. FilePath
121110
-> Callback eff Unit
122111
-> Eff (fs :: FS | eff) Unit
123112

124-
chown file uid gid cb = mkEff $ \_ -> runFn4
113+
chown file uid gid cb = return $ runFn4
125114
fs.chown file uid gid (handleCallback cb)
126115

127116
-- |
@@ -132,7 +121,7 @@ chmod :: forall eff. FilePath
132121
-> Callback eff Unit
133122
-> Eff (fs :: FS | eff) Unit
134123

135-
chmod file perms cb = mkEff $ \_ -> runFn3
124+
chmod file perms cb = return $ runFn3
136125
fs.chmod file (permsToString perms) (handleCallback cb)
137126

138127
-- |
@@ -142,7 +131,7 @@ stat :: forall eff. FilePath
142131
-> Callback eff Stats
143132
-> Eff (fs :: FS | eff) Unit
144133

145-
stat file cb = mkEff $ \_ -> runFn2
134+
stat file cb = return $ runFn2
146135
fs.stat file (handleCallback $ cb <<< (<$>) Stats)
147136

148137
-- |
@@ -153,7 +142,7 @@ link :: forall eff. FilePath
153142
-> Callback eff Unit
154143
-> Eff (fs :: FS | eff) Unit
155144

156-
link src dst cb = mkEff $ \_ -> runFn3
145+
link src dst cb = return $ runFn3
157146
fs.link src dst (handleCallback cb)
158147

159148
-- |
@@ -165,7 +154,7 @@ symlink :: forall eff. FilePath
165154
-> Callback eff Unit
166155
-> Eff (fs :: FS | eff) Unit
167156

168-
symlink src dest ty cb = mkEff $ \_ -> runFn4
157+
symlink src dest ty cb = return $ runFn4
169158
fs.symlink src dest (show ty) (handleCallback cb)
170159

171160
-- |
@@ -175,7 +164,7 @@ readlink :: forall eff. FilePath
175164
-> Callback eff FilePath
176165
-> Eff (fs :: FS | eff) Unit
177166

178-
readlink path cb = mkEff $ \_ -> runFn2
167+
readlink path cb = return $ runFn2
179168
fs.readlink path (handleCallback cb)
180169

181170
-- |
@@ -185,7 +174,7 @@ realpath :: forall eff. FilePath
185174
-> Callback eff FilePath
186175
-> Eff (fs :: FS | eff) Unit
187176

188-
realpath path cb = mkEff $ \_ -> runFn3
177+
realpath path cb = return $ runFn3
189178
fs.realpath path {} (handleCallback cb)
190179

191180
-- |
@@ -197,7 +186,7 @@ realpath' :: forall eff cache. FilePath
197186
-> Callback eff FilePath
198187
-> Eff (fs :: FS | eff) Unit
199188

200-
realpath' path cache cb = mkEff $ \_ -> runFn3
189+
realpath' path cache cb = return $ runFn3
201190
fs.realpath path cache (handleCallback cb)
202191

203192
-- |
@@ -207,7 +196,7 @@ unlink :: forall eff. FilePath
207196
-> Callback eff Unit
208197
-> Eff (fs :: FS | eff) Unit
209198

210-
unlink file cb = mkEff $ \_ -> runFn2
199+
unlink file cb = return $ runFn2
211200
fs.unlink file (handleCallback cb)
212201

213202
-- |
@@ -217,7 +206,7 @@ rmdir :: forall eff. FilePath
217206
-> Callback eff Unit
218207
-> Eff (fs :: FS | eff) Unit
219208

220-
rmdir file cb = mkEff $ \_ -> runFn2
209+
rmdir file cb = return $ runFn2
221210
fs.rmdir file (handleCallback cb)
222211

223212
-- |
@@ -237,17 +226,17 @@ mkdir' :: forall eff. FilePath
237226
-> Callback eff Unit
238227
-> Eff (fs :: FS | eff) Unit
239228

240-
mkdir' file perms cb = mkEff $ \_ -> runFn3
229+
mkdir' file perms cb = return $ runFn3
241230
fs.mkdir file (permsToString perms) (handleCallback cb)
242231

243232
-- |
244233
-- Reads the contents of a directory.
245234
--
246235
readdir :: forall eff. FilePath
247-
-> Callback eff [FilePath]
236+
-> Callback eff (Array FilePath)
248237
-> Eff (fs :: FS | eff) Unit
249238

250-
readdir file cb = mkEff $ \_ -> runFn2
239+
readdir file cb = return $ runFn2
251240
fs.readdir file (handleCallback cb)
252241

253242
-- |
@@ -259,7 +248,7 @@ utimes :: forall eff. FilePath
259248
-> Callback eff Unit
260249
-> Eff (fs :: FS | eff) Unit
261250

262-
utimes file atime mtime cb = mkEff $ \_ -> runFn4
251+
utimes file atime mtime cb = return $ runFn4
263252
fs.utimes file
264253
(ms (toEpochMilliseconds atime) / 1000)
265254
(ms (toEpochMilliseconds mtime) / 1000)
@@ -274,7 +263,7 @@ readFile :: forall eff. FilePath
274263
-> Callback eff Buffer
275264
-> Eff (fs :: FS | eff) Unit
276265

277-
readFile file cb = mkEff $ \_ -> runFn3
266+
readFile file cb = return $ runFn3
278267
fs.readFile file {} (handleCallback cb)
279268

280269
-- |
@@ -285,7 +274,7 @@ readTextFile :: forall eff. Encoding
285274
-> Callback eff String
286275
-> Eff (fs :: FS | eff) Unit
287276

288-
readTextFile encoding file cb = mkEff $ \_ -> runFn3
277+
readTextFile encoding file cb = return $ runFn3
289278
fs.readFile file { encoding: show encoding } (handleCallback cb)
290279

291280
-- |
@@ -296,7 +285,7 @@ writeFile :: forall eff. FilePath
296285
-> Callback eff Unit
297286
-> Eff (fs :: FS | eff) Unit
298287

299-
writeFile file buff cb = mkEff $ \_ -> runFn4
288+
writeFile file buff cb = return $ runFn4
300289
fs.writeFile file buff {} (handleCallback cb)
301290

302291
-- |
@@ -308,7 +297,7 @@ writeTextFile :: forall eff. Encoding
308297
-> Callback eff Unit
309298
-> Eff (fs :: FS | eff) Unit
310299

311-
writeTextFile encoding file buff cb = mkEff $ \_ -> runFn4
300+
writeTextFile encoding file buff cb = return $ runFn4
312301
fs.writeFile file buff { encoding: show encoding } (handleCallback cb)
313302

314303
-- |
@@ -319,7 +308,7 @@ appendFile :: forall eff. FilePath
319308
-> Callback eff Unit
320309
-> Eff (fs :: FS | eff) Unit
321310

322-
appendFile file buff cb = mkEff $ \_ -> runFn4
311+
appendFile file buff cb = return $ runFn4
323312
fs.appendFile file buff {} (handleCallback cb)
324313

325314
-- |
@@ -331,7 +320,7 @@ appendTextFile :: forall eff. Encoding
331320
-> Callback eff Unit
332321
-> Eff (fs :: FS | eff) Unit
333322

334-
appendTextFile encoding file buff cb = mkEff $ \_ -> runFn4
323+
appendTextFile encoding file buff cb = return $ runFn4
335324
fs.appendFile file buff { encoding: show encoding } (handleCallback cb)
336325

337326
-- |
@@ -340,5 +329,5 @@ appendTextFile encoding file buff cb = mkEff $ \_ -> runFn4
340329
exists :: forall eff. FilePath
341330
-> (Boolean -> Eff eff Unit)
342331
-> Eff (fs :: FS | eff) Unit
343-
exists file cb = mkEff $ \_ -> runFn2
332+
exists file cb = return $ runFn2
344333
fs.exists file $ \b -> runPure (unsafeInterleaveEff (cb b))

src/Node/FS/Stats.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* global require */
2+
/* global exports */
3+
"use strict";
4+
5+
// module Node.FS.Stats
6+
7+
exports.showStatsObj = require('util').inspect;
8+
9+
exports.statsMethod = function (m, s) {
10+
return s[m]();
11+
}

src/Node/FS/Stats.purs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,12 @@ type StatsObj =
4242
--
4343
data Stats = Stats StatsObj
4444

45-
foreign import showStatsObj
46-
"function showStatsObj (obj) {\
47-
\ return require('util').inspect(obj);\
48-
\}" :: StatsObj -> String
45+
foreign import showStatsObj :: StatsObj -> String
4946

5047
instance showStats :: Show Stats where
5148
show (Stats o) = "Stats " ++ showStatsObj o
5249

53-
foreign import statsMethod
54-
"function statsMethod(m, s) {\
55-
\ return s[m]();\
56-
\}" :: Fn2 String StatsObj Boolean
50+
foreign import statsMethod :: Fn2 String StatsObj Boolean
5751

5852
isFile :: Stats -> Boolean
5953
isFile (Stats s) = runFn2 statsMethod "isFile" s

src/Node/FS/Sync.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* global require */
2+
/* global exports */
3+
"use strict";
4+
5+
// module Node.FS.Sync
6+
7+
var fs = require('fs');
8+
exports.fs = fs;
9+
10+
exports.handleCallbackImpl = function (left, right, f) {
11+
return function (err, value) {
12+
if (err) {
13+
f(left(err))();
14+
} else {
15+
f(right(value))();
16+
}
17+
};
18+
};
19+
20+
exports.createSync = fs.openSync;
21+
exports.writeSeqSync = fs.writeSync;
22+
exports.readSeqSync = fs.readSync;

0 commit comments

Comments
 (0)