forked from go-interpreter/wagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.go
57 lines (47 loc) · 1.8 KB
/
call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2017 The go-interpreter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package exec
import "errors"
var (
// ErrSignatureMismatch is the error value used while trapping the VM when
// a signature mismatch between the table entry and the type entry is found
// in a call_indirect operation.
ErrSignatureMismatch = errors.New("exec: signature mismatch in call_indirect")
// ErrUndefinedElementIndex is the error value used while trapping the VM when
// an invalid index to the module's table space is used as an operand to
// call_indirect
ErrUndefinedElementIndex = errors.New("exec: undefined element index")
)
func (vm *VM) call() {
index := vm.fetchUint32()
vm.funcs[index].call(vm, int64(index))
}
func (vm *VM) callIndirect() {
index := vm.fetchUint32()
fnExpect := vm.module.Types.Entries[index]
_ = vm.fetchUint32() // reserved (https://github.com/WebAssembly/design/blob/27ac254c854994103c24834a994be16f74f54186/BinaryEncoding.md#call-operators-described-here)
tableIndex := vm.popUint32()
if int(tableIndex) >= len(vm.module.TableIndexSpace[0]) {
panic(ErrUndefinedElementIndex)
}
elemIndex := vm.module.TableIndexSpace[0][tableIndex]
fnActual := vm.module.FunctionIndexSpace[elemIndex]
if len(fnExpect.ParamTypes) != len(fnActual.Sig.ParamTypes) {
panic(ErrSignatureMismatch)
}
if len(fnExpect.ReturnTypes) != len(fnActual.Sig.ReturnTypes) {
panic(ErrSignatureMismatch)
}
for i := range fnExpect.ParamTypes {
if fnExpect.ParamTypes[i] != fnActual.Sig.ParamTypes[i] {
panic(ErrSignatureMismatch)
}
}
for i := range fnExpect.ReturnTypes {
if fnExpect.ReturnTypes[i] != fnActual.Sig.ReturnTypes[i] {
panic(ErrSignatureMismatch)
}
}
vm.funcs[elemIndex].call(vm, int64(elemIndex))
}