From 9ac87f93e98b5732159a0cfa12067dd459e03ace Mon Sep 17 00:00:00 2001 From: Sidharth Kulkarni Date: Sat, 9 May 2026 08:56:00 -0700 Subject: [PATCH] more wasm --- Makefile | 13 +++++++++++++ out/index.html | 12 ++++++++++++ out/index.js | 28 ++++++++++++++++++++++++++++ package.yaml | 8 ++++++++ shell.nix | 11 +++++++++++ 5 files changed, 72 insertions(+) create mode 100644 Makefile create mode 100644 out/index.html create mode 100644 out/index.js diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1dbd4d0 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ + +all: out/numbersquare.wasm out/ghc_wasm_jsffi.js + +out/numbersquare.wasm: app/* src/* numbersquare.cabal + wasm32-wasi-cabal build + wasm32-wasi-cabal install --installdir=out --overwrite-policy=always + +out/ghc_wasm_jsffi.js: out/numbersquare.wasm + $(shell wasm32-wasi-ghc --print-libdir)/post-link.mjs -i out/numbersquare.wasm -o out/ghc_wasm_jsffi.js + +.PHONY: clean +clean: + rm -rf dist-newstyle out/numbersquare.wasm out/ghc_wasm_jsffi.js diff --git a/out/index.html b/out/index.html new file mode 100644 index 0000000..c13272c --- /dev/null +++ b/out/index.html @@ -0,0 +1,12 @@ + + + + WASM test + + + + + + + + diff --git a/out/index.js b/out/index.js new file mode 100644 index 0000000..99b5e82 --- /dev/null +++ b/out/index.js @@ -0,0 +1,28 @@ +import { WASI, OpenFile, File, ConsoleStdout } from "https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/dist/index.js"; +import ghc_wasm_jsffi from "./ghc_wasm_jsffi.js" + +const args = []; +const env = ["GHCRTS=-H64m"]; +const fds = [ + new OpenFile(new File([])), // stdin + ConsoleStdout.lineBuffered((msg) => console.log(`[WASI stdout] ''${msg}`)), + ConsoleStdout.lineBuffered((msg) => console.warn(`[WASI stderr] ''${msg}`)), +]; +const options = { debug: false }; +const wasi = new WASI(args, env, fds, options); + +let __exports = {}; + +const { instance } = await WebAssembly.instantiateStreaming( + fetch("numbersquare.wasm"), + { + ghc_wasm_jsffi: ghc_wasm_jsffi(__exports), + wasi_snapshot_preview1: wasi.wasiImport, + } +); + +Object.assign(__exports, instance.exports); + +wasi.initialize(instance); +console.log(instance) +await instance.exports.hs_start(globalThis.example); diff --git a/package.yaml b/package.yaml index 4dbaee7..70af312 100644 --- a/package.yaml +++ b/package.yaml @@ -41,6 +41,14 @@ executable: source-dirs: - app main: Main.hs + when: + - condition: arch(wasm32) + ghc-options: + -no-hs-main + -optl-mexec-model=reactor + "-optl-Wl,--export=hs_start" + cpp-options: + -DWASM tests: numberspec-test: diff --git a/shell.nix b/shell.nix index 7f913a0..88b56b3 100644 --- a/shell.nix +++ b/shell.nix @@ -5,6 +5,9 @@ watchexec, wasm32-wasi-cabal, wasm32-wasi-ghc, + writeShellApplication, + nodejs, + python3, }: haskellPackages.developPackage { name = "numbersquare"; @@ -18,6 +21,14 @@ haskellPackages.developPackage { watchexec wasm32-wasi-cabal wasm32-wasi-ghc + nodejs + (writeShellApplication { + name = "dev-server"; + text = '' + python -m http.server -d ./out + ''; + runtimeInputs = [ python3 ]; + }) ] ++ (with haskellPackages; [ fourmolu