Skip to content

Commit

Permalink
Factor out system.SocketConfig.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Jun 1, 2024
1 parent b28364b commit 8050012
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
20 changes: 0 additions & 20 deletions system/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,8 @@ import (
"math"

"github.com/tetratelabs/wazero/api"
"github.com/wetware/go/util"
)

type SocketConfig struct {
Mailbox io.Writer
Deliver api.Function
}

func (c SocketConfig) Bind(ctx context.Context) Proc {
if c.Deliver == nil {
return util.Failf[Proc]("missing export: deliver")
}

return Proc_ServerToClient(c.Build(ctx))
}

func (c SocketConfig) Build(ctx context.Context) (sock Socket) {
sock.Buffer = c.Mailbox
sock.Deliver = c.Deliver
return
}

// Socket is an interface to a process.
type Socket struct {
// Buffer is an io.Writer that accumulates bytes in preparation for
Expand Down
8 changes: 5 additions & 3 deletions system/socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func TestSocket(t *testing.T) {
// or not...
deliver := &mockFn{Name: "deliver", Want: "test"}

proc := system.SocketConfig{
Mailbox: deliver.Mailbox(),
sock := system.Socket{
Buffer: deliver.Mailbox(),
Deliver: deliver,
}.Bind(ctx)
}

proc := system.Proc_ServerToClient(sock)
defer proc.Release()

f, release := proc.Handle(ctx, func(p system.Proc_handle_Params) error {
Expand Down
14 changes: 10 additions & 4 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"capnproto.org/go/capnp/v3/exp/bufferpool"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/wetware/go/util"
)

const ModuleName = "ww"
Expand All @@ -28,10 +29,15 @@ func (m *Module) Stdin() io.Reader {
// Boot returns the system's bootstrap client. This capability is
// analogous to the "root" user in a Unix system.
func (m *Module) Boot(ctx context.Context, mod api.Module) capnp.Client {
socket := SocketConfig{
Mailbox: &m.stdin,
Deliver: mod.ExportedFunction("deliver"),
}.Build(ctx)
deliver := mod.ExportedFunction("deliver")
if deliver == nil {
return util.Failf[capnp.Client]("missing export: deliver")
}

socket := Socket{
Buffer: &m.stdin,
Deliver: deliver,
}

server := Proc_NewServer(socket)
return capnp.NewClient(server)
Expand Down

0 comments on commit 8050012

Please sign in to comment.