Skip to content

Commit

Permalink
Add ww run command. Executes WASM binaries locally. (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault authored Apr 18, 2023
1 parent 0fd1498 commit e7899a8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cmd/ww/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/wetware/ww/internal/cmd/cluster"
"github.com/wetware/ww/internal/cmd/debug"
"github.com/wetware/ww/internal/cmd/run"
"github.com/wetware/ww/internal/cmd/start"
ww "github.com/wetware/ww/pkg"
)
Expand Down Expand Up @@ -58,11 +59,12 @@ var flags = []cli.Flag{
var commands = []*cli.Command{
start.Command(),
cluster.Command(),
run.Command(),
debug.Command(),
}

func main() {
run(&cli.App{
app := &cli.App{
Name: "wetware",
HelpName: "ww",
Usage: "simple, secure clusters",
Expand All @@ -75,10 +77,8 @@ func main() {
Metadata: map[string]interface{}{
"version": ww.Version,
},
})
}
}

func run(app *cli.App) {
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
Expand Down
79 changes: 79 additions & 0 deletions internal/cmd/run/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package run

import (
"crypto/rand"
"errors"
"io"
"os"

// "github.com/stealthrocket/wazergo"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/urfave/cli/v2"
// "github.com/wetware/ww/pkg/csp/proc"
)

func Command() *cli.Command {
return &cli.Command{
Name: "run",
Usage: "execute a local webassembly process",
Before: setup(),
After: teardown(),
Action: run(),
}
}

var (
r wazero.Runtime
// h *wazergo.ModuleInstance[*proc.Module]
)

func setup() cli.BeforeFunc {
return func(c *cli.Context) error {
r = wazero.NewRuntime(c.Context)
// h = proc.BindModule(c.Context, r)
wasi_snapshot_preview1.MustInstantiate(c.Context, r)
return nil
}
}

func teardown() cli.AfterFunc {
return func(c *cli.Context) error {
return r.Close(c.Context)
}
}

func run() cli.ActionFunc {
return func(c *cli.Context) error {
b, err := bytecode(c)
if err != nil {
return err
}

module, err := r.InstantiateWithConfig(c.Context, b, wazero.NewModuleConfig().
WithRandSource(rand.Reader).
WithStartFunctions(). // disable auto-calling of _start
WithStdout(c.App.Writer).
WithStderr(c.App.ErrWriter))
if err != nil {
return err
}

fn := module.ExportedFunction("_start")
if fn == nil {
return errors.New("ww: missing export: _start")
}

// _, err = fn.Call(wazergo.WithModuleInstance(c.Context, h))
_, err = fn.Call(c.Context)
return err
}
}

func bytecode(c *cli.Context) ([]byte, error) {
if c.Args().Len() > 0 {
return os.ReadFile(c.Args().First()) // file path
}

return io.ReadAll(c.App.Reader) // stdin
}

0 comments on commit e7899a8

Please sign in to comment.