Skip to content

Commit

Permalink
Support GHC JavaScript backend (#738)
Browse files Browse the repository at this point in the history
* Support GHC JavaScript backend

js-sources from the `miso.cabal` file are not linked into all.js.

One work around for this is to `cp jsbits/*` to the `.jsexe` directory and edit the `index.html` file and add:

```
  <script language="javascript" src="delegate.js" defer></script>
  <script language="javascript" src="diff.js" defer></script>
  <script language="javascript" src="isomorphic.js" defer></script>
  <script language="javascript" src="util.js" defer></script>
```

To build one of the examples with the included haskell.nix flake:

```
nix develop .#
javascript-unknown-ghcjs-cabal build todo-mvc
cp jsbits/* dist-newstyle/build/javascript-ghcjs/ghc-9.8.2/miso-examples-1.8.3.0/x/todo-mvc/build/todo-mvc/todo-mvc.jsexe/
vim dist-newstyle/build/javascript-ghcjs/ghc-9.8.2/miso-examples-1.8.3.0/x/todo-mvc/build/todo-mvc/todo-mvc.jsexe/index.html
```

* Include cabal in nix develop shell

* Remove cabal.project and nix files
  • Loading branch information
hamishmack authored Apr 29, 2024
1 parent e0df31a commit 2797f75
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions examples/haskell-miso.org/haskell-miso.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cabal-version: >=1.10
executable server
main-is:
Main.hs
if impl(ghcjs)
if impl(ghcjs) || arch(javascript)
buildable: False
else
ghc-options:
Expand Down Expand Up @@ -46,7 +46,7 @@ executable server
executable client
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
ghcjs-options:
Expand Down
26 changes: 13 additions & 13 deletions examples/miso-examples.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ executable simple
executable todo-mvc
main-is:
Main.hs
if !impl(ghcjs) && !flag(jsaddle)
if !impl(ghcjs) && !arch(javascript) && !flag(jsaddle)
buildable: False
else
if flag(jsaddle)
Expand Down Expand Up @@ -98,7 +98,7 @@ executable todo-mvc
executable threejs
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
ghcjs-options:
Expand All @@ -119,7 +119,7 @@ executable threejs
executable file-reader
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
ghcjs-options:
Expand All @@ -140,7 +140,7 @@ executable file-reader
executable xhr
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
ghcjs-options:
Expand All @@ -161,7 +161,7 @@ executable xhr
executable canvas2d
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
ghcjs-options:
Expand All @@ -181,7 +181,7 @@ executable canvas2d
executable router
main-is:
Main.hs
if !impl(ghcjs) && !flag(jsaddle)
if !impl(ghcjs) && !arch(javascript) && !flag(jsaddle)
buildable: False
else
if flag(jsaddle)
Expand Down Expand Up @@ -216,7 +216,7 @@ executable router
executable websocket
main-is:
Main.hs
if !impl(ghcjs) && !flag(jsaddle)
if !impl(ghcjs) && !arch(javascript) && !flag(jsaddle)
buildable: False
else
if flag(jsaddle)
Expand Down Expand Up @@ -250,7 +250,7 @@ executable websocket
executable mario
main-is:
Main.hs
if !impl(ghcjs) && !flag(jsaddle)
if !impl(ghcjs) && !arch(javascript) && !flag(jsaddle)
buildable: False
else
if flag(jsaddle)
Expand All @@ -266,13 +266,13 @@ executable mario
base < 5,
containers,
miso
if flag(jsaddle) && !impl(ghcjs) && !flag(ios)
if flag(jsaddle) && !impl(ghcjs) && !arch(javascript) && !flag(ios)
build-depends:
wai,
wai-app-static,
warp,
websockets
if flag(ios) && !impl(ghcjs)
if flag(ios) && !impl(ghcjs) && !arch(javascript)
cpp-options:
-DIOS
ghc-options:
Expand All @@ -288,7 +288,7 @@ executable mario
executable svg
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
ghcjs-options:
Expand All @@ -310,7 +310,7 @@ executable svg
executable compose-update
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
ghcjs-options:
Expand All @@ -328,7 +328,7 @@ executable compose-update
executable mathml
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
ghcjs-options:
Expand Down
4 changes: 2 additions & 2 deletions examples/sse/sse.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cabal-version: >=1.10
executable server
main-is:
Main.hs
if impl(ghcjs)
if impl(ghcjs) || arch(javascript)
buildable: False
else
ghc-options:
Expand Down Expand Up @@ -44,7 +44,7 @@ executable server
executable client
main-is:
Main.hs
if !impl(ghcjs)
if !impl(ghcjs) && !arch(javascript)
buildable: False
else
hs-source-dirs:
Expand Down
2 changes: 1 addition & 1 deletion jsstring-src/Miso/String.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import Text.StringLike (StringLike(..))
-- | String type swappable based on compiler
type MisoString = JS.JSString

#ifdef __GHCJS__
#ifdef ghcjs_HOST_OS
-- | `ToJSON` for `MisoString`
instance ToJSON MisoString where
toJSON = String . textFromJSString
Expand Down
12 changes: 6 additions & 6 deletions miso.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ flag jsstring-only
executable tests
main-is:
Main.hs
if !impl(ghcjs) || !flag(tests)
if !(impl(ghcjs) || arch(javascript)) || !flag(tests)
buildable: False
else
ghcjs-options:
Expand Down Expand Up @@ -121,7 +121,7 @@ library
Miso.Mathml.Element
Miso.String
Miso.WebSocket
if !impl(ghcjs)
if !(impl(ghcjs) || arch(javascript))
exposed-modules:
Miso.TypeLevel
other-modules:
Expand All @@ -130,7 +130,7 @@ library
-Wall
hs-source-dirs:
src
if impl(ghcjs) || flag (jsstring-only)
if impl(ghcjs) || arch(javascript) || flag (jsstring-only)
hs-source-dirs:
jsstring-src
else
Expand All @@ -151,17 +151,17 @@ library
tagsoup,
text,
transformers
if impl(ghcjs)
if impl(ghcjs) || arch(javascript)
build-depends:
ghcjs-base
else
build-depends:
servant-lucid
if impl(ghcjs) || flag (jsaddle)
if impl(ghcjs) || arch(javascript) || flag (jsaddle)
if flag (ios)
cpp-options:
-DIOS
if impl(ghcjs)
if impl(ghcjs) || arch(javascript)
js-sources:
jsbits/diff.js
jsbits/delegate.js
Expand Down
4 changes: 2 additions & 2 deletions sample-app-jsaddle/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Miso
import Miso.String

-- | JSAddle import
#ifndef __GHCJS__
#ifndef ghcjs_HOST_OS
import Language.Javascript.JSaddle.Warp as JSaddle
import qualified Network.Wai.Handler.Warp as Warp
import Network.WebSockets
Expand All @@ -29,7 +29,7 @@ data Action
| SayHelloWorld
deriving (Show, Eq)

#ifndef __GHCJS__
#ifndef ghcjs_HOST_OS
runApp :: JSM () -> IO ()
runApp f = JSaddle.debugOr 8080 (f >> syncPoint) JSaddle.jsaddleApp
#else
Expand Down
8 changes: 4 additions & 4 deletions src/Miso.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Miso
, module Miso.Event
, module Miso.Html
, module Miso.Subscription
#ifndef __GHCJS__
#ifndef ghcjs_HOST_OS
, module Miso.TypeLevel
#endif
, module Miso.Types
Expand All @@ -46,7 +46,7 @@ import System.Mem.StableName
import qualified Data.Sequence as S
import qualified JavaScript.Object.Internal as OI

#ifndef __GHCJS__
#ifndef ghcjs_HOST_OS
import Language.Javascript.JSaddle (eval, waitForAnimationFrame)
#ifdef IOS
import Miso.JSBits
Expand All @@ -67,7 +67,7 @@ import Miso.FFI
import Miso.Html
import Miso.Router
import Miso.Subscription
#ifndef __GHCJS__
#ifndef ghcjs_HOST_OS
import Miso.TypeLevel
#endif
import Miso.Types
Expand All @@ -82,7 +82,7 @@ common
-> (Sink action -> JSM (IORef VTree))
-> JSM ()
common App {..} m getView = do
#ifndef __GHCJS__
#ifndef ghcjs_HOST_OS
#ifdef IOS
mapM_ eval [delegateJs,diffJs,isomorphicJs,utilJs]
#else
Expand Down
2 changes: 1 addition & 1 deletion src/Miso/FFI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import qualified Data.JSString as JSS
import GHCJS.Marshal
import GHCJS.Types
import qualified JavaScript.Object.Internal as OI
#ifdef __GHCJS__
#ifdef ghcjs_HOST_OS
import Language.Javascript.JSaddle hiding (obj, val)
#else
import Language.Javascript.JSaddle hiding (Success, obj, val)
Expand Down
2 changes: 1 addition & 1 deletion src/Miso/Html/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import Text.HTML.TagSoup (Tag(..))
import Miso.Effect
import Miso.Event
import Miso.FFI
import Miso.String hiding (reverse)
import Miso.String hiding (reverse, elem)

-- | Core type for constructing a `VTree`, use this instead of `VTree` directly.
data View action
Expand Down
2 changes: 1 addition & 1 deletion src/Miso/Subscription/WebSocket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ websocketSub (URL u) (Protocols ps) f sink = do
WS.addEventListener socket "error" $ \v -> do
liftIO (writeIORef closedCode Nothing)
d' <- WS.data' v
#ifndef __GHCJS__
#ifndef ghcjs_HOST_OS
undef <- ghcjsPure (isUndefined d')
#else
let undef = isUndefined d'
Expand Down
4 changes: 2 additions & 2 deletions src/Miso/WebSocket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Miso.WebSocket

import GHC.Generics
import Prelude hiding (map)
#ifdef __GHCJS__
#ifdef ghcjs_HOST_OS
import GHCJS.Marshal
#endif

Expand Down Expand Up @@ -98,7 +98,7 @@ data CloseCode
-- ^ OtherCode that is reserved and not in the range 0999
deriving (Show, Eq, Generic)

#ifdef __GHCJS__
#ifdef ghcjs_HOST_OS
-- Defined here to avoid an orphan instance
instance ToJSVal CloseCode
instance FromJSVal CloseCode
Expand Down

0 comments on commit 2797f75

Please sign in to comment.