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
2.8 KiB
Go
98 lines
2.8 KiB
Go
package binary
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
wasm "github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
var addLocalZeroLocalTwo = []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 2, wasm.OpcodeI32Add, wasm.OpcodeEnd}
|
|
|
|
func TestEncodeCode(t *testing.T) {
|
|
addLocalZeroLocalOne := []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeI32Add, wasm.OpcodeEnd}
|
|
tests := []struct {
|
|
name string
|
|
input *wasm.Code
|
|
expected []byte
|
|
}{
|
|
{
|
|
name: "smallest function body",
|
|
input: &wasm.Code{ // Ex. (func)
|
|
Body: []byte{wasm.OpcodeEnd},
|
|
},
|
|
expected: []byte{
|
|
0x02, // 2 bytes to encode locals and the body
|
|
0x00, // no local blocks
|
|
wasm.OpcodeEnd, // Body
|
|
},
|
|
},
|
|
{
|
|
name: "params and instructions", // local.get index space is params, then locals
|
|
input: &wasm.Code{ // Ex. (func (type 3) local.get 0 local.get 1 i32.add)
|
|
Body: addLocalZeroLocalOne,
|
|
},
|
|
expected: append([]byte{
|
|
0x07, // 7 bytes to encode locals and the body
|
|
0x00, // no local blocks
|
|
},
|
|
addLocalZeroLocalOne..., // Body
|
|
),
|
|
},
|
|
{
|
|
name: "locals and instructions",
|
|
input: &wasm.Code{ // Ex. (func (result i32) (local i32, i32) local.get 0 local.get 1 i32.add)
|
|
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
|
|
Body: addLocalZeroLocalOne,
|
|
},
|
|
expected: append([]byte{
|
|
0x09, // 9 bytes to encode locals and the body
|
|
0x01, // 1 local block
|
|
0x02, wasm.ValueTypeI32, // local block 1
|
|
},
|
|
addLocalZeroLocalOne..., // Body
|
|
),
|
|
},
|
|
{
|
|
name: "mixed locals and instructions",
|
|
input: &wasm.Code{ // Ex. (func (result i32) (local i32) (local i64) (local i32) local.get 0 local.get 2 i32.add)
|
|
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeI32},
|
|
Body: addLocalZeroLocalTwo,
|
|
},
|
|
expected: append([]byte{
|
|
0x0d, // 13 bytes to encode locals and the body
|
|
0x03, // 3 local blocks
|
|
0x01, wasm.ValueTypeI32, // local block 1
|
|
0x01, wasm.ValueTypeI64, // local block 2
|
|
0x01, wasm.ValueTypeI32, // local block 3
|
|
},
|
|
addLocalZeroLocalTwo..., // Body
|
|
),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
bytes := encodeCode(tc.input)
|
|
require.Equal(t, tc.expected, bytes)
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkEncodeCode(b *testing.B) {
|
|
input := &wasm.Code{ // Ex. (func (result i32) (local i32) (local i64) (local i32) local.get 0 local.get 2 i32.add)
|
|
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeI32},
|
|
Body: addLocalZeroLocalTwo,
|
|
}
|
|
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
if bytes := encodeCode(input); len(bytes) == 0 {
|
|
b.Fatal("didn't encode anything")
|
|
}
|
|
}
|
|
}
|