diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-06-15 15:35:11 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-06-15 15:35:11 -0400 |
| commit | c1379a4c89ee5b09559aab2b82eb4bd233cd0484 (patch) | |
| tree | 64d998ee110d0c11def9d6b97d910c36bb7da599 | |
| parent | 5781e2c5ad539c9d488e7b9a3f28f555fc2b918e (diff) | |
| download | x-c1379a4c89ee5b09559aab2b82eb4bd233cd0484.tar.xz x-c1379a4c89ee5b09559aab2b82eb4bd233cd0484.zip | |
wasm: fix up example code
Signed-off-by: Xe Iaso <me@xeiaso.net>
| -rw-r--r-- | wasm/cmd/aiyou/main.go | 17 | ||||
| -rw-r--r-- | wasm/wasip1/Makefile | 3 | ||||
| -rw-r--r-- | wasm/wasip1/echoclient.rs | 6 |
3 files changed, 18 insertions, 8 deletions
diff --git a/wasm/cmd/aiyou/main.go b/wasm/cmd/aiyou/main.go index 45e9a46..ff60cca 100644 --- a/wasm/cmd/aiyou/main.go +++ b/wasm/cmd/aiyou/main.go @@ -3,11 +3,11 @@ package main import ( "context" "flag" + "fmt" "io/fs" - "math/rand" "net" "os" - "strconv" + "path/filepath" "time" "github.com/tetratelabs/wazero" @@ -26,7 +26,7 @@ var ( func main() { internal.HandleStartup() - ctx := opname.With(context.Background(), "tensei") + ctx := opname.With(context.Background(), "aiyou") data, err := os.ReadFile(*binary) if err != nil { @@ -42,8 +42,13 @@ func main() { ln.FatalErr(ctx, err) } - name := strconv.Itoa(rand.Int()) - config := wazero.NewModuleConfig().WithStdout(os.Stdout).WithStdin(os.Stdin).WithStderr(os.Stderr).WithArgs("aiyou").WithName(name).WithFS(ConnFS{}) + config := wazero.NewModuleConfig(). + // OS stdio + WithStdout(os.Stdout).WithStdin(os.Stdin).WithStderr(os.Stderr). + // Placeholder argv[0] + WithArgs("aiyou").WithName("aiyou"). + // Put network in /dev/net + WithFSConfig(wazero.NewFSConfig().WithFSMount(ConnFS{}, "/dev/")) mod, err := r.InstantiateModule(ctx, code, config) if err != nil { @@ -56,6 +61,8 @@ func main() { type ConnFS struct{} func (ConnFS) Open(name string) (fs.File, error) { + name = filepath.Base(name) + fmt.Println("connecting to", name) conn, err := net.Dial("tcp", name) if err != nil { return nil, err diff --git a/wasm/wasip1/Makefile b/wasm/wasip1/Makefile index c5cc92d..eed2dbd 100644 --- a/wasm/wasip1/Makefile +++ b/wasm/wasip1/Makefile @@ -6,6 +6,9 @@ export GOOS = wasip1 SOURCES := $(wildcard *.go) OBJECTS := $(patsubst %.go, %.wasm, $(SOURCES)) +%.wasm: %.rs + rustc --target=wasm32-wasi $^ + %.wasm: %.go $(GOBIN) build -o $@ $^ diff --git a/wasm/wasip1/echoclient.rs b/wasm/wasip1/echoclient.rs index 8f8e1ed..4049203 100644 --- a/wasm/wasip1/echoclient.rs +++ b/wasm/wasip1/echoclient.rs @@ -3,9 +3,9 @@ use std::io::prelude::*; fn main() -> io::Result<()> { let stdin = io::stdin(); // We get `Stdin` here. - let mut fout = File::create("localhost:1997")?; + let mut fout = File::create("/dev/localhost:1997")?; - print!("input> "); + print!("input> "); io::stdout().lock().flush()?; let mut buf = String::new(); stdin.read_line(&mut buf)?; @@ -17,7 +17,7 @@ fn main() -> io::Result<()> { let mut buf = Vec::new(); fout.read_to_end(&mut buf)?; let buf = unsafe { str::from_utf8_unchecked(&buf) }; - print!("{}", buf); + print!("output> {}", buf); io::stdout().lock().flush()?; Ok(()) |
