File: JavaScript.hs

package info (click to toggle)
ghc 9.6.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 158,216 kB
  • sloc: haskell: 648,228; ansic: 81,656; cpp: 11,808; javascript: 8,444; sh: 5,831; fortran: 3,527; python: 3,277; asm: 2,523; makefile: 2,298; yacc: 1,570; lisp: 532; xml: 196; perl: 145; csh: 2
file content (308 lines) | stat: -rw-r--r-- 10,537 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE JavaScriptFFI #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE CPP #-}

{-
  Child process support for JavaScript running on the node.js platform.

  Other platforms such as browsers will accept the JavaScript code, but all
  operations will result in unsupported operation exceptions.
 -}

#include "HsProcessConfig.h"

module System.Process.JavaScript
    ( mkProcessHandle
    , translateInternal
    , createProcess_Internal
    , withCEnvironment
    , closePHANDLE
    , startDelegateControlC
    , endDelegateControlC
    , stopDelegateControlC
    , isDefaultSignal
    , ignoreSignal
    , defaultSignal
    , createPipeInternal
    , createPipeInternalFd
    , interruptProcessGroupOfInternal
    , getProcessId
    , getCurrentProcessId
    ) where

import Control.Concurrent.MVar
import Control.Exception (throwIO)

import Data.Char (isAlphaNum)

import System.Exit
import System.IO
import System.IO.Error
import qualified System.Posix.Internals as Posix

import Foreign.C
import Foreign.Marshal
import Foreign.Ptr

import GHC.IO.Handle.FD (mkHandleFromFD)
import GHC.IO.Device (IODeviceType(..))
import GHC.IO.Encoding (getLocaleEncoding)
import GHC.IO.Exception
import qualified GHC.IO.FD as FD

import GHC.JS.Prim

import System.Process.Common hiding (mb_delegate_ctlc, mbPipe)

mkProcessHandle :: JSVal -> Bool -> IO ProcessHandle
mkProcessHandle p mb_delegate_ctlc = do
  m <- newMVar (OpenHandle p)
  ml <- newMVar ()
  return (ProcessHandle m mb_delegate_ctlc ml)

closePHANDLE :: JSVal -> IO ()
closePHANDLE _ = return ()

getProcessId :: PHANDLE -> IO Int
getProcessId ph =
  throwErrnoIfMinus1 "getProcessId" (js_getProcessId ph)

getCurrentProcessId :: IO Int
getCurrentProcessId =
  throwErrnoIfMinus1 "getCurrentProcessId" js_getCurrentProcessId

startDelegateControlC :: IO ()
startDelegateControlC =
  throwErrnoIfMinus1_ "startDelegateControlC" js_startDelegateControlC

stopDelegateControlC :: IO ()
stopDelegateControlC =
  throwErrnoIfMinus1_ "stopDelegateControlC" js_stopDelegateControlC

endDelegateControlC :: ExitCode -> IO ()
endDelegateControlC (ExitFailure (-2)) = throwIO UserInterrupt -- SIGINT
endDelegateControlC _                  = pure ()

ignoreSignal, defaultSignal :: CLong
ignoreSignal  = CONST_SIG_IGN
defaultSignal = CONST_SIG_DFL

isDefaultSignal :: CLong -> Bool
isDefaultSignal = (== defaultSignal)

interruptProcessGroupOfInternal
    :: ProcessHandle    -- ^ A process in the process group
    -> IO ()
interruptProcessGroupOfInternal ph =
      withProcessHandle ph $ \p_ -> do
        case p_ of
            OpenExtHandle{} -> return ()
            ClosedHandle  _ -> return ()
            OpenHandle    h ->
                throwErrnoIfMinus1_ "interruptProcessGroupOfInternal"
                                    (js_interruptProcessGroupOf h)

translateInternal :: String -> String
translateInternal "" = "''"
translateInternal str
   -- goodChar is a pessimistic predicate, such that if an argument is
   -- non-empty and only contains goodChars, then there is no need to
   -- do any quoting or escaping
 | all goodChar str = str
 | otherwise        = '\'' : foldr escape "'" str
  where escape '\'' = showString "'\\''"
        escape c    = showChar c
        goodChar c = isAlphaNum c || c `elem` "-_.,/"

-- node.js does not appear to have any built-in facilities
-- for creating pipes, so we leave this as an unsupported operation
-- for now
createPipeInternal :: IO (Handle, Handle)
createPipeInternal = ioError
  (ioeSetLocation unsupportedOperation "createPipeInternal")

createPipeInternalFd :: IO (Posix.FD, Posix.FD)
createPipeInternalFd = ioError
  (ioeSetLocation unsupportedOperation "createPipeInternalFd")

withCEnvironment :: [(String,String)] -> (Ptr CString  -> IO a) -> IO a
withCEnvironment envir act =
  let env' = map (\(name, val) -> name ++ ('=':val)) envir
  in withMany withCString env' (\pEnv -> withArray0 nullPtr pEnv act)

commandToProcess :: CmdSpec -> IO (FilePath, [String])
commandToProcess cmd =
  case cmd of
    ShellCommand xs   -> c2p (toJSString xs) jsNull
    RawCommand c args -> c2p (toJSString c) =<< toJSStrings args
  where
    c2p c as = do
      r <- throwErrnoIfJSNull "commandToProcess" (js_commandToProcess c as)
      fromJSStrings r >>= \case
        (x:xs) -> pure (x,xs)
        _      -> error "commandToProcess: empty list"

-- -----------------------------------------------------------------------------
-- JavaScript nodejs runProcess with signal handling in the child

createProcess_Internal
  :: String
       -- ^ Function name (for error messages).
       --
       --   This can be any 'String', but will typically be the name of the caller.
       --   E.g., 'spawnProcess' passes @"spawnProcess"@ here when calling
       --   'createProcess_'.
  -> CreateProcess
  -> IO ProcRetHandles
createProcess_Internal fun CreateProcess{ cmdspec = cmdsp,
                                  cwd = mb_cwd,
                                  env = mb_env,
                                  std_in = mb_stdin,
                                  std_out = mb_stdout,
                                  std_err = mb_stderr,
                                  close_fds = mb_close_fds,
                                  create_group = mb_create_group,
                                  delegate_ctlc = mb_delegate_ctlc,
                                  new_session = mb_new_session,
                                  child_user = mb_child_user,
                                  child_group = mb_child_group }
 = do
  (cmd, args) <- commandToProcess cmdsp
  withFilePathException cmd $ do
     fdin  <- mbFd fun fd_stdin  mb_stdin
     fdout <- mbFd fun fd_stdout mb_stdout
     fderr <- mbFd fun fd_stderr mb_stderr
     env'  <- maybe (pure jsNull)
                    (toJSStrings . concatMap (\(x,y) -> [x,y]))
                    mb_env

     let cwd' = maybe jsNull toJSString mb_cwd
     let c1 = toJSString cmd
     c2 <- case args of
               [] -> return jsNull
               _  -> toJSStrings args

     r <- throwErrnoIfJSNull fun $
             js_runInteractiveProcess c1
                                      c2
                                      cwd'
                                      env'
                                      fdin
                                      fdout
                                      fderr
                                      mb_close_fds
                                      mb_create_group
                                      mb_delegate_ctlc
                                      mb_new_session
                                      (maybe (-1) fromIntegral mb_child_group)
                                      (maybe (-1) fromIntegral mb_child_user)

     fdin_r:fdout_r:fderr_r:_ <-
         map (stdFD . fromIntegral) <$> (fromJSInts =<< getProp r "fds")

     hndStdInput  <- mbPipe mb_stdin  fdin_r  WriteMode
     hndStdOutput <- mbPipe mb_stdout fdout_r ReadMode
     hndStdError  <- mbPipe mb_stderr fderr_r ReadMode

     ph <- mkProcessHandle r mb_delegate_ctlc
     return $ ProcRetHandles { hStdInput = hndStdInput
                             , hStdOutput = hndStdOutput
                             , hStdError = hndStdError
                             , procHandle = ph
                             }

mbPipe :: StdStream -> FD.FD -> IOMode -> IO (Maybe Handle)
mbPipe CreatePipe fd mode = do
  enc <- getLocaleEncoding
  fmap Just (mkHandleFromFD fd
                            Stream
                            ("fd: " ++ show fd)
                            mode
                            False {-is_socket-}
                            (Just enc))
mbPipe _ _ _ = do
  return Nothing

stdFD :: CInt -> FD.FD
stdFD fd = FD.FD { FD.fdFD = fd
                 , FD.fdIsNonBlocking = 0
                 }

-- -----------------------------------------------------------------------------
-- Some helpers for dealing with JavaScript values

-- JavaScript value type synonyms, for readability
type JSArray  = JSVal
type JSString = JSVal

fromJSStrings :: JSVal -> IO [String]
fromJSStrings x = fmap (map fromJSString) (fromJSArray x)

fromJSInts :: JSVal -> IO [Int]
fromJSInts x = map fromJSInt <$> fromJSArray x

toJSStrings :: [String] -> IO JSVal
toJSStrings xs = toJSArray (map toJSString xs)

throwErrnoIfJSNull :: String -> IO JSVal -> IO JSVal
throwErrnoIfJSNull msg m = do
  r <- m
  if isNull r then throwErrno msg
              else return r

-- -----------------------------------------------------------------------------
-- Foreign imports from process.js

-- run an interactive process. Note that this foreign import is asynchronous
-- (interruptible) since it waits until the process has spawned (or an error
-- has occurred.
--
-- this should only be a short time, so it should be safe to call this from
-- an uninterruptible mask.

foreign import javascript interruptible "h$process_runInteractiveProcess"
  js_runInteractiveProcess
        :: JSString     -- ^ command or program
        -> JSArray      -- ^ arguments, null if it's a raw command
        -> JSString     -- ^ working dir, null for current
        -> JSArray      -- ^ environment, null for existing
        -> CInt         -- ^ stdin fd
        -> CInt         -- ^ stdout fd
        -> CInt         -- ^ stderr fd
        -> Bool         -- ^ close file descriptors in child (currently unsupported)
        -> Bool         -- ^ create a new process group
        -> Bool         -- ^ delegate ctrl-c
        -> Bool         -- ^ create a new session
        -> Int          -- ^ set child GID (-1 for unchanged)
        -> Int          -- ^ set child UID (-1 for unchanged)
        -> IO JSVal     -- ^ process handle (null if an error occurred)

foreign import javascript safe "h$process_commandToProcess"
  js_commandToProcess
        :: JSString
        -> JSArray
        -> IO JSArray

foreign import javascript unsafe "h$process_interruptProcessGroupOf"
  js_interruptProcessGroupOf
        :: PHANDLE
        -> IO Int

foreign import javascript unsafe "h$process_startDelegateControlC"
  js_startDelegateControlC
        :: IO Int

foreign import javascript unsafe "h$process_stopDelegateControlC"
  js_stopDelegateControlC
        :: IO Int

foreign import javascript unsafe "h$process_getCurrentProcessId"
  js_getCurrentProcessId
        :: IO Int

foreign import javascript unsafe "h$process_getProcessId"
  js_getProcessId
        :: PHANDLE
        -> IO Int