During #425, @neilalexander gave constructive feedback that the API is both moving fast, and not good enough yet. This attempts to reduce the incidental complexity at the cost of a little conflation. ### odd presence of `wasm` and `wasi` packages -> `api` package We had public API packages in wasm and wasi, which helped us avoid leaking too many internals as public. That these had names that look like there should be implementations in them cause unnecessary confusion. This squashes both into one package "api" which has no package collission with anything. We've long struggled with the poorly specified and non-uniformly implemented WASI specification. Trying to bring visibility to its constraints knowing they are routinely invalid taints our API for no good reason. This removes all `WASI` commands for a default to invoke the function `_start` if it exists. In doing so, there's only one path to start a module. Moreover, this puts all wasi code in a top-level package "wasi" as it isn't re-imported by any internal types. ### Reuse of Module for pre and post instantiation to `Binary` -> `Module` Module is defined by WebAssembly in many phases, from decoded to instantiated. However, using the same noun in multiple packages is very confusing. We at one point tried a name "DecodedModule" or "InstantiatedModule", but this is a fools errand. By deviating slightly from the spec we can make it unambiguous what a module is. This make a result of compilation a `Binary`, retaining `Module` for an instantiated one. In doing so, there's no longer any name conflicts whatsoever. ### Confusion about config -> `ModuleConfig` Also caused by splitting wasm into wasm+wasi is configuration. This conflates both into the same type `ModuleConfig` as it is simpler than trying to explain a "will never be finished" api of wasi snapshot-01 in routine use of WebAssembly. In other words, this further moves WASI out of the foreground as it has been nothing but burden. ```diff --- a/README.md +++ b/README.md @@ -49,8 +49,8 @@ For example, here's how you can allow WebAssembly modules to read -wm, err := r.InstantiateModule(wazero.WASISnapshotPreview1()) -defer wm.Close() +wm, err := wasi.InstantiateSnapshotPreview1(r) +defer wm.Close() -sysConfig := wazero.NewSysConfig().WithFS(os.DirFS("/work/home")) -module, err := wazero.StartWASICommandWithConfig(r, compiled, sysConfig) +config := wazero.ModuleConfig().WithFS(os.DirFS("/work/home")) +module, err := r.InstantiateModule(binary, config) defer module.Close() ... ```
86 lines
2.6 KiB
Go
86 lines
2.6 KiB
Go
package binary
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"unicode/utf8"
|
|
|
|
"github.com/tetratelabs/wazero/internal/leb128"
|
|
"github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
var noValType = []byte{0}
|
|
|
|
// encodedValTypes is a cache of size prefixed binary encoding of known val types.
|
|
var encodedValTypes = map[wasm.ValueType][]byte{
|
|
wasm.ValueTypeI32: {1, wasm.ValueTypeI32},
|
|
wasm.ValueTypeI64: {1, wasm.ValueTypeI64},
|
|
wasm.ValueTypeF32: {1, wasm.ValueTypeF32},
|
|
wasm.ValueTypeF64: {1, wasm.ValueTypeF64},
|
|
}
|
|
|
|
// encodeValTypes fast paths binary encoding of common value type lengths
|
|
func encodeValTypes(vt []wasm.ValueType) []byte {
|
|
// Special case nullary and parameter lengths of wasi_snapshot_preview1 to avoid excess allocations
|
|
switch uint32(len(vt)) {
|
|
case 0: // nullary
|
|
return noValType
|
|
case 1: // ex $wasi.fd_close or any result
|
|
if encoded, ok := encodedValTypes[vt[0]]; ok {
|
|
return encoded
|
|
}
|
|
case 2: // ex $wasi.environ_sizes_get
|
|
return []byte{2, vt[0], vt[1]}
|
|
case 4: // ex $wasi.fd_write
|
|
return []byte{4, vt[0], vt[1], vt[2], vt[3]}
|
|
case 9: // ex $wasi.fd_write
|
|
return []byte{9, vt[0], vt[1], vt[2], vt[3], vt[4], vt[5], vt[6], vt[7], vt[8]}
|
|
}
|
|
// Slow path others until someone complains with a valid signature
|
|
count := leb128.EncodeUint32(uint32(len(vt)))
|
|
return append(count, vt...)
|
|
}
|
|
|
|
func decodeValueTypes(r *bytes.Reader, num uint32) ([]wasm.ValueType, error) {
|
|
if num == 0 {
|
|
return nil, nil
|
|
}
|
|
ret := make([]wasm.ValueType, num)
|
|
buf := make([]wasm.ValueType, num)
|
|
_, err := io.ReadFull(r, buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i, v := range buf {
|
|
switch v {
|
|
case wasm.ValueTypeI32, wasm.ValueTypeF32, wasm.ValueTypeI64, wasm.ValueTypeF64:
|
|
ret[i] = v
|
|
default:
|
|
return nil, fmt.Errorf("invalid value type: %d", v)
|
|
}
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
// decodeUTF8 decodes a size prefixed string from the reader, returning it and the count of bytes read.
|
|
// contextFormat and contextArgs apply an error format when present
|
|
func decodeUTF8(r *bytes.Reader, contextFormat string, contextArgs ...interface{}) (string, uint32, error) {
|
|
size, sizeOfSize, err := leb128.DecodeUint32(r)
|
|
if err != nil {
|
|
return "", 0, fmt.Errorf("failed to read %s size: %w", fmt.Sprintf(contextFormat, contextArgs...), err)
|
|
}
|
|
|
|
buf := make([]byte, size)
|
|
if _, err = io.ReadFull(r, buf); err != nil {
|
|
return "", 0, fmt.Errorf("failed to read %s: %w", fmt.Sprintf(contextFormat, contextArgs...), err)
|
|
}
|
|
|
|
if !utf8.Valid(buf) {
|
|
return "", 0, fmt.Errorf("%s is not valid UTF-8", fmt.Sprintf(contextFormat, contextArgs...))
|
|
}
|
|
|
|
return string(buf), size + uint32(sizeOfSize), nil
|
|
}
|