unittest: vm.go

This commit is contained in:
mathetake
2020-05-09 18:06:17 +09:00
parent f768496bb4
commit 779f8b5365
4 changed files with 103 additions and 11 deletions

4
examples/README.md Normal file
View File

@@ -0,0 +1,4 @@
This is where we put e2e tests of virtual machine. Please place examples so that `examples/foo_test.go` is testing
`examples/wasm/foo.wasm` binary which is generated by compiling
`examples/wasm/foo.go` with latest version of TinyGo.

View File

@@ -8,10 +8,6 @@ import (
"github.com/mathetake/gasm/wasm/leb128"
)
var (
ErrInvalidSectionID = errors.New("invalid section id")
)
type SectionID byte
const (
@@ -31,7 +27,7 @@ const (
func (m *Module) readSections(r io.Reader) error {
for {
if err := m.readSection(r); err == io.EOF {
if err := m.readSection(r); errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
@@ -41,9 +37,7 @@ func (m *Module) readSections(r io.Reader) error {
func (m *Module) readSection(r io.Reader) error {
b := make([]byte, 1)
if _, err := io.ReadFull(r, b); err == io.EOF {
return err
} else if err != nil {
if _, err := io.ReadFull(r, b); err != nil {
return fmt.Errorf("read section id: %w", err)
}
@@ -80,7 +74,7 @@ func (m *Module) readSection(r io.Reader) error {
case SectionIDData:
err = m.readSectionData(r)
default:
err = ErrInvalidSectionID
err = errors.New("invalid section id")
}
if err != nil {

View File

@@ -97,11 +97,15 @@ func (vm *VirtualMachine) ExecExportedFunction(name string, args ...uint64) (ret
return nil, nil, fmt.Errorf("function index out of range")
}
f := vm.Functions[exp.Desc.Index]
if len(f.FunctionType().InputTypes) != len(args) {
return nil, nil, fmt.Errorf("invalid number of arguments")
}
for _, arg := range args {
vm.OperandStack.Push(arg)
}
f := vm.Functions[exp.Desc.Index]
f.Call(vm)
ret := make([]uint64, len(f.FunctionType().ReturnTypes))

View File

@@ -1,3 +1,93 @@
package wasm
// fixme:
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestVirtualMachine_ExecExportedFunction(t *testing.T) {
vm := &VirtualMachine{
InnerModule: &Module{
SecExports: map[string]*ExportSegment{
"a": {Desc: &ExportDesc{Index: 0, Kind: ExportKindFunction}},
"b": {Desc: &ExportDesc{Index: 0, Kind: ExportKindGlobal}},
"c": {Desc: &ExportDesc{Index: 100, Kind: ExportKindFunction}},
},
},
Functions: []VirtualMachineFunction{&HostFunction{
function: reflect.ValueOf(func(in int64) int64 {
return in * 2
}),
Signature: &FunctionType{
InputTypes: []ValueType{ValueTypeI64},
ReturnTypes: []ValueType{ValueTypeI64},
},
}},
OperandStack: NewVirtualMachineOperandStack(),
}
ret, retTypes, err := vm.ExecExportedFunction("a", 1)
require.NoError(t, err)
require.Len(t, retTypes, 1)
require.Len(t, ret, 1)
require.Equal(t, retTypes[0], ValueTypeI64)
require.Equal(t, int64(2), int64(ret[0]))
_, _, err = vm.ExecExportedFunction("a")
require.Error(t, err)
_, _, err = vm.ExecExportedFunction("b")
require.Error(t, err)
_, _, err = vm.ExecExportedFunction("c")
require.Error(t, err)
}
func TestVirtualMachine_FetchInt32(t *testing.T) {
vm := &VirtualMachine{
ActiveContext: &NativeFunctionContext{
PC: 1,
Function: &NativeFunction{Body: []byte{0x00, 0xFF, 0x00}},
},
}
actual := vm.FetchInt32()
require.Equal(t, int32(127), actual)
require.Equal(t, uint64(2), vm.ActiveContext.PC)
}
func TestVirtualMachine_FetchInt64(t *testing.T) {
vm := &VirtualMachine{
ActiveContext: &NativeFunctionContext{
PC: 1,
Function: &NativeFunction{Body: []byte{0x00, 0xFF, 0x00}},
},
}
actual := vm.FetchInt64()
require.Equal(t, int64(127), actual)
require.Equal(t, uint64(2), vm.ActiveContext.PC)
}
func TestVirtualMachine_FetchFloat32(t *testing.T) {
vm := &VirtualMachine{
ActiveContext: &NativeFunctionContext{
PC: 2,
Function: &NativeFunction{Body: []byte{0x00, 0x00, 0x40, 0xe1, 0x47, 0x40}},
},
}
actual := vm.FetchFloat32()
require.Equal(t, float32(3.1231232), actual)
require.Equal(t, uint64(5), vm.ActiveContext.PC)
}
func TestVirtualMachine_FetchFloat64(t *testing.T) {
vm := &VirtualMachine{
ActiveContext: &NativeFunctionContext{
Function: &NativeFunction{Body: []byte{
0x5e, 0xc4, 0xd8, 0xf9, 0x27, 0xfc, 0x08, 0x40,
}},
},
}
actual := vm.FetchFloat64()
require.Equal(t, 3.1231231231, actual)
require.Equal(t, uint64(7), vm.ActiveContext.PC)
}