diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-06-19 12:06:07 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-06-19 12:06:07 -0400 |
| commit | 673f8325e2116f5645eeada3727b81f6c37982ab (patch) | |
| tree | a98857e5fe531addd8d98aef9776273e0eb45241 /wasm | |
| parent | b797446d27d246df7384446215bd3ca37f0f930f (diff) | |
| download | x-673f8325e2116f5645eeada3727b81f6c37982ab.tar.xz x-673f8325e2116f5645eeada3727b81f6c37982ab.zip | |
GopherCon EU 2023 code
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'wasm')
| -rw-r--r-- | wasm/cmd/aiyou/main.go | 91 | ||||
| -rw-r--r-- | wasm/cmd/tensei/main.go | 94 | ||||
| -rw-r--r-- | wasm/main.go | 7 | ||||
| -rw-r--r-- | wasm/wasip1/.gitignore | 1 | ||||
| -rw-r--r-- | wasm/wasip1/Makefile | 15 | ||||
| -rw-r--r-- | wasm/wasip1/cat.go | 33 | ||||
| -rw-r--r-- | wasm/wasip1/echoclient.rs | 24 | ||||
| -rw-r--r-- | wasm/wasip1/envdump.go | 18 | ||||
| -rw-r--r-- | wasm/wasip1/hello.go | 9 | ||||
| -rw-r--r-- | wasm/wasip1/hello.rs | 3 | ||||
| -rw-r--r-- | wasm/wasip1/promptreply.rs | 9 |
11 files changed, 0 insertions, 304 deletions
diff --git a/wasm/cmd/aiyou/main.go b/wasm/cmd/aiyou/main.go deleted file mode 100644 index ff60cca..0000000 --- a/wasm/cmd/aiyou/main.go +++ /dev/null @@ -1,91 +0,0 @@ -package main - -import ( - "context" - "flag" - "fmt" - "io/fs" - "net" - "os" - "path/filepath" - "time" - - "github.com/tetratelabs/wazero" - "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" - "within.website/ln" - "within.website/ln/opname" - "within.website/x/internal" -) - -var ( - r wazero.Runtime - code wazero.CompiledModule - - binary = flag.String("wasm-binary", "./bin.wasm", "binary to run against every line of input from connections") -) - -func main() { - internal.HandleStartup() - ctx := opname.With(context.Background(), "aiyou") - - data, err := os.ReadFile(*binary) - if err != nil { - ln.FatalErr(ctx, err) - } - - r = wazero.NewRuntime(ctx) - - wasi_snapshot_preview1.MustInstantiate(ctx, r) - - code, err = r.CompileModule(ctx, data) - if err != nil { - ln.FatalErr(ctx, err) - } - - 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 { - ln.Error(ctx, err) - return - } - defer mod.Close(ctx) -} - -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 - } - - return ConnFile{Conn: conn}, nil -} - -type ConnFile struct { - net.Conn -} - -func (c ConnFile) Stat() (fs.FileInfo, error) { - return ConnFileInfo{c.Conn}, nil -} - -type ConnFileInfo struct { - conn net.Conn -} - -func (c ConnFileInfo) Name() string { return c.conn.RemoteAddr().String() } // base name of the file -func (c ConnFileInfo) Size() int64 { return 0 } // length in bytes for regular files; system-dependent for others -func (c ConnFileInfo) Mode() fs.FileMode { return 0 } // file mode bits -func (c ConnFileInfo) ModTime() time.Time { return time.Now() } // modification time -func (c ConnFileInfo) IsDir() bool { return false } // abbreviation for Mode().IsDir() -func (c ConnFileInfo) Sys() any { return c.conn } // underlying data source (can return nil) diff --git a/wasm/cmd/tensei/main.go b/wasm/cmd/tensei/main.go deleted file mode 100644 index 987ff48..0000000 --- a/wasm/cmd/tensei/main.go +++ /dev/null @@ -1,94 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "context" - "flag" - "fmt" - "log" - "math/rand" - "net" - "os" - "strconv" - - "github.com/tetratelabs/wazero" - "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" - "within.website/ln" - "within.website/ln/opname" - "within.website/x/internal" -) - -var ( - r wazero.Runtime - code wazero.CompiledModule - - binary = flag.String("wasm-binary", "./bin.wasm", "binary to run against every line of input from connections") - bind = flag.String("bind", ":1997", "TCP host:port to bind on") -) - -func main() { - internal.HandleStartup() - ctx := opname.With(context.Background(), "tensei") - - data, err := os.ReadFile(*binary) - if err != nil { - ln.FatalErr(ctx, err) - } - - r = wazero.NewRuntime(ctx) - - wasi_snapshot_preview1.MustInstantiate(ctx, r) - - code, err = r.CompileModule(ctx, data) - if err != nil { - ln.FatalErr(ctx, err) - } - - server, err := net.Listen("tcp", *bind) - if err != nil { - ln.FatalErr(ctx, err) - } - - for { - conn, err := server.Accept() - if err != nil { - log.Println("Failed to accept conn.", err) - continue - } - - fmt.Println(conn.RemoteAddr().String()) - - go func(conn net.Conn) { - defer func() { - fmt.Println("disconnect") - conn.Close() - }() - - scn := bufio.NewScanner(conn) - scn.Split(bufio.ScanLines) - - for scn.Scan() { - fout := &bytes.Buffer{} - fin := bytes.NewBuffer(scn.Bytes()) - - fmt.Println("<", fin.String()) - - name := strconv.Itoa(rand.Int()) - config := wazero.NewModuleConfig().WithStdout(fout).WithStdin(fin).WithArgs("mastosan").WithName(name) - - mod, err := r.InstantiateModule(ctx, code, config) - if err != nil { - ln.Error(ctx, err, ln.F{"remote_host": conn.RemoteAddr().String()}) - return - } - defer mod.Close(ctx) - - fmt.Print(">", fout.String()) - - conn.Write(fout.Bytes()) - conn.Close() - } - }(conn) - } -} diff --git a/wasm/main.go b/wasm/main.go deleted file mode 100644 index 7eab755..0000000 --- a/wasm/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("testing a wasm module from buildGoWasiModule") -} diff --git a/wasm/wasip1/.gitignore b/wasm/wasip1/.gitignore deleted file mode 100644 index 19e1bce..0000000 --- a/wasm/wasip1/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.wasm diff --git a/wasm/wasip1/Makefile b/wasm/wasip1/Makefile deleted file mode 100644 index eed2dbd..0000000 --- a/wasm/wasip1/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -GOBIN = gowasi - -export GOARCH = wasm -export GOOS = wasip1 - -SOURCES := $(wildcard *.go) -OBJECTS := $(patsubst %.go, %.wasm, $(SOURCES)) - -%.wasm: %.rs - rustc --target=wasm32-wasi $^ - -%.wasm: %.go - $(GOBIN) build -o $@ $^ - -all: $(OBJECTS) diff --git a/wasm/wasip1/cat.go b/wasm/wasip1/cat.go deleted file mode 100644 index 7033dbd..0000000 --- a/wasm/wasip1/cat.go +++ /dev/null @@ -1,33 +0,0 @@ -//go:build ignore - -package main - -import ( - "flag" - "fmt" - "io" - "log" - "os" -) - -func main() { - flag.Usage = func() { - fmt.Printf("%s <file>\n\nprints file to standard out\n", os.Args[0]) - } - flag.Parse() - - if flag.NArg() != 1 { - log.Fatalf("wanted 1 arg, got %#v", os.Args) - } - - fin, err := os.Open(flag.Arg(0)) - if err != nil { - log.Fatal(err) - } - defer fin.Close() - - _, err = io.Copy(os.Stdout, fin) - if err != nil { - log.Fatal(err) - } -} diff --git a/wasm/wasip1/echoclient.rs b/wasm/wasip1/echoclient.rs deleted file mode 100644 index 4049203..0000000 --- a/wasm/wasip1/echoclient.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::{io, fs::File, str, thread, time}; -use std::io::prelude::*; - -fn main() -> io::Result<()> { - let stdin = io::stdin(); // We get `Stdin` here. - let mut fout = File::create("/dev/localhost:1997")?; - - print!("input> "); - io::stdout().lock().flush()?; - let mut buf = String::new(); - stdin.read_line(&mut buf)?; - write!(fout, "{}", buf)?; - - let ten_millis = time::Duration::from_millis(10); - thread::sleep(ten_millis); - - let mut buf = Vec::new(); - fout.read_to_end(&mut buf)?; - let buf = unsafe { str::from_utf8_unchecked(&buf) }; - print!("output> {}", buf); - io::stdout().lock().flush()?; - - Ok(()) -} diff --git a/wasm/wasip1/envdump.go b/wasm/wasip1/envdump.go deleted file mode 100644 index 6f940c4..0000000 --- a/wasm/wasip1/envdump.go +++ /dev/null @@ -1,18 +0,0 @@ -//go:build ignore - -package main - -import ( - "fmt" - "os" -) - -func main() { - if len(os.Environ()) == 0 { - fmt.Println("No environment variables found") - return - } - for _, kv := range os.Environ() { - fmt.Println(kv) - } -} diff --git a/wasm/wasip1/hello.go b/wasm/wasip1/hello.go deleted file mode 100644 index 78a7f8f..0000000 --- a/wasm/wasip1/hello.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build ignore - -package main - -import "log" - -func main() { - log.Println("Hello, world!") -} diff --git a/wasm/wasip1/hello.rs b/wasm/wasip1/hello.rs deleted file mode 100644 index b4998a8..0000000 --- a/wasm/wasip1/hello.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("hello, world!") -} diff --git a/wasm/wasip1/promptreply.rs b/wasm/wasip1/promptreply.rs deleted file mode 100644 index 09d29db..0000000 --- a/wasm/wasip1/promptreply.rs +++ /dev/null @@ -1,9 +0,0 @@ -use std::io; - -fn main() -> io::Result<()> { - let mut buffer = String::new(); - let stdin = io::stdin(); // We get `Stdin` here. - stdin.read_line(&mut buffer)?; - println!("{}", buffer.trim()); - Ok(()) -} |
