This commit is contained in:
mathetake
2020-05-11 21:53:52 +09:00
parent b513ba4a44
commit 07ad79d1e3
5 changed files with 36 additions and 16 deletions

View File

@@ -27,7 +27,7 @@ func Test_fibonacci(t *testing.T) {
{in: 10, exp: 55}, {in: 10, exp: 55},
{in: 5, exp: 5}, {in: 5, exp: 5},
} { } {
ret, retTypes, err := vm.ExecExportedFunction("fib", uint64(c.in)) ret, retTypes, err := vm.ExecExportedFunction("fibonacci", uint64(c.in))
require.NoError(t, err) require.NoError(t, err)
require.Len(t, ret, len(retTypes)) require.Len(t, ret, len(retTypes))
require.Equal(t, wasm.ValueTypeI32, retTypes[0]) require.Equal(t, wasm.ValueTypeI32, retTypes[0])

View File

@@ -3,10 +3,10 @@ package main
func main() {} func main() {}
//export fib //export fibonacci
func fib(in uint32) uint32 { func fibonacci(in uint32) uint32 {
if in <= 1 { if in <= 1 {
return in return in
} }
return fib(in-1) + fib(in-2) return fibonacci(in-1) + fibonacci(in-2)
} }

Binary file not shown.

View File

@@ -141,6 +141,7 @@ func memoryGrow(vm *VirtualMachine) {
uint64(n+uint32(len(vm.Memory)/vmPageSize)) > uint64(*(vm.InnerModule.SecMemory[0].Max)) { uint64(n+uint32(len(vm.Memory)/vmPageSize)) > uint64(*(vm.InnerModule.SecMemory[0].Max)) {
v := int32(-1) v := int32(-1)
vm.OperandStack.Push(uint64(v)) vm.OperandStack.Push(uint64(v))
return
} }
vm.OperandStack.Push(uint64(len(vm.Memory)) / vmPageSize) vm.OperandStack.Push(uint64(len(vm.Memory)) / vmPageSize)

View File

@@ -404,17 +404,36 @@ func Test_memorySize(t *testing.T) {
} }
func Test_memoryGrow(t *testing.T) { func Test_memoryGrow(t *testing.T) {
vm := &VirtualMachine{ t.Run("ok", func(t *testing.T) {
ActiveContext: &NativeFunctionContext{}, vm := &VirtualMachine{
Memory: make([]byte, vmPageSize*2), ActiveContext: &NativeFunctionContext{},
OperandStack: NewVirtualMachineOperandStack(), Memory: make([]byte, vmPageSize*2),
InnerModule: &Module{ OperandStack: NewVirtualMachineOperandStack(),
SecMemory: []*MemoryType{{}}, InnerModule: &Module{
}, SecMemory: []*MemoryType{{}},
} },
}
vm.OperandStack.Push(5)
memoryGrow(vm)
assert.Equal(t, uint64(0x2), vm.OperandStack.Pop())
assert.Equal(t, 7, len(vm.Memory)/vmPageSize)
})
t.Run("oom", func(t *testing.T) {
vm := &VirtualMachine{
ActiveContext: &NativeFunctionContext{},
Memory: make([]byte, vmPageSize*2),
OperandStack: NewVirtualMachineOperandStack(),
InnerModule: &Module{
SecMemory: []*MemoryType{{Max: uint32Ptr(0)}},
},
}
exp := int32(-1)
vm.OperandStack.Push(5)
memoryGrow(vm)
assert.Equal(t, uint64(exp), vm.OperandStack.Pop())
})
vm.OperandStack.Push(5)
memoryGrow(vm)
assert.Equal(t, uint64(0x2), vm.OperandStack.Pop())
assert.Equal(t, 7, len(vm.Memory)/vmPageSize)
} }