mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-27 03:58:06 +00:00
37 lines
1,012 B
Go
37 lines
1,012 B
Go
package callback
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
"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.
|
|
func Invoke(ic *interop.Context) error {
|
|
cb := ic.VM.Estack().Pop().Interop().Value().(Callback)
|
|
args := ic.VM.Estack().Pop().Array()
|
|
if cb.ArgCount() != len(args) {
|
|
return errors.New("invalid argument count")
|
|
}
|
|
cb.LoadContext(ic.VM, args)
|
|
switch t := cb.(type) {
|
|
case *MethodCallback:
|
|
id := interopnames.ToID([]byte(interopnames.SystemContractCall))
|
|
return ic.SyscallHandler(ic.VM, id)
|
|
case *SyscallCallback:
|
|
return ic.SyscallHandler(ic.VM, t.desc.ID)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|