more wasm

This commit is contained in:
Sidharth Kulkarni 2026-05-09 08:56:00 -07:00
parent 9d67af9901
commit 9ac87f93e9
Signed by: skulk
SSH key fingerprint: SHA256:Jby+S9d1WmwqnXIrngHgccYNHz+cYquxN1zm3ym3Kbg
5 changed files with 72 additions and 0 deletions

13
Makefile Normal file
View file

@ -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

12
out/index.html Normal file
View file

@ -0,0 +1,12 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>WASM test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script src="./index.js" type="module"></script>
</body>
</html>

28
out/index.js Normal file
View file

@ -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);

View file

@ -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:

View file

@ -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