Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions app/App.hs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 5 additions & 14 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion app/Snake.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
Expand Down
1 change: 0 additions & 1 deletion app/TodoMVC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frontend/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions frontend/index.js
Original file line number Diff line number Diff line change
@@ -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}`)),
Expand All @@ -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);
8 changes: 3 additions & 5 deletions ghc-wasm-miso-examples.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ executable ghc-wasm-miso-examples
, aeson
, base
, containers
, ghc-experimental
, hs2048
, jsaddle
, jsaddle-wasm
, miso
, mtl
, random
Expand All @@ -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"