diff --git a/examples/hello/Shakefile.hs b/examples/hello/Shakefile.hs new file mode 100644 index 0000000..b21d88f --- /dev/null +++ b/examples/hello/Shakefile.hs @@ -0,0 +1,37 @@ +import Development.Shake +import Development.Shake.Command +import Development.Shake.FilePath +import Development.Shake.Util + +main = shakeArgs shakeOptions $ do + -- action $ do + -- sourceFiles <- getDirectoryFiles "" ["//*.luasm"] + -- need $ (-<.> "luac") <$> sourceFiles + want ["complete.component.wasm"] + alternatives $ do + "complete.component.wasm" %> \out -> do + need ["provider.component.wasm","importing.component.wasm"] + cmd_ + "wac plug --plug" + "provider.component.wasm" "importing.component.wasm" + "-o" "complete.component.wasm" + "*.component.wasm" %> \out -> do + let wasm = out `replaceExtensions` "wasm" + need [wasm] + cmd_ + "wasm-tools component new" + [wasm] + "-o" [out] + "*.wasm" %> \out -> do + let wit = out -<.> "wit" + let wat = out -<.> "wat" + need [wit,wat] + cmd_ + "wasm-tools component embed" + [wit] [wat] "-o" [out] + phony "run" $ do + need ["complete.component.wasm"] + cmd_ "wasmtime run --invoke add(1) complete.component.wasm" + phony "clean" $ do + removeFilesAfter ".shake" ["//*"] + liftIO $ removeFiles "." ["*.wasm"] diff --git a/examples/hello/hello.wat b/examples/hello/hello.wat new file mode 100644 index 0000000..1af4d0d --- /dev/null +++ b/examples/hello/hello.wat @@ -0,0 +1,27 @@ +(module + ;; Correct module name: stdio, not stdout + (import "wasi:cli/stdio@0.2.6" "get-stdout" + (func $get_stdout (result i32))) + + ;; Write and flush a buffer. + (import "wasi:io/streams@0.2.0" "blocking-write-and-flush" + (func $bwrite (param i32 i32 i32) (result i32))) + + (memory (export "memory") 1) + + (data (i32.const 0) "Hello, World!\n") + + (func (export "main") + (local $stdout i32) + ;; Get the stdout stream handle + call $get_stdout + local.set $stdout + + ;; Write "Hello, World!\n" (14 bytes) to stdout + local.get $stdout + i32.const 0 + i32.const 14 + call $bwrite + drop ;; ignore the return value (0 = success) + ) +) diff --git a/examples/hello/hello.wit b/examples/hello/hello.wit new file mode 100644 index 0000000..4c52be0 --- /dev/null +++ b/examples/hello/hello.wit @@ -0,0 +1,22 @@ +package root:component; + +world root { + import wasi:cli/stdin@0.2.6; + import wasi:cli/stdout@0.2.6; + + export wasi:cli/run@0.2.0; +} + +package wasi:cli@0.2.6 { + interface stdout { + use wasi:io/streams@0.2.6.{output-stream}; + + get-stdout: func() -> output-stream; + } +} + +package wasi:cli@0.2.0 { + interface run { + run: func() -> result; + } +} diff --git a/flake.nix b/flake.nix index 11c5fac..ea63194 100644 --- a/flake.nix +++ b/flake.nix @@ -10,6 +10,7 @@ inherit system; }; shake = pkgs.callPackage ./shake-wrapper.nix {}; + rustslop = pkgs.callPackage ./rustslop {}; in { devShells.${system}.default = pkgs.mkShell { packages = [ @@ -20,6 +21,10 @@ pkgs.wasm-tools pkgs.wac-cli ]; + inputsFrom = [ + rustslop + ]; + inherit (rustslop) CARGO_BUILD_TARGET; }; }; } diff --git a/rustslop/.gitignore b/rustslop/.gitignore new file mode 100644 index 0000000..1de5659 --- /dev/null +++ b/rustslop/.gitignore @@ -0,0 +1 @@ +target \ No newline at end of file diff --git a/rustslop/Cargo.lock b/rustslop/Cargo.lock new file mode 100644 index 0000000..b9d42e1 --- /dev/null +++ b/rustslop/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/rustslop/Cargo.toml b/rustslop/Cargo.toml new file mode 100644 index 0000000..1c61e9e --- /dev/null +++ b/rustslop/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/rustslop/default.nix b/rustslop/default.nix new file mode 100644 index 0000000..d9d6e4f --- /dev/null +++ b/rustslop/default.nix @@ -0,0 +1,13 @@ +{ lib +, rustPlatform +}: + +rustPlatform.buildRustPackage (finalAttrs: { + pname = "rustslop"; + version = "0.0.1"; + src = ./.; + cargoLock = { + lockFile = ./Cargo.lock; + }; + CARGO_BUILD_TARGET = "wasm32-wasip2"; +}) diff --git a/rustslop/src/main.rs b/rustslop/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/rustslop/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}