compiler: allow to alias interop packages

Fix #397.
This commit is contained in:
Evgenii Stratonikov 2020-08-19 10:10:40 +03:00
parent 36ce23789a
commit 181569c2a1
3 changed files with 28 additions and 2 deletions

View file

@ -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
}

View file

@ -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)
}

View file

@ -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 {