Skip to content

Commit

Permalink
Handle directory paths in ww run commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Nov 16, 2024
1 parent 5337c95 commit c9f67e3
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions ww.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package ww

import (
"context"
"errors"
"io"
"path/filepath"

"capnproto.org/go/capnp/v3"
"github.com/blang/semver/v4"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
"github.com/tetratelabs/wazero"
"github.com/wetware/go/proc"
guest "github.com/wetware/go/std/system"
Expand Down Expand Up @@ -95,20 +99,34 @@ func (env Env) Instantiate(ctx context.Context, r wazero.Runtime) (*proc.P, erro
}

func (env Env) LoadAndCompile(ctx context.Context, r wazero.Runtime, name string) (wazero.CompiledModule, error) {
b, err := env.ReadAll(ctx, name)
p, err := path.NewPath(name)
if err != nil {
return nil, err
}

return r.CompileModule(ctx, b)
}

func (env Env) ReadAll(ctx context.Context, name string) ([]byte, error) {
f, err := env.FS.Open(name)
n, err := env.FS.Unix.Get(ctx, p)
if err != nil {
return nil, err
}
defer f.Close()
defer n.Close()

switch node := n.(type) {
case files.File:
b, err := io.ReadAll(node)
if err != nil {
return nil, err
}
return r.CompileModule(ctx, b)

case files.Directory:
it := node.Entries()
for it.Next() {
if it.Name() == "main.wasm" {
child := filepath.Join(p.String(), it.Name())
return env.LoadAndCompile(ctx, r, child)
}
}
}

return io.ReadAll(f)
return nil, errors.New("binary not found")
}

0 comments on commit c9f67e3

Please sign in to comment.