From 181569c2a18fcbb2c099d6a49d0fdfa4b54e840c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 19 Aug 2020 10:10:40 +0300 Subject: [PATCH] compiler: allow to alias interop packages Fix #397. --- pkg/compiler/analysis.go | 2 +- pkg/compiler/codegen.go | 2 +- pkg/compiler/interop_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index aad9ea6a7..71870e98c 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -226,7 +226,7 @@ func isSyscall(fun *funcScope) bool { if fun.selector == nil || fun.pkg == nil || !isInteropPath(fun.pkg.Path()) { return false } - _, ok := syscalls[fun.selector.Name][fun.name] + _, ok := syscalls[fun.pkg.Name()][fun.name] return ok } diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index cd21fd72c..47afc481a 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -873,7 +873,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { emit.Opcode(c.prog.BinWriter, opcode.CALLA) } case isSyscall(f): - c.convertSyscall(n, f.selector.Name, f.name) + c.convertSyscall(n, f.pkg.Name(), f.name) default: emit.Call(c.prog.BinWriter, opcode.CALLL, f.label) } diff --git a/pkg/compiler/interop_test.go b/pkg/compiler/interop_test.go index f0cdc2fac..073c1c2ec 100644 --- a/pkg/compiler/interop_test.go +++ b/pkg/compiler/interop_test.go @@ -60,6 +60,19 @@ func TestFromAddress(t *testing.T) { eval(t, src, append(addr1.BytesBE(), addr2.BytesBE()...)) }) + + t.Run("AliasPackage", func(t *testing.T) { + src := ` + package foo + import uu "github.com/nspcc-dev/neo-go/pkg/interop/util" + func Main() []byte { + addr1 := uu.FromAddress("` + as1 + `") + addr2 := uu.FromAddress("` + as2 + `") + sum := append(addr1, addr2...) + return sum + }` + eval(t, src, append(addr1.BytesBE(), addr2.BytesBE()...)) + }) } func spawnVM(t *testing.T, ic *interop.Context, src string) *vm.VM { @@ -167,6 +180,19 @@ func TestAppCall(t *testing.T) { assertResult(t, v, big.NewInt(42)) }) + + t.Run("AliasPackage", func(t *testing.T) { + src := `package foo + import ee "github.com/nspcc-dev/neo-go/pkg/interop/engine" + func Main() int { + var addr = []byte(` + fmt.Sprintf("%#v", string(ih.BytesBE())) + `) + result := ee.AppCall(addr, "add3", 39) + return result.(int) + }` + v := spawnVM(t, ic, src) + require.NoError(t, v.Run()) + assertResult(t, v, big.NewInt(42)) + }) } func getAppCallScript(h string) string {