From 208424de988e8ca9b7a4ac9f00403ba3e5a41ca8 Mon Sep 17 00:00:00 2001 From: Peter Kurfer Date: Fri, 5 May 2023 10:11:12 +0200 Subject: [PATCH] initial commit --- examples/hello_world/go.mod | 11 +++++++ go.mod | 8 +++++ go.sum | 9 ++++++ test/test_host.go | 62 +++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 examples/hello_world/go.mod create mode 100644 go.mod create mode 100644 go.sum create mode 100644 test/test_host.go diff --git a/examples/hello_world/go.mod b/examples/hello_world/go.mod new file mode 100644 index 0000000..a059774 --- /dev/null +++ b/examples/hello_world/go.mod @@ -0,0 +1,11 @@ +module hello_world + +go 1.20 + +require ( +code.icb4dc0.de/buildr/wasi-module-sdk latest +) + +replace ( + code.icb4dc0.de/buildr/wasi-module-sdk => ../../ +) \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7ba3f28 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module code.icb4dc0.de/buildr/wasi-module-sdk + +go 1.20 + +require ( + buf.build/gen/go/buildr/buildr/protocolbuffers/go v1.30.0-20230504155321-a2ad89049f80.1 // indirect + github.com/tetratelabs/wazero v1.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..94155d4 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +buf.build/gen/go/buildr/buildr/protocolbuffers/go v1.30.0-20230504155321-a2ad89049f80.1 h1:gDR2MbPrmDLMU4SU83sLG53G1BO2jWLZRSwA7Zx/ORs= +buf.build/gen/go/buildr/buildr/protocolbuffers/go v1.30.0-20230504155321-a2ad89049f80.1/go.mod h1:HnJwr6AoSkS1h+Fr8+UR7MDcZMzmv7lJPJWeTjdclPs= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/tetratelabs/wazero v1.1.0 h1:EByoAhC+QcYpwSZJSs/aV0uokxPwBgKxfiokSUwAknQ= +github.com/tetratelabs/wazero v1.1.0/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= diff --git a/test/test_host.go b/test/test_host.go new file mode 100644 index 0000000..08a67fe --- /dev/null +++ b/test/test_host.go @@ -0,0 +1,62 @@ +package test + +import ( + rpcv1 "buf.build/gen/go/buildr/buildr/protocolbuffers/go/rpc/v1" + "context" + "errors" + "github.com/tetratelabs/wazero" + "github.com/tetratelabs/wazero/api" + "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" + "golang.org/x/exp/slog" + "google.golang.org/protobuf/proto" +) + +type Host struct { + Logger *slog.Logger +} + +func (h *Host) Run(ctx context.Context) (err error) { + r := wazero.NewRuntime(ctx) + defer r.Close(context.Background()) + + _, err = r.NewHostModuleBuilder("test"). + NewFunctionBuilder().WithFunc(h.log).Export("log"). + Instantiate(ctx) + + if err != nil { + return err + } + + closer, err := wasi_snapshot_preview1.Instantiate(ctx, r) + if err != nil { + return err + } + + defer func() { + err = errors.Join(err, closer.Close(context.Background())) + }() + + return nil +} + +func (h *Host) log(ctx context.Context, m api.Module, offset, byteCount uint32) { + buf, ok := m.Memory().Read(offset, byteCount) + if !ok { + return + } + + var taskLog *rpcv1.TaskLog + if err := proto.Unmarshal(buf, taskLog); err != nil { + h.Logger.Warn("failed to unmarshal task log", slog.String("err", err.Error())) + return + } + + rec := slog.NewRecord(taskLog.Time.AsTime(), slog.Level(taskLog.Level), taskLog.Message, -1) + + for i := range taskLog.Attributes { + attr := taskLog.Attributes[i] + rec.AddAttrs(slog.String(attr.Key, attr.Value)) + } + + _ = h.Logger.Handler().Handle(ctx, rec) +}