diff --git a/hostfunc/hostfunc.go b/hostfunc/hostfunc.go index be3391d2..2b670f00 100644 --- a/hostfunc/hostfunc.go +++ b/hostfunc/hostfunc.go @@ -19,6 +19,10 @@ func NewModuleBuilderWith(in map[string]*wasm.Module) *ModuleBuilder { return &ModuleBuilder{modules: in} } +func (m *ModuleBuilder) Done() map[string]*wasm.Module { + return m.modules +} + func (m *ModuleBuilder) MustSetFunction(modName, funcName string, fn func(machine *wasm.VirtualMachine) reflect.Value) { if err := m.SetFunction(modName, funcName, fn); err != nil { panic(err) @@ -34,6 +38,7 @@ func (m *ModuleBuilder) SetFunction(modName, funcName string, fn func(machine *w } mod.SecExports[funcName] = &wasm.ExportSegment{ + Name: funcName, Desc: &wasm.ExportDesc{ Kind: wasm.ExportKindFunction, Index: uint32(len(mod.IndexSpace.Function)), @@ -81,12 +86,8 @@ func getTypeOf(kind reflect.Kind) (wasm.ValueType, error) { case reflect.Int32, reflect.Uint32: return wasm.ValueTypeI32, nil case reflect.Int64, reflect.Uint64: - return wasm.ValueTypeI32, nil + return wasm.ValueTypeI64, nil default: return 0x00, fmt.Errorf("invalid type: %s", kind.String()) } } - -func (m *ModuleBuilder) Done() map[string]*wasm.Module { - return m.modules -} diff --git a/hostfunc/hostfunc_test.go b/hostfunc/hostfunc_test.go index 86375560..6ec8bc49 100644 --- a/hostfunc/hostfunc_test.go +++ b/hostfunc/hostfunc_test.go @@ -1,3 +1,87 @@ package hostfunc -// fixme: +import ( + "reflect" + "testing" + + "github.com/mathetake/gasm/wasm" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestModuleBuilder_SetFunction(t *testing.T) { + t.Run("register", func(t *testing.T) { + builder := NewModuleBuilder() + cases := []struct { + modName, funcName string + expIndex uint32 + }{ + {modName: "env", funcName: "foo", expIndex: 0}, + {modName: "env", funcName: "bar", expIndex: 1}, + {modName: "golang", funcName: "hello", expIndex: 0}, + {modName: "env", funcName: "great", expIndex: 2}, + } + + for _, c := range cases { + builder.MustSetFunction(c.modName, c.funcName, func(machine *wasm.VirtualMachine) reflect.Value { + return reflect.ValueOf(func() {}) + }) + } + + ms := builder.Done() + for _, c := range cases { + mod, ok := ms[c.modName] + require.True(t, ok) + e, ok := mod.SecExports[c.funcName] + require.True(t, ok) + require.Equal(t, c.funcName, e.Name) + require.Equal(t, wasm.ExportKindFunction, e.Desc.Kind) + require.Equal(t, c.expIndex, e.Desc.Index) + } + }) + + t.Run("type", func(t *testing.T) { + builder := NewModuleBuilder() + builder.MustSetFunction("a", "b", func(machine *wasm.VirtualMachine) reflect.Value { + return reflect.ValueOf(func(int32, int64) (float32, float64, int32) { + return 0, 0, 0 + }) + }) + ms := builder.Done() + mod, ok := ms["a"] + require.True(t, ok) + actualType := mod.IndexSpace.Function[0].FunctionType() + require.Equal(t, &wasm.FunctionType{ + InputTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64}, + ReturnTypes: []wasm.ValueType{wasm.ValueTypeF32, wasm.ValueTypeF64, wasm.ValueTypeI32}, + }, actualType) + }) +} + +func Test_getSignature(t *testing.T) { + v := reflect.ValueOf(func(int32, int64, float32, float64) (int32, float64) { return 0, 0 }) + actual, err := getSignature(v.Type()) + require.NoError(t, err) + require.Equal(t, &wasm.FunctionType{ + InputTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeF32, wasm.ValueTypeF64}, + ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeF64}, + }, actual) +} + +func Test_getTypeOf(t *testing.T) { + for _, c := range []struct { + kind reflect.Kind + exp wasm.ValueType + }{ + {kind: reflect.Int32, exp: wasm.ValueTypeI32}, + {kind: reflect.Uint32, exp: wasm.ValueTypeI32}, + {kind: reflect.Int64, exp: wasm.ValueTypeI64}, + {kind: reflect.Uint64, exp: wasm.ValueTypeI64}, + {kind: reflect.Float32, exp: wasm.ValueTypeF32}, + {kind: reflect.Float64, exp: wasm.ValueTypeF64}, + } { + actual, err := getTypeOf(c.kind) + require.NoError(t, err) + assert.Equal(t, c.exp, actual) + } +} diff --git a/wasm/segment.go b/wasm/segment.go index b0ea4456..b73a77bc 100644 --- a/wasm/segment.go +++ b/wasm/segment.go @@ -118,10 +118,10 @@ type ExportDesc struct { } const ( - ExportKindFunction = 0x00 - ExportKindTable = 0x01 - ExportKindMem = 0x02 - ExportKindGlobal = 0x03 + ExportKindFunction byte = 0x00 + ExportKindTable byte = 0x01 + ExportKindMem byte = 0x02 + ExportKindGlobal byte = 0x03 ) func readExportDesc(r io.Reader) (*ExportDesc, error) {