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>
98 lines
1.8 KiB
Go
98 lines
1.8 KiB
Go
package binary
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
wasm "github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
func TestEncodeExport(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input *wasm.Export
|
|
expected []byte
|
|
}{
|
|
{
|
|
name: "func no name, index 0",
|
|
input: &wasm.Export{ // Ex. (export "" (func 0)))
|
|
Kind: wasm.ExportKindFunc,
|
|
Name: "",
|
|
Index: 0,
|
|
},
|
|
expected: []byte{wasm.ExportKindFunc, 0x00, 0x00},
|
|
},
|
|
{
|
|
name: "func name, func index 0",
|
|
input: &wasm.Export{ // Ex. (export "pi" (func 0))
|
|
Kind: wasm.ExportKindFunc,
|
|
Name: "pi",
|
|
Index: 0,
|
|
},
|
|
expected: []byte{
|
|
0x02, 'p', 'i',
|
|
wasm.ExportKindFunc,
|
|
0x00,
|
|
},
|
|
},
|
|
{
|
|
name: "func name, index 10",
|
|
input: &wasm.Export{ // Ex. (export "pi" (func 10))
|
|
Kind: wasm.ExportKindFunc,
|
|
Name: "pi",
|
|
Index: 10,
|
|
},
|
|
expected: []byte{
|
|
0x02, 'p', 'i',
|
|
wasm.ExportKindFunc,
|
|
0x0a,
|
|
},
|
|
},
|
|
{
|
|
name: "memory no name, index 0",
|
|
input: &wasm.Export{ // Ex. (export "" (memory 0)))
|
|
Kind: wasm.ExportKindMemory,
|
|
Name: "",
|
|
Index: 0,
|
|
},
|
|
expected: []byte{0x00, wasm.ExportKindMemory, 0x00},
|
|
},
|
|
{
|
|
name: "memory name, memory index 0",
|
|
input: &wasm.Export{ // Ex. (export "mem" (memory 0))
|
|
Kind: wasm.ExportKindMemory,
|
|
Name: "mem",
|
|
Index: 0,
|
|
},
|
|
expected: []byte{
|
|
0x03, 'm', 'e', 'm',
|
|
wasm.ExportKindMemory,
|
|
0x00,
|
|
},
|
|
},
|
|
{
|
|
name: "memory name, index 10",
|
|
input: &wasm.Export{ // Ex. (export "mem" (memory 10))
|
|
Kind: wasm.ExportKindMemory,
|
|
Name: "mem",
|
|
Index: 10,
|
|
},
|
|
expected: []byte{
|
|
0x03, 'm', 'e', 'm',
|
|
wasm.ExportKindMemory,
|
|
0x0a,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
bytes := encodeExport(tc.input)
|
|
require.Equal(t, tc.expected, bytes)
|
|
})
|
|
}
|
|
}
|