compiler: make Notify accept varargs
This commit is contained in:
parent
c0b5271386
commit
efad66aee1
3 changed files with 46 additions and 3 deletions
|
@ -662,7 +662,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
|||
// We can be sure builtins are of type *ast.Ident.
|
||||
c.convertBuiltin(n)
|
||||
case isSyscall(f):
|
||||
c.convertSyscall(f.selector.Name, f.name)
|
||||
c.convertSyscall(n, f.selector.Name, f.name)
|
||||
default:
|
||||
emit.Call(c.prog.BinWriter, opcode.CALL, f.label)
|
||||
}
|
||||
|
@ -978,12 +978,18 @@ func (c *codegen) getByteArray(expr ast.Expr) []byte {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *codegen) convertSyscall(api, name string) {
|
||||
func (c *codegen) convertSyscall(expr *ast.CallExpr, api, name string) {
|
||||
api, ok := syscalls[api][name]
|
||||
if !ok {
|
||||
c.prog.Err = fmt.Errorf("unknown VM syscall api: %s", name)
|
||||
return
|
||||
}
|
||||
switch name {
|
||||
case "Notify":
|
||||
numArgs := len(expr.Args)
|
||||
emit.Int(c.prog.BinWriter, int64(numArgs))
|
||||
emit.Opcode(c.prog.BinWriter, opcode.PACK)
|
||||
}
|
||||
emit.Syscall(c.prog.BinWriter, api)
|
||||
|
||||
// This NOP instruction is basically not needed, but if we do, we have a
|
||||
|
|
|
@ -2,6 +2,10 @@ package compiler_test
|
|||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestStoragePutGet(t *testing.T) {
|
||||
|
@ -20,3 +24,24 @@ func TestStoragePutGet(t *testing.T) {
|
|||
`
|
||||
eval(t, src, []byte("foo"))
|
||||
}
|
||||
|
||||
func TestNotify(t *testing.T) {
|
||||
src := `package foo
|
||||
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||
func Main(arg int) {
|
||||
runtime.Notify(arg, "sum", arg+1)
|
||||
runtime.Notify()
|
||||
runtime.Notify("single")
|
||||
}`
|
||||
|
||||
v, s := vmAndCompileInterop(t, src)
|
||||
v.Estack().PushVal(11)
|
||||
|
||||
require.NoError(t, v.Run())
|
||||
require.Equal(t, 3, len(s.events))
|
||||
|
||||
exp0 := []vm.StackItem{vm.NewBigIntegerItem(11), vm.NewByteArrayItem([]byte("sum")), vm.NewBigIntegerItem(12)}
|
||||
assert.Equal(t, exp0, s.events[0].Value())
|
||||
assert.Equal(t, []vm.StackItem{}, s.events[1].Value())
|
||||
assert.Equal(t, []vm.StackItem{vm.NewByteArrayItem([]byte("single"))}, s.events[2].Value())
|
||||
}
|
||||
|
|
|
@ -53,6 +53,11 @@ func assertResult(t *testing.T, vm *vm.VM, result interface{}) {
|
|||
}
|
||||
|
||||
func vmAndCompile(t *testing.T, src string) *vm.VM {
|
||||
v, _ := vmAndCompileInterop(t, src)
|
||||
return v
|
||||
}
|
||||
|
||||
func vmAndCompileInterop(t *testing.T, src string) (*vm.VM, *storagePlugin) {
|
||||
vm := vm.New()
|
||||
|
||||
storePlugin := newStoragePlugin()
|
||||
|
@ -61,12 +66,13 @@ func vmAndCompile(t *testing.T, src string) *vm.VM {
|
|||
b, err := compiler.Compile(strings.NewReader(src))
|
||||
require.NoError(t, err)
|
||||
vm.Load(b)
|
||||
return vm
|
||||
return vm, storePlugin
|
||||
}
|
||||
|
||||
type storagePlugin struct {
|
||||
mem map[string][]byte
|
||||
interops map[uint32]vm.InteropFunc
|
||||
events []vm.StackItem
|
||||
}
|
||||
|
||||
func newStoragePlugin() *storagePlugin {
|
||||
|
@ -77,6 +83,7 @@ func newStoragePlugin() *storagePlugin {
|
|||
s.interops[vm.InteropNameToID([]byte("Neo.Storage.Get"))] = s.Get
|
||||
s.interops[vm.InteropNameToID([]byte("Neo.Storage.Put"))] = s.Put
|
||||
s.interops[vm.InteropNameToID([]byte("Neo.Storage.GetContext"))] = s.GetContext
|
||||
s.interops[vm.InteropNameToID([]byte("Neo.Runtime.Notify"))] = s.Notify
|
||||
return s
|
||||
|
||||
}
|
||||
|
@ -89,6 +96,11 @@ func (s *storagePlugin) getInterop(id uint32) *vm.InteropFuncPrice {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *storagePlugin) Notify(v *vm.VM) error {
|
||||
s.events = append(s.events, v.Estack().Pop().Item())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *storagePlugin) Delete(vm *vm.VM) error {
|
||||
vm.Estack().Pop()
|
||||
key := vm.Estack().Pop().Bytes()
|
||||
|
|
Loading…
Reference in a new issue