-
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 .github/dependabot: add internal/wasmtools for Cargo updates cmd/wit-bindgen-go, internal/{module,witcli}: extract module helper into new package internal/wasmtools: try to use disk-based compilation cache in TMPDIR internal/cmd/wasm-tools: add Wazero-based wasm-tools executable This removes the requirement for wasmtime in the Makefile Makefile: use Wazero-based internal/cmd/wasm-tools executable instead of Wasmtime .github/workflows/test: ignore all Markdown files internal/wasmtools: rebuild wasm-tools.wasm.gz internal/{cmd/wasm-tools,wasmtools}, wit: default mapping of ./ and / directories .github/workflows: install wasm wrapped in Go version of wasm-tools all: revert 26c1e72; Wazero directory handling tricky to manage Wazero flattens ./ into '', which is treated as /, overriding the / mapping. Makefile: just use wasm-tools It’s faster, and works with arbitrary paths.
- Loading branch information
Showing
22 changed files
with
275 additions
and
214 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
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 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
|
||
"go.bytecodealliance.org/internal/wasmtools" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
wasmTools, err := wasmtools.New(ctx) | ||
if err != nil { | ||
exit(err) | ||
} | ||
|
||
fsMap := map[string]fs.FS{ | ||
"./": os.DirFS("./"), | ||
} | ||
|
||
var args []string | ||
if len(os.Args) != 0 { | ||
args = os.Args[1:] | ||
} | ||
|
||
err = wasmTools.Run(ctx, os.Stdin, os.Stdout, os.Stderr, fsMap, args...) | ||
if err != nil { | ||
exit(err) | ||
} | ||
} | ||
|
||
func exit(err error) { | ||
fmt.Fprintf(os.Stderr, "wasm-tools: %v\n", err) | ||
os.Exit(1) | ||
} |
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,118 @@ | ||
//go:build !tinygo | ||
|
||
package wasmtools | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"crypto/rand" | ||
_ "embed" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/tetratelabs/wazero" | ||
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" | ||
"go.bytecodealliance.org/internal/module" | ||
) | ||
|
||
//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 = sync.OnceValue(func() wazero.CompilationCache { | ||
// First try on-disk cache, so subsequent invocations can share cache | ||
tmp := os.TempDir() | ||
if tmp != "" { | ||
rep := strings.NewReplacer(" ", "", "(", "", ")", "") | ||
dir := filepath.Join(tmp, rep.Replace(module.Path()+"@"+module.Version())) | ||
c, err := wazero.NewCompilationCacheWithDir(dir) | ||
if err == nil { | ||
return c | ||
} | ||
} | ||
|
||
// Fall back to in-memory cache | ||
return 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) | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.