@@ -56,37 +56,38 @@ module DAP.Adaptor
5656import Control.Concurrent.Lifted ( fork , killThread )
5757import Control.Exception ( throwIO )
5858import Control.Concurrent.STM ( atomically , readTVarIO , modifyTVar' )
59- import Control.Monad ( when , unless )
59+ import Control.Monad ( when , unless , void )
6060import Control.Monad.Except ( runExceptT , throwError )
6161import Control.Monad.State ( runStateT , gets , gets , modify' )
62- import Control.Monad.Reader
62+ import Control.Monad.IO.Class ( liftIO )
63+ import Control.Monad.Reader ( asks , ask , runReaderT )
6364import Data.Aeson ( FromJSON , Result (.. ), fromJSON )
6465import Data.Aeson.Encode.Pretty ( encodePretty )
6566import Data.Aeson.Types ( object , Key , KeyValue ((.=) ), ToJSON )
67+ import Data.IORef ( readIORef , writeIORef )
6668import Data.Text ( unpack , pack )
6769import Network.Socket ( SockAddr )
6870import System.IO ( Handle )
6971import qualified Data.ByteString.Lazy.Char8 as BL8
7072import qualified Data.ByteString.Char8 as BS
7173import qualified Data.HashMap.Strict as H
72- import Data.IORef
7374----------------------------------------------------------------------------
7475import DAP.Types
7576import DAP.Utils
7677import DAP.Internal
7778----------------------------------------------------------------------------
78- logWarn :: BL8. ByteString -> Adaptor app r ()
79+ logWarn :: BL8. ByteString -> Adaptor app request ()
7980logWarn msg = logWithAddr WARN Nothing (withBraces msg)
8081----------------------------------------------------------------------------
81- logError :: BL8. ByteString -> Adaptor app r ()
82+ logError :: BL8. ByteString -> Adaptor app request ()
8283logError msg = logWithAddr ERROR Nothing (withBraces msg)
8384----------------------------------------------------------------------------
84- logInfo :: BL8. ByteString -> Adaptor app r ()
85+ logInfo :: BL8. ByteString -> Adaptor app request ()
8586logInfo msg = logWithAddr INFO Nothing (withBraces msg)
8687----------------------------------------------------------------------------
8788-- | Meant for internal consumption, used to signify a message has been
8889-- SENT from the server
89- debugMessage :: BL8. ByteString -> Adaptor app r ()
90+ debugMessage :: BL8. ByteString -> Adaptor app request ()
9091debugMessage msg = do
9192 shouldLog <- getDebugLogging
9293 addr <- getAddress
@@ -95,7 +96,7 @@ debugMessage msg = do
9596 $ logger DEBUG addr (Just SENT ) msg
9697----------------------------------------------------------------------------
9798-- | Meant for external consumption
98- logWithAddr :: Level -> Maybe DebugStatus -> BL8. ByteString -> Adaptor app r ()
99+ logWithAddr :: Level -> Maybe DebugStatus -> BL8. ByteString -> Adaptor app request ()
99100logWithAddr level status msg = do
100101 addr <- getAddress
101102 liftIO (logger level addr status msg)
@@ -115,22 +116,22 @@ logger level addr maybeDebug msg = do
115116 , msg
116117 ]
117118----------------------------------------------------------------------------
118- getDebugLogging :: Adaptor app r Bool
119+ getDebugLogging :: Adaptor app request Bool
119120getDebugLogging = asks (debugLogging . serverConfig)
120121----------------------------------------------------------------------------
121- getServerCapabilities :: Adaptor app r Capabilities
122+ getServerCapabilities :: Adaptor app request Capabilities
122123getServerCapabilities = asks (serverCapabilities . serverConfig)
123124----------------------------------------------------------------------------
124- getAddress :: Adaptor app r SockAddr
125+ getAddress :: Adaptor app request SockAddr
125126getAddress = asks address
126127----------------------------------------------------------------------------
127- getHandle :: Adaptor app r Handle
128+ getHandle :: Adaptor app request Handle
128129getHandle = asks handle
129130----------------------------------------------------------------------------
130131getRequestSeqNum :: Adaptor app Request Seq
131132getRequestSeqNum = asks (requestSeqNum . request)
132133----------------------------------------------------------------------------
133- getDebugSessionId :: Adaptor app r SessionId
134+ getDebugSessionId :: Adaptor app request SessionId
134135getDebugSessionId = do
135136 var <- asks (sessionId)
136137 res <- liftIO $ readIORef var
@@ -142,7 +143,7 @@ getDebugSessionId = do
142143 let err = " No Debug Session has started"
143144 sendError (ErrorMessage (pack err)) Nothing
144145----------------------------------------------------------------------------
145- setDebugSessionId :: SessionId -> Adaptor app r ()
146+ setDebugSessionId :: SessionId -> Adaptor app request ()
146147setDebugSessionId session = do
147148 var <- asks sessionId
148149 liftIO $ writeIORef var (Just session)
@@ -167,21 +168,21 @@ registerNewDebugSession
167168 -- > withAdaptor $ sendOutputEvent defaultOutputEvent { outputEventOutput = output }
168169 -- > ]
169170 --
170- -> Adaptor app r ()
171+ -> Adaptor app request ()
171172registerNewDebugSession k v debuggerConcurrentActions = do
172173 store <- asks appStore
173174 lcl <- ask
174175 let lcl' = lcl { request = () }
175176 let emptyState = AdaptorState MessageTypeEvent []
176177 debuggerThreadState <- liftIO $
177178 DebuggerThreadState
178- <$> sequence [fork $ action (runAdaptorWith lcl' emptyState " s " ) | action <- debuggerConcurrentActions]
179+ <$> sequence [fork $ action (runAdaptorWith lcl' emptyState) | action <- debuggerConcurrentActions]
179180 liftIO . atomically $ modifyTVar' store (H. insert k (debuggerThreadState, v))
180181 logInfo $ BL8. pack $ " Registered new debug session: " <> unpack k
181182 setDebugSessionId k
182183
183184----------------------------------------------------------------------------
184- updateDebugSession :: (app -> app ) -> Adaptor app r ()
185+ updateDebugSession :: (app -> app ) -> Adaptor app request ()
185186updateDebugSession updateFun = do
186187 sessionId <- getDebugSessionId
187188 store <- asks appStore
@@ -192,7 +193,7 @@ getDebugSession = do
192193 (_, _, app) <- getDebugSessionWithThreadIdAndSessionId
193194 pure app
194195----------------------------------------------------------------------------
195- getDebugSessionWithThreadIdAndSessionId :: Adaptor app r (SessionId , DebuggerThreadState , app )
196+ getDebugSessionWithThreadIdAndSessionId :: Adaptor app request (SessionId , DebuggerThreadState , app )
196197getDebugSessionWithThreadIdAndSessionId = do
197198 sessionId <- getDebugSessionId
198199 appStore <- liftIO . readTVarIO =<< getAppStore
@@ -212,7 +213,7 @@ getDebugSessionWithThreadIdAndSessionId = do
212213-- | Whenever a debug Session ends (cleanly or otherwise) this function
213214-- will remove the local debugger communication state from the global state
214215----------------------------------------------------------------------------
215- destroyDebugSession :: Adaptor app r ()
216+ destroyDebugSession :: Adaptor app request ()
216217destroyDebugSession = do
217218 (sessionId, DebuggerThreadState {.. }, _) <- getDebugSessionWithThreadIdAndSessionId
218219 store <- getAppStore
@@ -221,7 +222,7 @@ destroyDebugSession = do
221222 atomically $ modifyTVar' store (H. delete sessionId)
222223 logInfo $ BL8. pack $ " SessionId " <> unpack sessionId <> " ended"
223224----------------------------------------------------------------------------
224- getAppStore :: Adaptor app r (AppStore app )
225+ getAppStore :: Adaptor app request (AppStore app )
225226getAppStore = asks appStore
226227----------------------------------------------------------------------------
227228getCommand :: Adaptor app Request Command
@@ -231,7 +232,7 @@ getCommand = command <$> asks request
231232-- Sends a raw JSON payload to the editor. No "seq", "type" or "command" fields are set.
232233-- The message is still encoded with the ProtocolMessage Header, byte count, and CRLF.
233234--
234- sendRaw :: ToJSON value => value -> Adaptor app r ()
235+ sendRaw :: ToJSON value => value -> Adaptor app request ()
235236sendRaw value = do
236237 handle <- getHandle
237238 address <- getAddress
@@ -259,36 +260,40 @@ send action = do
259260
260261 -- "seq" and "type" must be set for all protocol messages
261262 setField " type" messageType
262- unless (messageType == MessageTypeEvent ) $
263- setField " seq" seqNum
263+ unless (messageType == MessageTypeEvent ) (setField " seq" seqNum)
264264
265265 -- Once all fields are set, fetch the payload for sending
266266 payload <- object <$> gets payload
267267
268268 -- Send payload to client from debug adaptor
269269 writeToHandle address handle payload
270270 resetAdaptorStatePayload
271-
272- sendEvent :: Adaptor app r () -> Adaptor app r ()
271+ ----------------------------------------------------------------------------
272+ -- | Write event to Handle
273+ sendEvent
274+ :: Adaptor app request ()
275+ -> Adaptor app request ()
273276sendEvent action = do
274- () <- action
277+ () <- action
275278 handle <- getHandle
276279 messageType <- gets messageType
277280 address <- getAddress
281+ let errorMsg =
282+ " Use 'send' function when responding to a DAP request, 'sendEvent'\
283+ \ is for responding to events"
278284 case messageType of
279- MessageTypeResponse -> error " use send"
280- MessageTypeRequest -> error " use send"
281- MessageTypeEvent -> do
282- address <- getAddress
285+ MessageTypeResponse ->
286+ sendError (ErrorMessage errorMsg) Nothing
287+ MessageTypeRequest ->
288+ sendError (ErrorMessage errorMsg) Nothing
289+ MessageTypeEvent ->
283290 setField " type" messageType
284291
285292 -- Once all fields are set, fetch the payload for sending
286293 payload <- object <$> gets payload
287294 -- Send payload to client from debug adaptor
288295 writeToHandle address handle payload
289296 resetAdaptorStatePayload
290-
291-
292297----------------------------------------------------------------------------
293298-- | Writes payload to the given 'Handle' using the local connection lock
294299----------------------------------------------------------------------------
@@ -297,15 +302,15 @@ writeToHandle
297302 => SockAddr
298303 -> Handle
299304 -> event
300- -> Adaptor app r ()
305+ -> Adaptor app request ()
301306writeToHandle _ handle evt = do
302307 let msg = encodeBaseProtocolMessage evt
303308 debugMessage (" \n " <> encodePretty evt)
304309 withConnectionLock (BS. hPutStr handle msg)
305310----------------------------------------------------------------------------
306311-- | Resets Adaptor's payload
307312----------------------------------------------------------------------------
308- resetAdaptorStatePayload :: Adaptor app r ()
313+ resetAdaptorStatePayload :: Adaptor app request ()
309314resetAdaptorStatePayload = modify' $ \ s -> s { payload = [] }
310315----------------------------------------------------------------------------
311316sendSuccesfulResponse :: Adaptor app Request () -> Adaptor app Request ()
@@ -319,7 +324,10 @@ sendSuccesfulEmptyResponse :: Adaptor app Request ()
319324sendSuccesfulEmptyResponse = sendSuccesfulResponse (pure () )
320325----------------------------------------------------------------------------
321326-- | Sends successful event
322- sendSuccesfulEvent :: EventType -> Adaptor app r () -> Adaptor app r ()
327+ sendSuccesfulEvent
328+ :: EventType
329+ -> Adaptor app request ()
330+ -> Adaptor app request ()
323331sendSuccesfulEvent event action = do
324332 sendEvent $ do
325333 setEvent event
@@ -333,7 +341,7 @@ sendSuccesfulEvent event action = do
333341sendError
334342 :: ErrorMessage
335343 -> Maybe Message
336- -> Adaptor app r a
344+ -> Adaptor app request a
337345sendError errorMessage maybeMessage = do
338346 throwError (errorMessage, maybeMessage)
339347----------------------------------------------------------------------------
@@ -352,24 +360,24 @@ sendErrorResponse errorMessage maybeMessage = do
352360----------------------------------------------------------------------------
353361setErrorMessage
354362 :: ErrorMessage
355- -> Adaptor app r ()
363+ -> Adaptor app request ()
356364setErrorMessage v = setField " message" v
357365----------------------------------------------------------------------------
358366-- | Sends successful event
359367setSuccess
360368 :: Bool
361- -> Adaptor app r ()
369+ -> Adaptor app request ()
362370setSuccess = setField " success"
363371----------------------------------------------------------------------------
364372setBody
365373 :: ToJSON value
366374 => value
367- -> Adaptor app r ()
375+ -> Adaptor app request ()
368376setBody value = setField " body" value
369377----------------------------------------------------------------------------
370378setType
371379 :: MessageType
372- -> Adaptor app r ()
380+ -> Adaptor app request ()
373381setType messageType = do
374382 modify' $ \ adaptorState ->
375383 adaptorState
@@ -378,14 +386,14 @@ setType messageType = do
378386----------------------------------------------------------------------------
379387setEvent
380388 :: EventType
381- -> Adaptor app r ()
389+ -> Adaptor app request ()
382390setEvent = setField " event"
383391----------------------------------------------------------------------------
384392setField
385393 :: ToJSON value
386394 => Key
387395 -> value
388- -> Adaptor app r ()
396+ -> Adaptor app request ()
389397setField key value = do
390398 currentPayload <- gets payload
391399 modify' $ \ adaptorState ->
@@ -395,7 +403,7 @@ setField key value = do
395403----------------------------------------------------------------------------
396404withConnectionLock
397405 :: IO ()
398- -> Adaptor app r ()
406+ -> Adaptor app request ()
399407withConnectionLock action = do
400408 lock <- asks handleLock
401409 liftIO (withLock lock action)
@@ -418,19 +426,21 @@ getArguments = do
418426 x -> do
419427 logError (BL8. pack (show x))
420428 liftIO $ throwIO (ParseException (show x))
421-
422429----------------------------------------------------------------------------
423430-- | Evaluates Adaptor action by using and updating the state in the MVar
424- runAdaptorWith :: AdaptorLocal app r -> AdaptorState -> String -> Adaptor app r () -> IO ()
425- runAdaptorWith lcl st s (Adaptor action) = do
426- runStateT (runReaderT (runExceptT action) lcl) st
427- return ()
428-
431+ runAdaptorWith
432+ :: AdaptorLocal app request
433+ -> AdaptorState
434+ -> Adaptor app request ()
435+ -> IO ()
436+ runAdaptorWith lcl st (Adaptor action) =
437+ void (runStateT (runReaderT (runExceptT action) lcl) st)
429438----------------------------------------------------------------------------
430439-- | Utility for evaluating a monad transformer stack
431440runAdaptor :: AdaptorLocal app Request -> AdaptorState -> Adaptor app Request () -> IO ()
432441runAdaptor lcl s (Adaptor client) =
433442 runStateT (runReaderT (runExceptT client) lcl) s >>= \ case
434443 (Left (errorMessage, maybeMessage), s') ->
435444 runAdaptor lcl s' (sendErrorResponse errorMessage maybeMessage)
436- (Right () , s') -> pure ()
445+ (Right () , _) -> pure ()
446+ ----------------------------------------------------------------------------
0 commit comments