initial commit

This commit is contained in:
Peter 2023-05-05 10:11:12 +02:00
parent 53a068b36c
commit 208424de98
No known key found for this signature in database
4 changed files with 90 additions and 0 deletions

View file

@ -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 => ../../
)

8
go.mod Normal file
View file

@ -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
)

9
go.sum Normal file
View file

@ -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=

62
test/test_host.go Normal file
View file

@ -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)
}