| 1 |
package table_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/tetratelabs/wazero" |
| 8 |
"github.com/tetratelabs/wazero/api" |
| 9 |
"github.com/tetratelabs/wazero/experimental/table" |
| 10 |
"github.com/tetratelabs/wazero/internal/testing/binaryencoding" |
| 11 |
"github.com/tetratelabs/wazero/internal/testing/require" |
| 12 |
"github.com/tetratelabs/wazero/internal/wasm" |
| 13 |
) |
| 14 |
|
| 15 |
func TestLookupFunction(t *testing.T) { |
| 16 |
const i32 = wasm.ValueTypeI32 |
| 17 |
const i32a = api.ValueTypeI32 |
| 18 |
bytes := binaryencoding.EncodeModule(&wasm.Module{ |
| 19 |
TypeSection: []wasm.FunctionType{ |
| 20 |
{Results: []wasm.ValueType{i32}}, |
| 21 |
{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32, i32}}, |
| 22 |
}, |
| 23 |
FunctionSection: []wasm.Index{0, 1}, |
| 24 |
CodeSection: []wasm.Code{ |
| 25 |
{Body: []byte{ |
| 26 |
wasm.OpcodeI32Const, 1, |
| 27 |
wasm.OpcodeEnd, |
| 28 |
}}, |
| 29 |
{Body: []byte{ |
| 30 |
|
| 31 |
wasm.OpcodeLocalGet, 1, |
| 32 |
wasm.OpcodeLocalGet, 0, |
| 33 |
wasm.OpcodeEnd, |
| 34 |
}}, |
| 35 |
}, |
| 36 |
ElementSection: []wasm.ElementSegment{ |
| 37 |
{ |
| 38 |
OffsetExpr: wasm.NewConstantExpressionFromI32(0), |
| 39 |
Init: []wasm.ConstantExpression{ |
| 40 |
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, []byte{0}), |
| 41 |
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, []byte{1}), |
| 42 |
}, |
| 43 |
Type: wasm.RefTypeFuncref, |
| 44 |
}, |
| 45 |
}, |
| 46 |
TableSection: []wasm.Table{{Type: wasm.RefTypeFuncref, Min: 100}}, |
| 47 |
}) |
| 48 |
|
| 49 |
r := wazero.NewRuntime(context.Background()) |
| 50 |
m, err := r.Instantiate(context.Background(), bytes) |
| 51 |
require.NoError(t, err) |
| 52 |
require.NotNil(t, m) |
| 53 |
|
| 54 |
t.Run("v_i32", func(t *testing.T) { |
| 55 |
f := table.LookupFunction(m, 0, 0, nil, []api.ValueType{i32a}) |
| 56 |
var result [1]uint64 |
| 57 |
err = f.CallWithStack(context.Background(), result[:]) |
| 58 |
require.NoError(t, err) |
| 59 |
require.Equal(t, uint64(1), result[0]) |
| 60 |
}) |
| 61 |
t.Run("i32i32_i32i32", func(t *testing.T) { |
| 62 |
f := table.LookupFunction(m, 0, 1, []api.ValueType{i32a, i32a}, []api.ValueType{i32a, i32a}) |
| 63 |
stack := [2]uint64{100, 200} |
| 64 |
err = f.CallWithStack(context.Background(), stack[:]) |
| 65 |
require.NoError(t, err) |
| 66 |
require.Equal(t, uint64(200), stack[0]) |
| 67 |
require.Equal(t, uint64(100), stack[1]) |
| 68 |
}) |
| 69 |
|
| 70 |
t.Run("panics", func(t *testing.T) { |
| 71 |
err := require.CapturePanic(func() { |
| 72 |
table.LookupFunction(m, 0, 2000, nil, []api.ValueType{i32a}) |
| 73 |
}) |
| 74 |
require.Equal(t, "invalid table access", err.Error()) |
| 75 |
err = require.CapturePanic(func() { |
| 76 |
table.LookupFunction(m, 1000, 0, nil, []api.ValueType{i32a}) |
| 77 |
}) |
| 78 |
require.Equal(t, "table index out of range", err.Error()) |
| 79 |
err = require.CapturePanic(func() { |
| 80 |
table.LookupFunction(m, 0, 0, nil, []api.ValueType{api.ValueTypeF32}) |
| 81 |
}) |
| 82 |
require.Equal(t, "indirect call type mismatch", err.Error()) |
| 83 |
err = require.CapturePanic(func() { |
| 84 |
table.LookupFunction(m, 0, 0, []api.ValueType{i32a}, nil) |
| 85 |
}) |
| 86 |
require.Equal(t, "indirect call type mismatch", err.Error()) |
| 87 |
err = require.CapturePanic(func() { |
| 88 |
table.LookupFunction(m, 0, 1, []api.ValueType{i32a, i32a}, nil) |
| 89 |
}) |
| 90 |
require.Equal(t, "indirect call type mismatch", err.Error()) |
| 91 |
err = require.CapturePanic(func() { |
| 92 |
table.LookupFunction(m, 0, 1, []api.ValueType{i32a, i32a}, []api.ValueType{i32a, api.ValueTypeF32}) |
| 93 |
}) |
| 94 |
require.Equal(t, "indirect call type mismatch", err.Error()) |
| 95 |
}) |
| 96 |
} |