forked from nknorg/nkn
/
func_flowcontrol.go
74 lines (62 loc) · 1.44 KB
/
func_flowcontrol.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
package vm
import (
"github.com/nknorg/nkn/util/log"
. "github.com/nknorg/nkn/vm/errors"
)
func opNop(e *ExecutionEngine) (VMState, error) {
return NONE, nil
}
func opJmp(e *ExecutionEngine) (VMState, error) {
offset := int(e.context.OpReader.ReadInt16())
offset = e.context.GetInstructionPointer() + offset - 3
if offset > len(e.context.Code) {
return FAULT, ErrFault
}
var (
fValue = true
)
if e.opCode > JMP {
fValue = PopBoolean(e)
if e.opCode == JMPIFNOT {
fValue = !fValue
}
}
if fValue {
e.context.SetInstructionPointer(int64(offset))
}
return NONE, nil
}
func opCall(e *ExecutionEngine) (VMState, error) {
e.invocationStack.Push(e.context.Clone())
e.context.SetInstructionPointer(int64(e.context.GetInstructionPointer() + 2))
e.opCode = JMP
e.context = e.CurrentContext()
opJmp(e)
return NONE, nil
}
func opRet(e *ExecutionEngine) (VMState, error) {
e.invocationStack.Pop()
return NONE, nil
}
func opAppCall(e *ExecutionEngine) (VMState, error) {
codeHash := e.context.OpReader.ReadBytes(20)
code, err := e.table.GetCode(codeHash)
if code == nil {
return FAULT, err
}
if e.opCode == TAILCALL {
e.invocationStack.Pop()
}
e.LoadCode(code, false)
return NONE, nil
}
func opSysCall(e *ExecutionEngine) (VMState, error) {
s := e.context.OpReader.ReadVarString()
log.Error("[opSysCall] service name:", s)
success, err := e.service.Invoke(s, e)
if success {
return NONE, nil
} else {
return FAULT, err
}
}