diff --git a/examples/hs/plain-ghc/Hello.hi b/examples/hs/plain-ghc/Hello.hi new file mode 100644 index 0000000..01cd7ce Binary files /dev/null and b/examples/hs/plain-ghc/Hello.hi differ diff --git a/examples/hs/plain-ghc/Hello.hs b/examples/hs/plain-ghc/Hello.hs new file mode 100644 index 0000000..0bffcf2 --- /dev/null +++ b/examples/hs/plain-ghc/Hello.hs @@ -0,0 +1,15 @@ +module Main where + +import Foreign (Int32) + + +foreign export ccall "hello" hello :: IO Int32 + +hello :: IO Int32 +hello = do + putStrLn "hello from Haskell!!!" + pure 0 + +main :: IO () +main = error "defining main shouldn't be necessary since we set -no-hs-main, \ + \but HLS is yelling at me and i just want to calm her down." diff --git a/examples/hs/plain-ghc/Hello.o b/examples/hs/plain-ghc/Hello.o new file mode 100755 index 0000000..355c3a6 Binary files /dev/null and b/examples/hs/plain-ghc/Hello.o differ diff --git a/examples/hs/plain-ghc/Hello_stub.h b/examples/hs/plain-ghc/Hello_stub.h new file mode 100644 index 0000000..700966c --- /dev/null +++ b/examples/hs/plain-ghc/Hello_stub.h @@ -0,0 +1,9 @@ +#include +#if defined(__cplusplus) +extern "C" { +#endif +extern HsInt32 hello(void); +#if defined(__cplusplus) +} +#endif + diff --git a/examples/hs/plain-ghc/Shakefile.hs b/examples/hs/plain-ghc/Shakefile.hs new file mode 100644 index 0000000..28ac784 --- /dev/null +++ b/examples/hs/plain-ghc/Shakefile.hs @@ -0,0 +1,39 @@ +import Development.Shake +import Development.Shake.Command +import Development.Shake.FilePath +import Development.Shake.Util +import Data.Char (toUpper) + +capitalise (x:xs) = toUpper x : xs + +ghcflags = + [ "-no-hs-main" + , "-optl-mexec-model=reactor" + , "-optl-Wl,--export=hello,--export=hs_init" + ] + +main = shakeArgs shakeOptions $ do + want ["call-hello.wasm","hello.wasm"] + "call-hello.wasm" %> \out -> do + need ["call-hello.wat"] + cmd_ "wasm-tools parse call-hello.wat" "-o" [out] + "hello.wasm" %> \out -> do + need ["Hello.hs"] + cmd_ + "wasm32-wasi-ghc" + "Hello.hs" ghcflags + "-o" [out] + phony "run" $ do + need ["call-hello.wasm","hello.wasm"] + cmd_ "wasmtime run" + "--preload" "env=hello.wasm" + "--invoke" "call-hello" + "call-hello.wasm" + phony "clean" $ do + removeFilesAfter ".shake" ["//*"] + liftIO $ removeFiles "." + [ "*.wasm" + , "*.o" + , "*_stub.h" + , "*.hi" + ] diff --git a/examples/hs/plain-ghc/call-hello.wat b/examples/hs/plain-ghc/call-hello.wat new file mode 100644 index 0000000..7b92011 --- /dev/null +++ b/examples/hs/plain-ghc/call-hello.wat @@ -0,0 +1,6 @@ +(module + (import "env" "hello" (func $hello (result i32))) + (import "env" "hs_init" (func $hs-init (param i32 i32))) + (func (export "call-hello") (result i32) + (call $hs-init (i32.const 0) (i32.const 0)) + (call $hello))) diff --git a/examples/addcomponent/adder.wat b/examples/wat/addcomponent/adder.wat similarity index 100% rename from examples/addcomponent/adder.wat rename to examples/wat/addcomponent/adder.wat diff --git a/examples/addcomponent/adder.wit b/examples/wat/addcomponent/adder.wit similarity index 100% rename from examples/addcomponent/adder.wit rename to examples/wat/addcomponent/adder.wit diff --git a/examples/cont-stack/Shakefile.hs b/examples/wat/cont-stack/Shakefile.hs similarity index 100% rename from examples/cont-stack/Shakefile.hs rename to examples/wat/cont-stack/Shakefile.hs diff --git a/examples/cont-stack/cont-stack.wat b/examples/wat/cont-stack/cont-stack.wat similarity index 100% rename from examples/cont-stack/cont-stack.wat rename to examples/wat/cont-stack/cont-stack.wat diff --git a/examples/cont-stack/run.js b/examples/wat/cont-stack/run.js similarity index 100% rename from examples/cont-stack/run.js rename to examples/wat/cont-stack/run.js diff --git a/examples/hello/Shakefile.hs b/examples/wat/hello/Shakefile.hs similarity index 100% rename from examples/hello/Shakefile.hs rename to examples/wat/hello/Shakefile.hs diff --git a/examples/hello/hello.wat b/examples/wat/hello/hello.wat similarity index 100% rename from examples/hello/hello.wat rename to examples/wat/hello/hello.wat diff --git a/examples/hello/hello.wit b/examples/wat/hello/hello.wit similarity index 100% rename from examples/hello/hello.wit rename to examples/wat/hello/hello.wit diff --git a/examples/providecomponent/Shakefile.hs b/examples/wat/providecomponent/Shakefile.hs similarity index 100% rename from examples/providecomponent/Shakefile.hs rename to examples/wat/providecomponent/Shakefile.hs diff --git a/examples/providecomponent/importing.wat b/examples/wat/providecomponent/importing.wat similarity index 100% rename from examples/providecomponent/importing.wat rename to examples/wat/providecomponent/importing.wat diff --git a/examples/providecomponent/importing.wit b/examples/wat/providecomponent/importing.wit similarity index 100% rename from examples/providecomponent/importing.wit rename to examples/wat/providecomponent/importing.wit diff --git a/examples/providecomponent/provider.wat b/examples/wat/providecomponent/provider.wat similarity index 100% rename from examples/providecomponent/provider.wat rename to examples/wat/providecomponent/provider.wat diff --git a/examples/providecomponent/provider.wit b/examples/wat/providecomponent/provider.wit similarity index 100% rename from examples/providecomponent/provider.wit rename to examples/wat/providecomponent/provider.wit diff --git a/examples/silly/Shakefile.hs b/examples/wat/silly/Shakefile.hs similarity index 100% rename from examples/silly/Shakefile.hs rename to examples/wat/silly/Shakefile.hs diff --git a/examples/silly/silly.wat b/examples/wat/silly/silly.wat similarity index 100% rename from examples/silly/silly.wat rename to examples/wat/silly/silly.wat diff --git a/examples/silly/silly.wit b/examples/wat/silly/silly.wit similarity index 100% rename from examples/silly/silly.wit rename to examples/wat/silly/silly.wit diff --git a/flake.lock b/flake.lock index d212ee9..53e7806 100644 --- a/flake.lock +++ b/flake.lock @@ -1,6 +1,61 @@ { "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "ghc-wasm": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + }, + "locked": { + "host": "gitlab.haskell.org", + "lastModified": 1784272731, + "narHash": "sha256-VcoIdIlUXz7fxu7gXbwih9Wy0m+i2wq6yYsSiFMuDs0=", + "owner": "haskell-wasm", + "repo": "ghc-wasm-meta", + "rev": "ec050b490a8499a1d1d1a3c093b8e760a20d4ebf", + "type": "gitlab" + }, + "original": { + "host": "gitlab.haskell.org", + "owner": "haskell-wasm", + "repo": "ghc-wasm-meta", + "type": "gitlab" + } + }, "nixpkgs": { + "locked": { + "lastModified": 1784182977, + "narHash": "sha256-Hy8zg2HEcsk4WqowQK3UKRXz1fdQM7Z2un3nSyLbJaQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "29c22b7761070f37c4c2b0dd6a8777f0bd43e268", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-26.05-darwin", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { "locked": { "lastModified": 1781607440, "narHash": "sha256-rxO+uc/KFbSJp+pgyXRuAX6QlG9hJdnt0BXpEQRXY+U=", @@ -18,7 +73,23 @@ }, "root": { "inputs": { - "nixpkgs": "nixpkgs" + "ghc-wasm": "ghc-wasm", + "nixpkgs": "nixpkgs_2" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index 17adf31..81e85af 100644 --- a/flake.nix +++ b/flake.nix @@ -1,6 +1,7 @@ { inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + ghc-wasm.url = "gitlab:haskell-wasm/ghc-wasm-meta?host=gitlab.haskell.org"; }; outputs = inputs: @@ -19,7 +20,7 @@ pkgs.wasmtime pkgs.wasm-tools pkgs.wac-cli - pkgs.guile + inputs.ghc-wasm.packages.${system}.all_9_14 ]; }; }; diff --git a/guile/cps/.gitignore b/guile/cps/.gitignore deleted file mode 100644 index 395c5c2..0000000 --- a/guile/cps/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*.cps -*.go \ No newline at end of file diff --git a/guile/cps/Shakefile.hs b/guile/cps/Shakefile.hs deleted file mode 100644 index 23849e6..0000000 --- a/guile/cps/Shakefile.hs +++ /dev/null @@ -1,21 +0,0 @@ -import Development.Shake -import Development.Shake.Command -import Development.Shake.FilePath -import Development.Shake.Util - -main = shakeArgs shakeOptions $ do - action $ do - sourceFiles <- getDirectoryFiles "" ["//*.scm"] - need $ (-<.> "cps") <$> sourceFiles - "*.cps" %> \out -> do - withTempFile $ \temp_cps -> do - let scm = out -<.> "scm" - need [scm] - cmd_ - "guild compile -O0 -Ocps -t cps -o" [temp_cps] - [scm] - cmd_ (FileStdout out) - (Stdin ";; -*- mode:scheme -*-\n") "cat -" [temp_cps] - phony "clean" $ do - removeFilesAfter ".shake" ["//*"] - liftIO $ removeFiles "." ["*.cps","*.go"] diff --git a/guile/cps/t.scm b/guile/cps/t.scm deleted file mode 100644 index 4c83eb9..0000000 --- a/guile/cps/t.scm +++ /dev/null @@ -1,3 +0,0 @@ -(display "the numberr is ... ") -(display (+ 5 (* 3 4))) -(newline)