| 1 |
package wasm |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"math" |
| 6 |
"testing" |
| 7 |
"unsafe" |
| 8 |
|
| 9 |
"github.com/tetratelabs/wazero/api" |
| 10 |
"github.com/tetratelabs/wazero/internal/testing/require" |
| 11 |
) |
| 12 |
|
| 13 |
type arbitrary struct{} |
| 14 |
|
| 15 |
|
| 16 |
var testCtx = context.WithValue(context.Background(), arbitrary{}, "arbitrary") |
| 17 |
|
| 18 |
func Test_parseGoFunc(t *testing.T) { |
| 19 |
tests := []struct { |
| 20 |
name string |
| 21 |
input interface{} |
| 22 |
expectNeedsModule bool |
| 23 |
expectedType *FunctionType |
| 24 |
}{ |
| 25 |
{ |
| 26 |
name: "() -> ()", |
| 27 |
input: func() {}, |
| 28 |
expectedType: &FunctionType{}, |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "(ctx) -> ()", |
| 32 |
input: func(context.Context) {}, |
| 33 |
expectedType: &FunctionType{}, |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "(ctx, mod) -> ()", |
| 37 |
input: func(context.Context, api.Module) {}, |
| 38 |
expectNeedsModule: true, |
| 39 |
expectedType: &FunctionType{}, |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "all supported params and i32 result", |
| 43 |
input: func(uint32, uint64, float32, float64, uintptr) uint32 { return 0 }, |
| 44 |
expectedType: &FunctionType{Params: []ValueType{i32, i64, f32, f64, externref}, Results: []ValueType{i32}}, |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "all supported params and i32 result - (ctx)", |
| 48 |
input: func(context.Context, uint32, uint64, float32, float64, uintptr) uint32 { return 0 }, |
| 49 |
expectedType: &FunctionType{Params: []ValueType{i32, i64, f32, f64, externref}, Results: []ValueType{i32}}, |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "all supported params and i32 result - (ctx, mod)", |
| 53 |
input: func(context.Context, api.Module, uint32, uint64, float32, float64, uintptr) uint32 { return 0 }, |
| 54 |
expectNeedsModule: true, |
| 55 |
expectedType: &FunctionType{Params: []ValueType{i32, i64, f32, f64, externref}, Results: []ValueType{i32}}, |
| 56 |
}, |
| 57 |
} |
| 58 |
for _, tt := range tests { |
| 59 |
tc := tt |
| 60 |
|
| 61 |
t.Run(tc.name, func(t *testing.T) { |
| 62 |
paramTypes, resultTypes, code, err := parseGoReflectFunc(tc.input) |
| 63 |
require.NoError(t, err) |
| 64 |
_, isModuleFunc := code.GoFunc.(api.GoModuleFunction) |
| 65 |
require.Equal(t, tc.expectNeedsModule, isModuleFunc) |
| 66 |
require.Equal(t, tc.expectedType, &FunctionType{Params: paramTypes, Results: resultTypes}) |
| 67 |
}) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func Test_parseGoFunc_Errors(t *testing.T) { |
| 72 |
tests := []struct { |
| 73 |
name string |
| 74 |
input interface{} |
| 75 |
expectedErr string |
| 76 |
}{ |
| 77 |
{ |
| 78 |
name: "module no context", |
| 79 |
input: func(api.Module) {}, |
| 80 |
expectedErr: "invalid signature: api.Module parameter must be preceded by context.Context", |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "not a func", |
| 84 |
input: struct{}{}, |
| 85 |
expectedErr: "kind != func: struct", |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "unsupported param", |
| 89 |
input: func(context.Context, uint32, string) {}, |
| 90 |
expectedErr: "param[2] is unsupported: string", |
| 91 |
}, |
| 92 |
{ |
| 93 |
name: "unsupported result", |
| 94 |
input: func() string { return "" }, |
| 95 |
expectedErr: "result[0] is unsupported: string", |
| 96 |
}, |
| 97 |
{ |
| 98 |
name: "error result", |
| 99 |
input: func() error { return nil }, |
| 100 |
expectedErr: "result[0] is an error, which is unsupported", |
| 101 |
}, |
| 102 |
{ |
| 103 |
name: "incorrect order", |
| 104 |
input: func(api.Module, context.Context) error { return nil }, |
| 105 |
expectedErr: "invalid signature: api.Module parameter must be preceded by context.Context", |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "multiple context.Context", |
| 109 |
input: func(context.Context, uint64, context.Context) error { return nil }, |
| 110 |
expectedErr: "param[2] is a context.Context, which may be defined only once as param[0]", |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "multiple wasm.Module", |
| 114 |
input: func(context.Context, api.Module, uint64, api.Module) error { return nil }, |
| 115 |
expectedErr: "param[3] is a api.Module, which may be defined only once as param[0]", |
| 116 |
}, |
| 117 |
} |
| 118 |
|
| 119 |
for _, tt := range tests { |
| 120 |
tc := tt |
| 121 |
|
| 122 |
t.Run(tc.name, func(t *testing.T) { |
| 123 |
_, _, _, err := parseGoReflectFunc(tc.input) |
| 124 |
require.EqualError(t, err, tc.expectedErr) |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func Test_callGoFunc(t *testing.T) { |
| 130 |
tPtr := uintptr(unsafe.Pointer(t)) |
| 131 |
inst := &ModuleInstance{} |
| 132 |
|
| 133 |
tests := []struct { |
| 134 |
name string |
| 135 |
input interface{} |
| 136 |
inputParams, expectedResults []uint64 |
| 137 |
}{ |
| 138 |
{ |
| 139 |
name: "() -> ()", |
| 140 |
input: func() {}, |
| 141 |
}, |
| 142 |
{ |
| 143 |
name: "(ctx) -> ()", |
| 144 |
input: func(ctx context.Context) { |
| 145 |
require.Equal(t, testCtx, ctx) |
| 146 |
}, |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "(ctx, mod) -> ()", |
| 150 |
input: func(ctx context.Context, m api.Module) { |
| 151 |
require.Equal(t, testCtx, ctx) |
| 152 |
require.Equal(t, inst, m) |
| 153 |
}, |
| 154 |
}, |
| 155 |
{ |
| 156 |
name: "all supported params and i32 result", |
| 157 |
input: func(v uintptr, w uint32, x uint64, y float32, z float64) uint32 { |
| 158 |
require.Equal(t, tPtr, v) |
| 159 |
require.Equal(t, uint32(math.MaxUint32), w) |
| 160 |
require.Equal(t, uint64(math.MaxUint64), x) |
| 161 |
require.Equal(t, float32(math.MaxFloat32), y) |
| 162 |
require.Equal(t, math.MaxFloat64, z) |
| 163 |
return 100 |
| 164 |
}, |
| 165 |
inputParams: []uint64{ |
| 166 |
api.EncodeExternref(tPtr), |
| 167 |
math.MaxUint32, |
| 168 |
math.MaxUint64, |
| 169 |
api.EncodeF32(math.MaxFloat32), |
| 170 |
api.EncodeF64(math.MaxFloat64), |
| 171 |
}, |
| 172 |
expectedResults: []uint64{100}, |
| 173 |
}, |
| 174 |
{ |
| 175 |
name: "all supported params and i32 result - (ctx)", |
| 176 |
input: func(ctx context.Context, v uintptr, w uint32, x uint64, y float32, z float64) uint32 { |
| 177 |
require.Equal(t, testCtx, ctx) |
| 178 |
require.Equal(t, tPtr, v) |
| 179 |
require.Equal(t, uint32(math.MaxUint32), w) |
| 180 |
require.Equal(t, uint64(math.MaxUint64), x) |
| 181 |
require.Equal(t, float32(math.MaxFloat32), y) |
| 182 |
require.Equal(t, math.MaxFloat64, z) |
| 183 |
return 100 |
| 184 |
}, |
| 185 |
inputParams: []uint64{ |
| 186 |
api.EncodeExternref(tPtr), |
| 187 |
math.MaxUint32, |
| 188 |
math.MaxUint64, |
| 189 |
api.EncodeF32(math.MaxFloat32), |
| 190 |
api.EncodeF64(math.MaxFloat64), |
| 191 |
}, |
| 192 |
expectedResults: []uint64{100}, |
| 193 |
}, |
| 194 |
{ |
| 195 |
name: "all supported params and i32 result - (ctx, mod)", |
| 196 |
input: func(ctx context.Context, m api.Module, v uintptr, w uint32, x uint64, y float32, z float64) uint32 { |
| 197 |
require.Equal(t, testCtx, ctx) |
| 198 |
require.Equal(t, inst, m) |
| 199 |
require.Equal(t, tPtr, v) |
| 200 |
require.Equal(t, uint32(math.MaxUint32), w) |
| 201 |
require.Equal(t, uint64(math.MaxUint64), x) |
| 202 |
require.Equal(t, float32(math.MaxFloat32), y) |
| 203 |
require.Equal(t, math.MaxFloat64, z) |
| 204 |
return 100 |
| 205 |
}, |
| 206 |
inputParams: []uint64{ |
| 207 |
api.EncodeExternref(tPtr), |
| 208 |
math.MaxUint32, |
| 209 |
math.MaxUint64, |
| 210 |
api.EncodeF32(math.MaxFloat32), |
| 211 |
api.EncodeF64(math.MaxFloat64), |
| 212 |
}, |
| 213 |
expectedResults: []uint64{100}, |
| 214 |
}, |
| 215 |
} |
| 216 |
for _, tt := range tests { |
| 217 |
tc := tt |
| 218 |
|
| 219 |
t.Run(tc.name, func(t *testing.T) { |
| 220 |
_, _, code, err := parseGoReflectFunc(tc.input) |
| 221 |
require.NoError(t, err) |
| 222 |
|
| 223 |
resultLen := len(tc.expectedResults) |
| 224 |
stackLen := len(tc.inputParams) |
| 225 |
if resultLen > stackLen { |
| 226 |
stackLen = resultLen |
| 227 |
} |
| 228 |
stack := make([]uint64, stackLen) |
| 229 |
copy(stack, tc.inputParams) |
| 230 |
|
| 231 |
switch code.GoFunc.(type) { |
| 232 |
case api.GoFunction: |
| 233 |
code.GoFunc.(api.GoFunction).Call(testCtx, stack) |
| 234 |
case api.GoModuleFunction: |
| 235 |
code.GoFunc.(api.GoModuleFunction).Call(testCtx, inst, stack) |
| 236 |
default: |
| 237 |
t.Fatal("unexpected type.") |
| 238 |
} |
| 239 |
|
| 240 |
var results []uint64 |
| 241 |
if resultLen > 0 { |
| 242 |
results = stack[:resultLen] |
| 243 |
} |
| 244 |
require.Equal(t, tc.expectedResults, results) |
| 245 |
}) |
| 246 |
} |
| 247 |
} |