/
helpers.go
64 lines (53 loc) · 1.49 KB
/
helpers.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
package wasmgo
import (
"errors"
"github.com/eosspark/eos-go/wasmgo/utils"
)
var _ ImportResolver = (*NopResolver)(nil)
// NopResolver is a nil WebAssembly module import resolver.
type NopResolver struct{}
func (r *NopResolver) ResolveFunc(module, field string) FunctionImport {
panic("func import not allowed")
}
func (r *NopResolver) ResolveGlobal(module, field string) int64 {
panic("global import not allowed")
}
// RunWithGasLimit runs a WebAssembly modules function denoted by its ID with a specified set
// of parameters for a specified amount of instructions (also known as gas) denoted by `limit`.
// Panics on logical errors.
func (vm *VirtualMachine) RunWithGasLimit(entryID, limit int, params ...int64) (int64, error) {
count := 0
vm.Ignite(entryID, params...)
for !vm.Exited {
vm.Execute()
if vm.Delegate != nil {
vm.Delegate()
vm.Delegate = nil
}
count++
if count == limit {
return -1, errors.New("gas limit exceeded")
}
}
if vm.ExitError != nil {
return -1, utils.UnifyError(vm.ExitError)
}
return vm.ReturnValue, nil
}
// Run runs a WebAssembly modules function denoted by its ID with a specified set
// of parameters.
// Panics on logical errors.
func (vm *VirtualMachine) Run(entryID int, params ...int64) (int64, error) {
vm.Ignite(entryID, params...)
for !vm.Exited {
vm.Execute()
if vm.Delegate != nil {
vm.Delegate()
vm.Delegate = nil
}
}
if vm.ExitError != nil {
return -1, utils.UnifyError(vm.ExitError)
}
return vm.ReturnValue, nil
}