-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/wasmtools, all: refinements by @ydnar
.github/workflows: remove some (now) unneeded steps .gitignore: condense internal/wasmtools/.gitignore internal/wasmtools: use single quotes, add Rust edition Makefile: update order of wasm-tools.wasm.\* targets) internal/wasmtools: use gzipped wasm-tools.wasm.gz with sync.Once internal/wasmtools: rebuild wasm-tools.wasm.gz CHANGELOG: wordsmith the wasm-tools and Wazero update internal/wasmtools, wit: remove optional name arg Removing this did not seem to affect tests. internal/wasmtools: leave timeout to the caller Makefile: reorder gzip targets internal/wasmtools: oops Makefile: next try wit: remove check for wasm-tools in PATH internal/wasmtools, wit: swap key and value types for fsMap Not all fs.FS are hashable, but all strings are, so use map[string]fs.FS instead. wit/bindgen: use internal/wasmtools to run wasm-tools in WebAssembly internal/wasmtools: use wazero.CompilationCache This speeds up wit/bindgen tests 10x internal/wasmtools: fix TinyGo impl internal/wasmtools: remove Runner interface This wasn’t used anywhere, so removing. internal/wasmtools: change Run() to accept optional stdout and stderr internal/wasmtools: move args to varadic trailing arg in Run wit: (*testing.common).Errorf does not support error-wrapping directive %w internal/wasmtools, wit/bindgen: additional cleanups for TinyGo and WASI .github/workflows/test: TinyGo needs wasm-tools for -target=wasip2
- Loading branch information
Showing
18 changed files
with
195 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.DS_Store | ||
/generated | ||
/internal/wasmtools/target | ||
/internal/wasmtools/wasm-tools.wasm | ||
go.work.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
[package] | ||
name = "wasm-tools-go" | ||
name = 'wasm-tools-go' | ||
edition = '2021' | ||
|
||
[dependencies] | ||
wasm-tools = { version = "1.221.0", default-features = false, features = [ | ||
"component", | ||
wasm-tools = { version = '1.221.0', default-features = false, features = [ | ||
'component', | ||
] } | ||
|
||
[profile.release] | ||
lto = true # compile with link-time optimization | ||
codegen-units = 1 # compile with a single codegen unit | ||
opt-level = "z" # optimize for size | ||
panic = "abort" # panic=abort will remove the panic code | ||
opt-level = 'z' # optimize for size | ||
panic = 'abort' # panic=abort will remove the panic code | ||
strip = true # strip the debug information |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package wasmtools | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"io/fs" | ||
) | ||
|
||
type instance interface { | ||
Close(ctx context.Context) error | ||
Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, fsMap map[string]fs.FS, args ...string) error | ||
} | ||
|
||
var _ instance = &Instance{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//go:build !tinygo | ||
|
||
package wasmtools | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"crypto/rand" | ||
_ "embed" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"sync" | ||
|
||
"github.com/tetratelabs/wazero" | ||
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" | ||
) | ||
|
||
//go:embed wasm-tools.wasm.gz | ||
var compressed []byte | ||
|
||
var decompress = sync.OnceValues(func() ([]byte, error) { | ||
r, err := gzip.NewReader(bytes.NewReader(compressed)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer r.Close() | ||
var buf bytes.Buffer | ||
_, err = buf.ReadFrom(r) | ||
return buf.Bytes(), err | ||
}) | ||
|
||
var compilationCache = wazero.NewCompilationCache() | ||
|
||
// Instance is a compiled wazero instance. | ||
type Instance struct { | ||
runtime wazero.Runtime | ||
module wazero.CompiledModule | ||
} | ||
|
||
// New creates a new wazero instance. | ||
func New(ctx context.Context) (*Instance, error) { | ||
c := wazero.NewRuntimeConfig(). | ||
WithCloseOnContextDone(true). | ||
WithCompilationCache(compilationCache) | ||
|
||
r := wazero.NewRuntimeWithConfig(ctx, c) | ||
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil { | ||
return nil, fmt.Errorf("error instantiating WASI: %w", err) | ||
} | ||
|
||
wasmTools, err := decompress() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
module, err := r.CompileModule(ctx, wasmTools) | ||
if err != nil { | ||
return nil, fmt.Errorf("error compiling wasm module: %w", err) | ||
} | ||
return &Instance{runtime: r, module: module}, nil | ||
} | ||
|
||
// Close closes the wazero runtime resource. | ||
func (w *Instance) Close(ctx context.Context) error { | ||
return w.runtime.Close(ctx) | ||
} | ||
|
||
// Run runs the wasm module with the context, arguments, | ||
// and optional stdin, stdout, stderr, and filesystem map. | ||
// Supply a context with a timeout or other cancellation mechanism to control execution time. | ||
// Returns an error if instantiation fails. | ||
func (w *Instance) Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, fsMap map[string]fs.FS, args ...string) error { | ||
config := wazero.NewModuleConfig(). | ||
WithRandSource(rand.Reader). | ||
WithSysNanosleep(). | ||
WithSysNanotime(). | ||
WithSysWalltime(). | ||
WithArgs(append([]string{"wasm-tools.wasm"}, args...)...) | ||
|
||
if stdin != nil { | ||
config = config.WithStdin(stdin) | ||
} | ||
if stdout != nil { | ||
config = config.WithStdout(stdout) | ||
} | ||
if stderr != nil { | ||
config = config.WithStderr(stderr) | ||
} | ||
|
||
if len(fsMap) != 0 { | ||
fsConfig := wazero.NewFSConfig() | ||
for guestPath, guestFS := range fsMap { | ||
fsConfig = fsConfig.WithFSMount(guestFS, guestPath) | ||
} | ||
config = config.WithFSConfig(fsConfig) | ||
} | ||
|
||
_, err := w.runtime.InstantiateModule(ctx, w.module, config) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build tinygo | ||
|
||
package wasmtools | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"io/fs" | ||
) | ||
|
||
var errTinyGo = errors.New("wasm-tools disabled under TinyGo") | ||
|
||
type Instance struct{} | ||
|
||
func New(ctx context.Context) (*Instance, error) { | ||
return &Instance{}, errTinyGo | ||
} | ||
|
||
func (w *Instance) Close(ctx context.Context) error { | ||
return errTinyGo | ||
} | ||
|
||
func (w *Instance) Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, fsMap map[string]fs.FS, args ...string) error { | ||
return errTinyGo | ||
} |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.