miso icon indicating copy to clipboard operation
miso copied to clipboard

GHC WebAssembly/JS backend support

Open mikelpr opened this issue 2 years ago • 9 comments

Is your feature request related to a problem? Please describe. ghcjs is a bit outdated and stuck at ghc 8.*

Describe the solution you'd like both tweag's WebAssembly backend and a new JS backend were recently merged into GHC 9 and will probably get the most support going forward wrt haskell on browsers

Additional context the new JS backend talks about not being complete enough yet (missing Template Haskell) and the WebAssembly backend I don't know if has access to the DOM

anyways, would be exciting!

mikelpr avatar Jun 01 '23 03:06 mikelpr

According to the IOG blog ghcjs-base and friends have been updated to support the ghc js backend, so I think that means this issue has finally become actionable :).

noinia avatar Apr 11 '24 20:04 noinia

Everything seems to compile on ghc 9.10-alpha3 from ghcup and head.hackage with this patch

diff --git a/miso.cabal b/miso.cabal
index dd27739..bf2a39a 100644
--- a/miso.cabal
+++ b/miso.cabal
@@ -137,6 +137,7 @@ library
     hs-source-dirs:
       text-src
   build-depends:
+    ghcjs-base,
     aeson,
     base < 5,
     bytestring,
diff --git a/src/Miso/Html/Types.hs b/src/Miso/Html/Types.hs
index 3310fda..a2b4dfb 100644
--- a/src/Miso/Html/Types.hs
+++ b/src/Miso/Html/Types.hs
@@ -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 (elem, reverse)
 
 -- | Core type for constructing a `VTree`, use this instead of `VTree` directly.
 data View action

I use the command

cabal build --with-ghc=javascript-unknown-ghcjs-ghc --with-ghc-pkg=javascript-unknown-ghcjs-ghc-pkg --allow-newer

But I run out of memory during linking of the first demo application in the README. Only have 8G of RAM on this laptop, will try another machine some other day if nobody else in this thread has time...

ysangkok avatar Apr 17 '24 04:04 ysangkok

I ran the test suite (needed a change for aeson-2) and also had to pass --with-hsc2hs=javascript-unknown-ghcjs-hsc2hs to cabal. It succeeded.


edit: apparently have to run the test through the browsers. There I get:

uncaught exception in Haskell main thread: ReferenceError: h$splitmix_init is not defined

hasufell avatar Apr 17 '24 10:04 hasufell

https://github.com/haskellari/splitmix/pull/75

hasufell avatar Apr 17 '24 11:04 hasufell

As another resource: We have some examples of how to use Miso with the WASM backend here: https://github.com/tweag/ghc-wasm-miso-examples

amesgen avatar Apr 17 '24 17:04 amesgen

uncaught exception in Haskell main thread: ReferenceError: h$splitmix_init is not defined

Fixing that with a source-repository-package stanza in cabal.project now leads to:

uncaught exception in Haskell main thread: ReferenceError: $1 is not defined

The offending generated code is:

function h$$72640042() {var a=h$stack[(h$sp-1)];
h$sp-=2;
window.global_test_results = $1;(a);
return h$rs();
};

hasufell avatar Apr 18 '24 06:04 hasufell

As another resource: We have some examples of how to use Miso with the WASM backend here: https://github.com/tweag/ghc-wasm-miso-examples

Doesn't work here: https://github.com/tweag/ghc-wasm-miso-examples/issues/10

hasufell avatar Apr 18 '24 06:04 hasufell

Seems like the JavaScript part of this issue is addressed in:

  • #738

ysangkok avatar Apr 18 '24 14:04 ysangkok

has anyone done a size comparison between WASM and (the new) GHC JS backends? considering @dmjio worries about size on #738

mikelpr avatar Apr 30 '24 22:04 mikelpr