-
Notifications
You must be signed in to change notification settings - Fork 39
/
call.go
executable file
·133 lines (110 loc) · 3.65 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2017~2022 The Bottos Authors
// This file is part of the Bottos Chain library.
// Created by Rocket Core Team of Bottos.
// This program is free software: you can distribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Bottos. If not, see <http://www.gnu.org/licenses/>.
// 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.
/*
* file description: call func
* @Author: Stewart Li
* @Date: 2017-12-07
* @Last Modified by:
* @Last Modified time:
*/
package exec
import (
"errors"
"fmt"
)
func (vm *VM) doCall(compiled compiledFunction, index int64) {
defer func() {
err := recover()
//fmt.Println("*EXCEPT !!! VM::doCall err: ",err.(string))
if err != nil {
fmt.Println("*EXCEPT !!! VM::doCall err: ",err.(string))
//log.Infof(err.(string))
return
}
}()
newStack := make([]uint64, compiled.maxDepth)
locals := make([]uint64, compiled.totalLocalVars)
for i := compiled.args - 1; i >= 0; i-- {
locals[i] = vm.popUint64()
}
// save execution context
prevCtxt := vm.ctx
vm.ctx = context{
stack: newStack,
locals: locals,
code: compiled.code,
pc: 0,
curFunc: index,
}
rtrn := vm.execCode(compiled)
// restore execution context
vm.ctx = prevCtxt
if compiled.returns {
vm.pushUint64(rtrn)
}
}
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() {
defer func() {
err := recover()
//fmt.Println("EXCEPT !!! ",err)
if err != nil {
fmt.Println("*EXCEPT !!! VM::call err: ",err.(string))
//log.Infof(err.(string))
return
}
}()
index := vm.fetchUint32()
vm.doCall(vm.compiledFuncs[index], 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.doCall(vm.compiledFuncs[elemIndex], int64(elemIndex))
}