Skip to content

Commit

Permalink
Update Foreign.JavaScript.Types to GHC 9.12
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichApfelmus committed Dec 25, 2024
1 parent 37972f0 commit b80cd80
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
11 changes: 11 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ flags: +buildExamples

package snap-server
flags: +openssl

if (impl(ghc == 9.12.1))
constraints:
, hashable >= 1.5
, template-haskell >= 2.23

allow-newer:
, *:time
, *:ghc-prim
, *:template-haskell
, *:base
82 changes: 59 additions & 23 deletions src/Foreign/JavaScript/Types.hs
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-}
module Foreign.JavaScript.Types where

import Control.Concurrent.STM
( STM
, TMVar
, TQueue
, TVar
)
import Control.DeepSeq
( NFData (..)
, force
)
import Data.Aeson
( toJSON
, (.=)
, (.:)
)
import Data.ByteString.Char8
( ByteString
)
import Data.Map
( Map
)
import Data.String
( fromString
)
import Data.Text
( Text
)
import Data.Typeable
( Typeable
)
import Snap.Core
( Cookie(..)
)
import System.IO
( stderr
)

import qualified Control.Concurrent.STM as STM
import qualified Control.Exception as E
import Control.Concurrent.STM as STM
import Control.Concurrent.MVar
import Control.DeepSeq
import Data.Aeson as JSON
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS (hPutStrLn)
import Data.Map as Map
import Data.String
import Data.Text
import Data.Typeable
import Snap.Core (Cookie(..))
import System.IO (stderr)
import qualified Data.Aeson as JSON
import qualified Data.ByteString.Char8 as BS (hPutStrLn)
import qualified Data.Map as Map

import Control.Concurrent.MVar
import Foreign.RemotePtr

{-----------------------------------------------------------------------------
Expand Down Expand Up @@ -196,8 +226,8 @@ data ClientMsg
| Quit
deriving (Eq, Show)

instance FromJSON ClientMsg where
parseJSON (Object msg) = do
instance JSON.FromJSON ClientMsg where
parseJSON (JSON.Object msg) = do
tag <- msg .: "tag"
case (tag :: Text) of
"Event" -> Event <$> (msg .: "name") <*> (msg .: "arguments")
Expand All @@ -209,8 +239,10 @@ readClient :: Comm -> STM ClientMsg
readClient c = do
msg <- readComm c
case JSON.fromJSON msg of
Error s -> error $ "Foreign.JavaScript: Error parsing client message " ++ show s
Success x -> return x
JSON.Error s ->
error $ "Foreign.JavaScript: Error parsing client message " ++ show s
JSON.Success x
-> pure x

-- | Messages sent by the Haskell server.
data ServerMsg
Expand All @@ -226,11 +258,15 @@ instance NFData ServerMsg where
rnf (Debug x) = rnf x
rnf (Timestamp ) = ()

instance ToJSON ServerMsg where
toJSON (Debug x) = object [ "tag" .= t "Debug" , "contents" .= toJSON x]
toJSON (Timestamp ) = object [ "tag" .= t "Timestamp" ]
toJSON (RunEval x) = object [ "tag" .= t "RunEval" , "contents" .= toJSON x]
toJSON (CallEval x) = object [ "tag" .= t "CallEval", "contents" .= toJSON x]
instance JSON.ToJSON ServerMsg where
toJSON (Debug x) =
JSON.object [ "tag" .= t "Debug", "contents" .= toJSON x]
toJSON Timestamp =
JSON.object [ "tag" .= t "Timestamp" ]
toJSON (RunEval x) =
JSON.object [ "tag" .= t "RunEval", "contents" .= toJSON x]
toJSON (CallEval x) =
JSON.object [ "tag" .= t "CallEval", "contents" .= toJSON x]

t :: String -> Text
t s = fromString s
Expand Down Expand Up @@ -335,8 +371,8 @@ data Window = Window
newPartialWindow :: IO Window
newPartialWindow = do
ptr <- newRemotePtr "" () =<< newVendor
b1 <- newTMVarIO id
b2 <- newTVarIO NoBuffering
b1 <- STM.newTMVarIO id
b2 <- STM.newTVarIO NoBuffering
let nop = const $ return ()
Window undefined [] nop undefined b1 b2 (return ()) nop nop ptr <$> newVendor <*> newVendor

Expand Down
1 change: 1 addition & 0 deletions threepenny-gui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tested-with:
, GHC == 9.6.6
, GHC == 9.8.4
, GHC == 9.10.1
, GHC == 9.12.1

extra-source-Files:
CHANGELOG.md
Expand Down

0 comments on commit b80cd80

Please sign in to comment.