Skip to content

Commit

Permalink
Move fs tests to testdata/fs.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Aug 14, 2024
1 parent 5e25d47 commit db3b7a3
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 3 deletions.
Binary file modified examples/hello-world/main.wasm
Binary file not shown.
Binary file modified system/cmd/nop/main.wasm
Binary file not shown.
10 changes: 7 additions & 3 deletions system/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@ import (
"github.com/wetware/go/system"
)

const IPFS_ROOT = "/ipfs/QmcKcGYiGcfSQ1s3VX7SnzQwRZHsgAuuPXdnrRghjCrBMx/system/testdata/fs"

func TestFS(t *testing.T) {
t.Parallel()

root, err := path.NewPath("/ipfs/QmQuTsZYyFSVXD8r6yfWyJyJ5xhzV8wkqy9wWuTeoccDtW")
root, err := path.NewPath(IPFS_ROOT)
require.NoError(t, err)

ipfs, err := rpc.NewLocalApi()
require.NoError(t, err)

fs := system.FS{Ctx: context.Background(), API: ipfs.Unixfs(), Root: root}
err = fstest.TestFS(fs,
"main.go",
"main.wasm",
"testdata")
require.NoError(t, err)
}

func TestIPFSNode(t *testing.T) {
t.Parallel()

root, err := path.NewPath("/ipfs/QmQuTsZYyFSVXD8r6yfWyJyJ5xhzV8wkqy9wWuTeoccDtW")
root, err := path.NewPath(IPFS_ROOT)
require.NoError(t, err)

ipfs, err := rpc.NewLocalApi()
Expand All @@ -43,7 +47,7 @@ func TestIPFSNode(t *testing.T) {
names = append(names, entry.Name)
}

expect := []string{"testdata"}
expect := []string{"testdata", "main.go", "main.wasm"}
require.ElementsMatch(t, names, expect,
"unexpected file path")
}
84 changes: 84 additions & 0 deletions system/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package system

import (
"context"
"fmt"
"log/slog"

"github.com/libp2p/go-libp2p/core/host"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)

type HostConfig struct {
NS string
Host host.Host
}

func (c HostConfig) Instantiate(ctx context.Context, r wazero.Runtime) (api.Module, error) {
return r.NewHostModuleBuilder("ww").
NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(c.Send),
[]api.ValueType{MemorySegment(0).ValueType()}, // params
[]api.ValueType{}). // return values
Export("send").
Instantiate(ctx)
}

func (c HostConfig) Send(ctx context.Context, mod api.Module, stack []uint64) {
if len(stack) != MemorySegment(0).NumWords() {
slog.ErrorContext(ctx, "unable to send message",
"reason", "unexpected number of parameters to host function ww::send",
"stack", stack)
return
}

mem := mod.Memory()
seg := MemorySegment(stack[0])
msg, ok := seg.Load(mem)
if !ok {
slog.ErrorContext(ctx, "out-of-bounds memory access",
"offset", seg.Offset(),
"length", seg.Length())
return
}

fmt.Println(string(msg))

// TODO: gotta send the message somewhere

// s, err := c.Host.NewStream(ctx, message.To(), message.Protos()...) // TODO: do we need to cache streams?
// if err != nil {
// // mod.CloseWithExitCode()
// }
}

type MemorySegment uint64

func NewMemorySegment(offset, length uint32) MemorySegment {
s := MemorySegment(offset) << 32
s |= MemorySegment(length)
return s
}

func (MemorySegment) NumWords() int {
return 1
}

func (MemorySegment) ValueType() api.ValueType {
return api.ValueTypeI64
}

func (s MemorySegment) Offset() uint32 {
return uint32(s >> 32) // 32 leftmost bits
}

func (s MemorySegment) Length() uint32 {
return uint32(s) // 32 rightmost bits
}

func (s MemorySegment) Load(mem api.Memory) ([]byte, bool) {
offset := s.Offset()
length := s.Length()
return mem.Read(offset, length)
}
18 changes: 18 additions & 0 deletions system/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package system_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/wetware/go/system"
)

func TestMemorySegment(t *testing.T) {
t.Parallel()

seg := system.NewMemorySegment(1, 2)
t.Logf("%064b", seg)

assert.Equal(t, uint32(1), seg.Offset())
assert.Equal(t, uint32(2), seg.Length())
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions ww.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ func (c Cluster) Serve(ctx context.Context) error {
}
defer wasi.Close(ctx)

sys, err := system.HostConfig{
NS: c.NS,
Host: c.Host,
}.Instantiate(ctx, r)
if err != nil {
return err
}
defer sys.Close(ctx)

compiled, err := c.CompileNode(ctx, r, root)
if err != nil {
return err
Expand Down

0 comments on commit db3b7a3

Please sign in to comment.