2020-07-27 08:06:06 +00:00
|
|
|
package callback
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-07-27 09:23:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-07-27 08:06:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Callback is an interface for arbitrary callbacks.
|
|
|
|
type Callback interface {
|
|
|
|
// ArgCount returns expected number of arguments.
|
|
|
|
ArgCount() int
|
|
|
|
// LoadContext loads context and arguments on stack.
|
|
|
|
LoadContext(*vm.VM, []stackitem.Item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke invokes provided callback.
|
2020-07-27 09:23:48 +00:00
|
|
|
func Invoke(ic *interop.Context, v *vm.VM) error {
|
2020-07-27 08:06:06 +00:00
|
|
|
cb := v.Estack().Pop().Interop().Value().(Callback)
|
|
|
|
args := v.Estack().Pop().Array()
|
|
|
|
if cb.ArgCount() != len(args) {
|
|
|
|
return errors.New("invalid argument count")
|
|
|
|
}
|
|
|
|
cb.LoadContext(v, args)
|
2020-07-27 09:23:48 +00:00
|
|
|
switch cb.(type) {
|
|
|
|
case *MethodCallback:
|
|
|
|
id := emit.InteropNameToID([]byte("System.Contract.Call"))
|
|
|
|
return ic.SyscallHandler(v, id)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-27 08:06:06 +00:00
|
|
|
}
|