Skip to content

Commit 9b4a489

Browse files
Migrate process things (#14)
* Cleanup Process-related FFI * Sync signals with 'signal-exitv4'
1 parent 32a1fee commit 9b4a489

File tree

2 files changed

+19
-39
lines changed

2 files changed

+19
-39
lines changed

src/Node/Library/Execa/SignalExit.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@ export function processCallFn(originalProcessReallyExit, exitCode) {
1414
return originalProcessReallyExit.call(global.process, exitCode);
1515
}
1616

17-
export function processKill(pid, sig) {
18-
global.process.kill(pid, sig);
19-
}
20-
21-
export function processListenersLength(sig) {
22-
return global.process.listeners(sig).length;
23-
}
24-
25-
export function processOn(sig, listener) {
26-
return global.process.on(sig, listener);
27-
}
28-
29-
export function processOff(sig, listener) {
30-
return global.process.off(sig, listener);
31-
}
32-
3317
export function customProcessEmit(cb) {
3418
return function (ev, arg) {
3519
const thisArg = this;
@@ -38,6 +22,3 @@ export function customProcessEmit(cb) {
3822
};
3923
}
4024

41-
export function processExitCode() {
42-
return global.process.exitCode;
43-
}

src/Node/Library/Execa/SignalExit.purs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Data.Foldable (traverse_)
1414
import Data.Maybe (Maybe(..), fromMaybe)
1515
import Data.Monoid (guard)
1616
import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable)
17-
import Data.Posix (Pid)
1817
import Data.Set (Set)
1918
import Data.Set as Set
2019
import Data.Traversable (for)
@@ -23,10 +22,12 @@ import Effect.Exception (try)
2322
import Effect.Ref (Ref)
2423
import Effect.Ref as Ref
2524
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, mkEffectFn1, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3)
26-
import Node.EventEmitter (EventEmitter, EventHandle(..), on, unsafeEmitFn)
25+
import Node.EventEmitter (EventEmitter, EventHandle(..), listenerCount, on, unsafeEmitFn)
2726
import Node.EventEmitter as EventEmitter
2827
import Node.Platform (Platform(..))
28+
import Node.Process (Process, mkSignalH', process)
2929
import Node.Process as Process
30+
import Unsafe.Coerce (unsafeCoerce)
3031

3132
foreign import unsafeProcessHasProp :: EffectFn1 String Boolean
3233
foreign import unsafeReadProcessProp :: forall a. EffectFn1 String a
@@ -36,12 +37,10 @@ foreign import data ProcessEmitFn :: Type
3637
foreign import data ProcessReallyExitFn :: Type
3738
foreign import processCallFn :: EffectFn2 ProcessReallyExitFn (Nullable Int) Unit
3839

39-
foreign import processOn :: forall cb. EffectFn2 String cb Unit
40-
foreign import processOff :: forall cb. EffectFn2 String cb Unit
41-
foreign import processKill :: EffectFn2 Pid String Unit
42-
foreign import processListenersLength :: EffectFn1 String Int
4340
foreign import customProcessEmit :: EffectFn3 (EffectFn1 ProcessEmitFn Boolean) String (Nullable Int) Boolean -> EffectFn2 String (Nullable Int) Boolean
44-
foreign import processExitCode :: Effect (Nullable Int)
41+
42+
processToEventEmitter :: Process -> EventEmitter
43+
processToEventEmitter = unsafeCoerce
4544

4645
isWin :: Boolean
4746
isWin = Just Win32 == Process.platform
@@ -148,17 +147,17 @@ onExit' cb options = do
148147
-- for that to occur.
149148
signalListeners <- for signals \sig -> map hush $ try do
150149
let listener = mkListener sig countRef
151-
runEffectFn2 processOn sig listener
150+
rm <- process # on (mkSignalH' sig) listener
152151
pure $ void $ try do
153-
runEffectFn2 processOff sig listener
152+
rm
154153
Ref.write signalListeners signalListenersRef
155154
runEffectFn2 unsafeWriteProcessProp "emit" processEmitFn
156155
runEffectFn2 unsafeWriteProcessProp "reallyExit" processReallyExitFn
157156

158157
-- Good
159158
mkListener :: String -> Ref Int -> Effect Unit
160159
mkListener sig countRef = do
161-
listenersLen <- runEffectFn1 processListenersLength sig
160+
listenersLen <- listenerCount (processToEventEmitter process) sig
162161
count <- Ref.read countRef
163162
when (listenersLen == count) do
164163
unload
@@ -167,7 +166,7 @@ onExit' cb options = do
167166
-- "SIGHUP" throws an `ENOSYS` error on Windows,
168167
-- so use a supported signal instead
169168
let sig' = if isWin && sig == "SIGHUP" then "SIGINT" else sig
170-
runEffectFn2 processKill Process.pid sig'
169+
Process.killStr Process.pid sig'
171170

172171
processReallyExitFn = mkEffectFn1 \(code :: Nullable Int) -> do
173172
{ emitter
@@ -183,9 +182,10 @@ onExit' cb options = do
183182
{ originalProcessEmit } <- getGlobalRecOnProcessObject
184183
if ev == exitEvent then do
185184
exitCode <- case toMaybe arg of
186-
Nothing -> processExitCode
185+
Nothing ->
186+
map toNullable $ Process.getExitCode
187187
Just exitCode' -> do
188-
runEffectFn2 unsafeWriteProcessProp exitEvent exitCode'
188+
Process.setExitCode exitCode'
189189
pure $ notNull exitCode'
190190

191191
ret <- runEffectFn1 runOriginalProcessEmit originalProcessEmit
@@ -266,17 +266,17 @@ getGlobalRecOnProcessObject =
266266
-- state from which it is not safe to try and enter JS
267267
-- listeners.
268268
signals :: Array String
269-
signals = normal <> windows <> linux
269+
signals = normal <> nonWindows <> linux
270270
where
271271
normal =
272-
[ "SIGABRT"
273-
, "SIGALRM"
274-
, "SIGHUP"
272+
[ "SIGHUP"
275273
, "SIGINT"
276274
, "SIGTERM"
277275
]
278-
windows = guard isWin
279-
[ "SIGVTALRM"
276+
nonWindows = guard (not isWin)
277+
[ "SIGABRT"
278+
, "SIGALRM"
279+
, "SIGVTALRM"
280280
, "SIGXCPU"
281281
, "SIGXFSZ"
282282
, "SIGUSR2"
@@ -297,5 +297,4 @@ signals = normal <> windows <> linux
297297
, "SIGPOLL"
298298
, "SIGPWR"
299299
, "SIGSTKFLT"
300-
, "SIGUNUSED"
301300
]

0 commit comments

Comments
 (0)