Skip to content

Commit

Permalink
internal/wasmtools, wit: rename to WasmTools to Instance
Browse files Browse the repository at this point in the history
Signed-off-by: Jiaxiao Zhou <[email protected]>
  • Loading branch information
Mossaka committed Dec 11, 2024
1 parent 78bcda8 commit 4dba4ee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
10 changes: 5 additions & 5 deletions internal/wasmtools/wasmtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type Executor interface {
Run(ctx context.Context, args []string, stdin io.Reader, fsMap map[fs.FS]string, name *string) (stdout io.Reader, stderr io.Reader, err error)
}

type WasmTools struct {
type Instance struct {
runtime wazero.Runtime
module wazero.CompiledModule
}

func NewWasmTools(ctx context.Context) (*WasmTools, error) {
func New(ctx context.Context) (*Instance, error) {
c := wazero.NewRuntimeConfig().WithCloseOnContextDone(true)
r := wazero.NewRuntimeWithConfig(ctx, c)
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
Expand All @@ -39,14 +39,14 @@ func NewWasmTools(ctx context.Context) (*WasmTools, error) {
if err != nil {
return nil, fmt.Errorf("error compiling wasm module: %w", err)
}
return &WasmTools{runtime: r, module: module}, nil
return &Instance{runtime: r, module: module}, nil
}

func (w *WasmTools) Close(ctx context.Context) error {
func (w *Instance) Close(ctx context.Context) error {
return w.runtime.Close(ctx)
}

func (w *WasmTools) Run(ctx context.Context, args []string, stdin io.Reader, fsMap map[fs.FS]string, name *string) (stdout io.Reader, stderr io.Reader, err error) {
func (w *Instance) Run(ctx context.Context, args []string, stdin io.Reader, fsMap map[fs.FS]string, name *string) (stdout io.Reader, stderr io.Reader, err error) {
stdoutBuffer := &bytes.Buffer{}
stderrBuffer := &bytes.Buffer{}

Expand Down
2 changes: 1 addition & 1 deletion wit/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func loadWIT(path string, reader io.Reader) (*Resolve, error) {
} else {
stdin = reader
}
wasmTools, err := wasmtools.NewWasmTools(ctx)
wasmTools, err := wasmtools.New(ctx)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions wit/testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestGoldenWITRoundTrip(t *testing.T) {
return
}
ctx := context.Background()
wasmTools, err := wasmtools.NewWasmTools(ctx)
wasmTools, err := wasmtools.New(ctx)
if err != nil {
t.Skipf("wasm-tools not available: %v", err)
return
Expand All @@ -103,8 +103,10 @@ func TestGoldenWITRoundTrip(t *testing.T) {
if stderr != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(stderr)
t.Error(buf.String())
return
if buf.Len() > 0 {
t.Error(buf.String())
return
}
}
// Parse the JSON into a Resolve.
res2, err := DecodeJSON(stdout)
Expand Down

0 comments on commit 4dba4ee

Please sign in to comment.