This moves to a new end-user API under the root package `wazero`. This
simplifies call sites while hardening function calls to their known
return value. Most importantly, this moves most logic internal, as
noted in the RATIONALE.md.
Ex.
```go
// Read WebAssembly binary containing an exported "fac" function.
source, _ := os.ReadFile("./tests/engine/testdata/fac.wasm")
// Decode the binary as WebAssembly module.
mod, _ := wazero.DecodeModuleBinary(source)
// Initialize the execution environment called "store" with Interpreter-based engine.
store := wazero.NewStore()
// Instantiate the module, which returns its exported functions
functions, _ := store.Instantiate(mod)
// Get the factorial function
fac, _ := functions.GetFunctionI64Return("fac")
// Discover 7! is 5040
fmt.Println(fac(context.Background(), 7))
```
PS I changed the README to factorial because the wat version of
fibonacci is not consistent with the TinyGo one!
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takaya Saeki <takaya@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package binary
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"math"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
wasm "github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
func TestLimitsType(t *testing.T) {
|
|
zero := uint32(0)
|
|
max := uint32(math.MaxUint32)
|
|
|
|
tests := []struct {
|
|
name string
|
|
input *wasm.LimitsType
|
|
expected []byte
|
|
}{
|
|
{
|
|
name: "min 0",
|
|
input: &wasm.LimitsType{},
|
|
expected: []byte{0x0, 0},
|
|
},
|
|
{
|
|
name: "min 0, max 0",
|
|
input: &wasm.LimitsType{Max: &zero},
|
|
expected: []byte{0x1, 0, 0},
|
|
},
|
|
{
|
|
name: "min largest",
|
|
input: &wasm.LimitsType{Min: max},
|
|
expected: []byte{0x0, 0xff, 0xff, 0xff, 0xff, 0xf},
|
|
},
|
|
{
|
|
name: "min 0, max largest",
|
|
input: &wasm.LimitsType{Max: &max},
|
|
expected: []byte{0x1, 0, 0xff, 0xff, 0xff, 0xff, 0xf},
|
|
},
|
|
{
|
|
name: "min largest max largest",
|
|
input: &wasm.LimitsType{Min: max, Max: &max},
|
|
expected: []byte{0x1, 0xff, 0xff, 0xff, 0xff, 0xf, 0xff, 0xff, 0xff, 0xff, 0xf},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
b := encodeLimitsType(tc.input)
|
|
t.Run(fmt.Sprintf("encode - %s", tc.name), func(t *testing.T) {
|
|
require.Equal(t, tc.expected, b)
|
|
})
|
|
|
|
t.Run(fmt.Sprintf("decode - %s", tc.name), func(t *testing.T) {
|
|
decoded, err := decodeLimitsType(bytes.NewReader(b))
|
|
require.NoError(t, err)
|
|
require.Equal(t, decoded, tc.input)
|
|
})
|
|
}
|
|
}
|