diff --git a/app/App.hs b/app/App.hs index 8a7967b..4864e3a 100644 --- a/app/App.hs +++ b/app/App.hs @@ -1,16 +1,15 @@ module App (start) where -import Control.Monad.IO.Class +import GHC.Wasm.Prim import Language.Javascript.JSaddle (JSM) import SimpleCounter qualified import Snake qualified -import System.Environment import TodoMVC qualified import TwoZeroFourEight qualified -start :: JSM () -start = - liftIO (getEnv "EXAMPLE") >>= \case +start :: JSString -> JSM () +start e = + case fromJSString e of "simplecounter" -> SimpleCounter.start "snake" -> Snake.start "todomvc" -> TodoMVC.start diff --git a/app/Main.hs b/app/Main.hs index 17dba5f..3f1a49c 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,19 +1,10 @@ -{-# LANGUAGE CPP #-} - -module Main (main) where +module MyMain (main) where import App (start) - -#if wasi_HOST_OS +import GHC.Wasm.Prim import Language.Javascript.JSaddle.Wasm qualified as JSaddle.Wasm -foreign export javascript "hs_start" main :: IO () - -main :: IO () -main = JSaddle.Wasm.run start -#else -import Language.Javascript.JSaddle.Warp qualified as JSaddle.Warp +foreign export javascript "hs_start" main :: JSString -> IO () -main :: IO () -main = JSaddle.Warp.run 8081 start -#endif +main :: JSString -> IO () +main e = JSaddle.Wasm.run $ start e diff --git a/app/Snake.hs b/app/Snake.hs index cf367d7..81bcba9 100644 --- a/app/Snake.hs +++ b/app/Snake.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE CPP #-} {-# LANGUAGE MultiWayIf #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} diff --git a/app/TodoMVC.hs b/app/TodoMVC.hs index ef4a165..99bc835 100644 --- a/app/TodoMVC.hs +++ b/app/TodoMVC.hs @@ -10,7 +10,6 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE ExtendedDefaultRules #-} -{-# LANGUAGE CPP #-} -- from https://github.com/dmjio/miso/blob/master/examples/todo-mvc/Main.hs diff --git a/flake.lock b/flake.lock index f3eff4e..396d611 100644 --- a/flake.lock +++ b/flake.lock @@ -25,11 +25,11 @@ }, "locked": { "host": "gitlab.haskell.org", - "lastModified": 1709548536, - "narHash": "sha256-qXyJ5A3vllB8x6WwpGYaYa9z/y+V/uP9CoW3bsXtZbY=", + "lastModified": 1710140452, + "narHash": "sha256-HxyaSp2E4GENg2wjV6dXQzZRhSSEPL1SYGQZV+VBgKI=", "owner": "ghc", "repo": "ghc-wasm-meta", - "rev": "426e46a5a8ffda63a5dde02e0bf9eb60f9cca089", + "rev": "f600684166791a4b40d56247b780e0aa6f818aef", "type": "gitlab" }, "original": { @@ -41,11 +41,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1709237383, - "narHash": "sha256-cy6ArO4k5qTx+l5o+0mL9f5fa86tYUX3ozE1S+Txlds=", + "lastModified": 1709961763, + "narHash": "sha256-6H95HGJHhEZtyYA3rIQpvamMKAGoa8Yh2rFV29QnuGw=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "1536926ef5621b09bba54035ae2bb6d806d72ac8", + "rev": "3030f185ba6a4bf4f18b87f345f104e6a6961f34", "type": "github" }, "original": { diff --git a/frontend/build.sh b/frontend/build.sh index 88c195f..eec7aa1 100755 --- a/frontend/build.sh +++ b/frontend/build.sh @@ -23,8 +23,9 @@ hs_wasm_path=$(wasm32-wasi-cabal list-bin ghc-wasm-miso-examples) if $dev_mode; then cp "$hs_wasm_path" dist/bin.wasm else - wasm-opt ${1+"$@"} "$hs_wasm_path" -o dist/bin.wasm - wasm-strip --keep-section=name dist/bin.wasm + wizer --allow-wasi --wasm-bulk-memory true --init-func _initialize -o dist/bin.wasm "$hs_wasm_path" + wasm-opt ${1+"$@"} dist/bin.wasm -o dist/bin.wasm + wasm-tools strip -o dist/bin.wasm dist/bin.wasm fi cp ./*.js dist diff --git a/frontend/index.js b/frontend/index.js index 09121e9..aac042e 100644 --- a/frontend/index.js +++ b/frontend/index.js @@ -1,11 +1,8 @@ -import { WASI, OpenFile, File, ConsoleStdout } from "https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.2.19/dist/index.js"; +import { WASI, OpenFile, File, ConsoleStdout } from "https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.2.20/dist/index.js"; import ghc_wasm_jsffi from "./ghc_wasm_jsffi.js"; const args = []; -const env = [ - `EXAMPLE=${globalThis.example}`, - // `GHCRTS=-S`, -]; +const env = []; const fds = [ new OpenFile(new File([])), // stdin ConsoleStdout.lineBuffered((msg) => console.log(`[WASI stdout] ${msg}`)), @@ -22,4 +19,4 @@ const { instance } = await WebAssembly.instantiateStreaming(fetch("bin.wasm"), { Object.assign(instance_exports, instance.exports); wasi.initialize(instance); -await instance.exports.hs_start(); +await instance.exports.hs_start(globalThis.example); diff --git a/ghc-wasm-miso-examples.cabal b/ghc-wasm-miso-examples.cabal index 6835ad8..bffd764 100644 --- a/ghc-wasm-miso-examples.cabal +++ b/ghc-wasm-miso-examples.cabal @@ -8,8 +8,10 @@ executable ghc-wasm-miso-examples , aeson , base , containers + , ghc-experimental , hs2048 , jsaddle + , jsaddle-wasm , miso , mtl , random @@ -22,8 +24,4 @@ executable ghc-wasm-miso-examples SimpleCounter Snake TodoMVC - if os(wasi) - build-depends: jsaddle-wasm - ghc-options: -no-hs-main -optl-mexec-model=reactor "-optl-Wl,--export=hs_start" - else - build-depends: jsaddle-warp + ghc-options: -no-hs-main -optl-mexec-model=reactor "-optl-Wl,--export=hs_start"