mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 23:33:37 +00:00
vm: allow to call VM methods from outside
Abstract out (*VM).Call method and use in in CALL* opcodes.
This commit is contained in:
parent
6ecd1ae437
commit
13b9eda08d
1 changed files with 14 additions and 12 deletions
26
pkg/vm/vm.go
26
pkg/vm/vm.go
|
@ -1235,13 +1235,9 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
|
|||
|
||||
case opcode.CALL, opcode.CALLL:
|
||||
v.checkInvocationStackSize()
|
||||
newCtx := ctx.Copy()
|
||||
newCtx.local = nil
|
||||
newCtx.arguments = nil
|
||||
v.istack.PushVal(newCtx)
|
||||
|
||||
offset := v.getJumpOffset(newCtx, parameter)
|
||||
v.Jump(newCtx, offset)
|
||||
// Note: jump offset must be calculated regarding to new context,
|
||||
// but it is cloned and thus has the same script and instruction pointer.
|
||||
v.Call(ctx, v.getJumpOffset(ctx, parameter))
|
||||
|
||||
case opcode.CALLA:
|
||||
ptr := v.estack.Pop().Item().(*stackitem.Pointer)
|
||||
|
@ -1249,11 +1245,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
|
|||
panic("invalid script in pointer")
|
||||
}
|
||||
|
||||
newCtx := ctx.Copy()
|
||||
newCtx.local = nil
|
||||
newCtx.arguments = nil
|
||||
v.istack.PushVal(newCtx)
|
||||
v.Jump(newCtx, ptr.Position())
|
||||
v.Call(ctx, ptr.Position())
|
||||
|
||||
case opcode.SYSCALL:
|
||||
interopID := GetInteropID(parameter)
|
||||
|
@ -1471,6 +1463,16 @@ func (v *VM) Jump(ctx *Context, offset int) {
|
|||
ctx.nextip = offset
|
||||
}
|
||||
|
||||
// Call calls method by offset. It is similar to Jump but also
|
||||
// pushes new context to the invocation state
|
||||
func (v *VM) Call(ctx *Context, offset int) {
|
||||
newCtx := ctx.Copy()
|
||||
newCtx.local = nil
|
||||
newCtx.arguments = nil
|
||||
v.istack.PushVal(newCtx)
|
||||
v.Jump(newCtx, offset)
|
||||
}
|
||||
|
||||
// getJumpOffset returns instruction number in a current context
|
||||
// to a which JMP should be performed.
|
||||
// parameter should have length either 1 or 4 and
|
||||
|
|
Loading…
Reference in a new issue