Skip to content

Commit 13fa204

Browse files
Add more FFI; fix FFI; some uncurried FFI (#44)
1 parent bcaa839 commit 13fa204

File tree

4 files changed

+74
-10
lines changed

4 files changed

+74
-10
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,30 @@ Breaking changes:
1919
BySignal signal -> ...
2020
```
2121
See https://pursuit.purescript.org/packages/purescript-node-event-emitter/3.0.0/docs/Node.EventEmitter for more details.
22+
- Update `pid` type signature to return `Maybe Pid` rather than `Pid` (#44 by @JordanMartinez)
23+
- Update `kill` returned value from `Effect Unit` to `Effect Boolean` (#44 by @JordanMartinez)
2224

2325

2426
New features:
2527
- Added event handler for `spawn` event (#43 by @JordanMartinez)
28+
- Added missing APIs (#44 by @JordanMartinez)
29+
30+
- exitCode
31+
- kill (no signal specified)
32+
- kill' (kill with a `String` signal)
33+
- killSignal (kill with an ADT `Signal` arg)
34+
- killed
35+
- signalCode
36+
- spawnArgs
37+
- spawnFile
2638

2739
Bugfixes:
2840

2941
Other improvements:
3042
- Bumped CI's node version to `lts/*` (#41 by @JordanMartinez)
3143
- Updated CI `actions/checkout` and `actions/setup-nodee` to `v3` (#41 by @JordanMartinez)
3244
- Format codebase & enforce formatting in CI via purs-tidy (#42 by @JordanMartinez)
45+
- Migrate more FFI to uncurried functions (#44 by @JordanMartinez)
3346

3447
## [v9.0.0](https://github.com/purescript-node/purescript-node-child-process/releases/tag/v9.0.0) - 2022-04-29
3548

src/Node/ChildProcess.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ export function unsafeFromNullable(msg) {
99
};
1010
}
1111

12+
export const connectedImpl = (cp) => cp.connected;
13+
export const disconnectImpl = (cp) => cp.disconnect();
14+
export const exitCodeImpl = (cp) => cp.exitCode;
15+
export const pidImpl = (cp) => cp.pid;
16+
export const killImpl = (cp) => cp.kill();
17+
export const killStrImpl = (cp, str) => cp.kill(str);
18+
export const killedImpl = (cp) => cp.killed;
19+
export const signalCodeImpl = (cp) => cp.signalCode;
20+
export const spawnArgs = (cp) => cp.spawnArgs;
21+
export const spawnFile = (cp) => cp.spawnFile;
22+
1223
export function spawnImpl(command) {
1324
return args => opts => () => spawn(command, args, opts);
1425
}

src/Node/ChildProcess.purs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ module Node.ChildProcess
2626
, stderr
2727
, pid
2828
, connected
29+
, disconnect
30+
, exitCode
2931
, kill
32+
, kill'
33+
, killSignal
34+
, killed
35+
, signalCode
3036
, send
31-
, disconnect
3237
, Error
3338
, toStandardError
3439
, Exit(..)
@@ -61,7 +66,7 @@ import Data.Posix.Signal (Signal)
6166
import Data.Posix.Signal as Signal
6267
import Effect (Effect)
6368
import Effect.Exception as Exception
64-
import Effect.Uncurried (EffectFn2, mkEffectFn1, mkEffectFn2)
69+
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2)
6570
import Foreign (Foreign)
6671
import Foreign.Object (Object)
6772
import Node.Buffer (Buffer)
@@ -152,13 +157,22 @@ foreign import unsafeFromNullable :: forall a. String -> Nullable a -> a
152157

153158
-- | The process ID of a child process. Note that if the process has already
154159
-- | exited, another process may have taken the same ID, so be careful!
155-
pid :: ChildProcess -> Pid
156-
pid = _.pid <<< runChildProcess
160+
pid :: ChildProcess -> Effect (Maybe Pid)
161+
pid cp = map toMaybe $ runEffectFn1 pidImpl cp
162+
163+
foreign import pidImpl :: EffectFn1 (ChildProcess) (Nullable Pid)
157164

158165
-- | Indicates whether it is still possible to send and receive
159166
-- | messages from the child process.
160167
connected :: ChildProcess -> Effect Boolean
161-
connected (ChildProcess cp) = mkEffect \_ -> cp.connected
168+
connected cp = runEffectFn1 connectedImpl cp
169+
170+
foreign import connectedImpl :: EffectFn1 (ChildProcess) (Boolean)
171+
172+
exitCode :: ChildProcess -> Effect (Maybe Int)
173+
exitCode cp = map toMaybe $ runEffectFn1 exitCodeImpl cp
174+
175+
foreign import exitCodeImpl :: EffectFn1 (ChildProcess) (Nullable Int)
162176

163177
-- | Send messages to the (`nodejs`) child process.
164178
-- |
@@ -174,7 +188,19 @@ send msg handle (ChildProcess cp) = mkEffect \_ -> runFn2 cp.send msg handle
174188

175189
-- | Closes the IPC channel between parent and child.
176190
disconnect :: ChildProcess -> Effect Unit
177-
disconnect = _.disconnect <<< runChildProcess
191+
disconnect cp = runEffectFn1 disconnectImpl cp
192+
193+
foreign import disconnectImpl :: EffectFn1 (ChildProcess) (Unit)
194+
195+
kill :: ChildProcess -> Effect Boolean
196+
kill cp = runEffectFn1 killImpl cp
197+
198+
foreign import killImpl :: EffectFn1 (ChildProcess) (Boolean)
199+
200+
kill' :: String -> ChildProcess -> Effect Boolean
201+
kill' sig cp = runEffectFn2 killStrImpl cp sig
202+
203+
foreign import killStrImpl :: EffectFn2 (ChildProcess) (String) (Boolean)
178204

179205
-- | Send a signal to a child process. In the same way as the
180206
-- | [unix kill(2) system call](https://linux.die.net/man/2/kill),
@@ -184,8 +210,22 @@ disconnect = _.disconnect <<< runChildProcess
184210
-- | and the signal. They can vary from system to system.
185211
-- | The child process might emit an `"error"` event if the signal
186212
-- | could not be delivered.
187-
kill :: Signal -> ChildProcess -> Effect Unit
188-
kill sig (ChildProcess cp) = mkEffect \_ -> cp.kill (Signal.toString sig)
213+
killSignal :: Signal -> ChildProcess -> Effect Boolean
214+
killSignal sig cp = kill' (Signal.toString sig) cp
215+
216+
killed :: ChildProcess -> Effect Boolean
217+
killed cp = runEffectFn1 killedImpl cp
218+
219+
signalCode :: ChildProcess -> Effect (Maybe String)
220+
signalCode cp = map toMaybe $ runEffectFn1 signalCodeImpl cp
221+
222+
foreign import signalCodeImpl :: EffectFn1 (ChildProcess) (Nullable String)
223+
224+
foreign import killedImpl :: EffectFn1 (ChildProcess) (Boolean)
225+
226+
foreign import spawnArgs :: ChildProcess -> Array String
227+
228+
foreign import spawnFile :: ChildProcess -> String
189229

190230
mkEffect :: forall a. (Unit -> a) -> Effect a
191231
mkEffect = unsafeCoerce

test/Main.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ main = do
2424

2525
log "doesn't perform effects too early"
2626
spawn "ls" [ "-la" ] defaultSpawnOptions >>= \ls -> do
27-
let _ = kill SIGTERM ls
27+
let _ = kill ls
2828
ls # on_ exitH \exit ->
2929
case exit of
3030
Normally 0 ->
@@ -34,7 +34,7 @@ main = do
3434

3535
log "kills processes"
3636
spawn "ls" [ "-la" ] defaultSpawnOptions >>= \ls -> do
37-
_ <- kill SIGTERM ls
37+
_ <- kill ls
3838
ls # on_ exitH \exit ->
3939
case exit of
4040
BySignal SIGTERM ->

0 commit comments

Comments
 (0)