Skip to content

Commit

Permalink
rename examples/call to examples/deliver. add ww call subcommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Nov 16, 2024
1 parent 8000daa commit a986945
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
79 changes: 79 additions & 0 deletions cmd/internal/call/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package call

import (
"bytes"
"io"
"os"

"capnproto.org/go/capnp/v3"
"github.com/urfave/cli/v2"
"github.com/wetware/go/proc"
)

func Command() *cli.Command {
return &cli.Command{
Name: "call",
Usage: "generate method call data",
ArgsUsage: "<method>",
Flags: []cli.Flag{
&cli.Uint64SliceFlag{
Name: "push",
Usage: "push a word onto the guest stack",
},
},
Action: func(c *cli.Context) error {
m, seg := capnp.NewSingleSegmentMessage(nil)
defer m.Release()

call, err := proc.NewRootMethodCall(seg)
if err != nil {
return err
}

if err := call.SetName(c.Args().First()); err != nil {
return err
}

stack := c.Uint64Slice("push")
size := int32(len(stack))

s, err := call.NewStack(size)
if err != nil {
return err
}
for i, word := range stack {
s.Set(i, word)
}

if b, err := message(c); err != nil {
return err
} else if err = call.SetCallData(b); err != nil {
return err
}

if b, err := m.Marshal(); err != nil {
return err
} else if _, err = io.Copy(os.Stdout, bytes.NewReader(b)); err != nil {
return err
}

return nil
},
}
}

func message(c *cli.Context) ([]byte, error) {
switch f := c.App.Reader.(type) {
case *os.File:
info, err := f.Stat()
if err != nil {
return nil, err
}

if info.Size() > 0 {
return io.ReadAll(f)
}
}

return nil, nil
}
2 changes: 2 additions & 0 deletions cmd/ww/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/lmittmann/tint"
"github.com/urfave/cli/v2"

"github.com/wetware/go/cmd/internal/call"
"github.com/wetware/go/cmd/internal/export"
"github.com/wetware/go/cmd/internal/run"
)
Expand Down Expand Up @@ -41,6 +42,7 @@ func main() {
Commands: []*cli.Command{
run.Command(),
export.Command(),
call.Command(),
},
}

Expand Down
Binary file removed examples/call/main.wasm
Binary file not shown.
7 changes: 3 additions & 4 deletions examples/call/main.go → examples/deliver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ func main() {
defer f.Close()

var n int64
var status int
if n, err = io.Copy(f, os.Stdin); err != nil {
status = system.StatusFailed
err = fmt.Errorf("request: %w", err)
} else if n, err = io.Copy(os.Stdout, f); err != nil {
status = system.StatusFailed
err = fmt.Errorf("response: %w", err)
}

if err != nil {
slog.Error("failed to read message from stdin",
"reason", err,
"read", n)
os.Exit(system.StatusFailed)
}

os.Exit(status)
slog.Debug("delivered message",
"size", n)
}
Binary file added examples/deliver/main.wasm
Binary file not shown.

0 comments on commit a986945

Please sign in to comment.