From 02ce59cfd51f06e0119291e87bfdf55f5e7c9532 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 27 Oct 2022 22:35:06 +0300 Subject: [PATCH] binding: avoid name conflicts with Go keywords And clashing one name on another after rename. --- cli/smartcontract/generate_test.go | 14 ++++++++++++++ cli/smartcontract/testdata/nameservice/nns.go | 8 ++++---- pkg/smartcontract/binding/generate.go | 11 ++++++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cli/smartcontract/generate_test.go b/cli/smartcontract/generate_test.go index 9eada4790..ef753fc60 100644 --- a/cli/smartcontract/generate_test.go +++ b/cli/smartcontract/generate_test.go @@ -45,6 +45,15 @@ func TestGenerate(t *testing.T) { ReturnType: smartcontract.IntegerType, Safe: true, }, + manifest.Method{ + Name: "zum", + Parameters: []manifest.Parameter{ + manifest.NewParameter("type", smartcontract.IntegerType), + manifest.NewParameter("typev", smartcontract.IntegerType), + manifest.NewParameter("func", smartcontract.IntegerType), + }, + ReturnType: smartcontract.IntegerType, + }, manifest.Method{ Name: "justExecute", Parameters: []manifest.Parameter{ @@ -172,6 +181,11 @@ func Sum3() int { return neogointernal.CallWithToken(Hash, "sum3", int(contract.ReadOnly)).(int) } +// Zum invokes ` + "`zum`" + ` method of contract. +func Zum(typev int, typev_ int, funcv int) int { + return neogointernal.CallWithToken(Hash, "zum", int(contract.All), typev, typev_, funcv).(int) +} + // JustExecute invokes ` + "`justExecute`" + ` method of contract. func JustExecute(arr []interface{}) { neogointernal.CallWithTokenNoRet(Hash, "justExecute", int(contract.All), arr) diff --git a/cli/smartcontract/testdata/nameservice/nns.go b/cli/smartcontract/testdata/nameservice/nns.go index 2d9259616..62c59082b 100644 --- a/cli/smartcontract/testdata/nameservice/nns.go +++ b/cli/smartcontract/testdata/nameservice/nns.go @@ -47,8 +47,8 @@ func (c *ContractReader) IsAvailable(name string) (bool, error) { } // GetRecord invokes `getRecord` method of contract. -func (c *ContractReader) GetRecord(name string, type *big.Int) (string, error) { - return unwrap.UTF8String(c.invoker.Call(Hash, "getRecord", name, type)) +func (c *ContractReader) GetRecord(name string, typev *big.Int) (string, error) { + return unwrap.UTF8String(c.invoker.Call(Hash, "getRecord", name, typev)) } // GetAllRecords invokes `getAllRecords` method of contract. @@ -57,6 +57,6 @@ func (c *ContractReader) GetAllRecords(name string) (stackitem.Item, error) { } // Resolve invokes `resolve` method of contract. -func (c *ContractReader) Resolve(name string, type *big.Int) (string, error) { - return unwrap.UTF8String(c.invoker.Call(Hash, "resolve", name, type)) +func (c *ContractReader) Resolve(name string, typev *big.Int) (string, error) { + return unwrap.UTF8String(c.invoker.Call(Hash, "resolve", name, typev)) } diff --git a/pkg/smartcontract/binding/generate.go b/pkg/smartcontract/binding/generate.go index f9dfb2986..b3a735917 100644 --- a/pkg/smartcontract/binding/generate.go +++ b/pkg/smartcontract/binding/generate.go @@ -3,6 +3,7 @@ package binding import ( "bytes" "fmt" + "go/token" "io" "sort" "strconv" @@ -201,6 +202,8 @@ func TemplateFromManifest(cfg Config, scTypeConverter func(string, smartcontract } else if m.Safe { mtd.CallFlag = callflag.ReadOnly.String() } + + var varnames = make(map[string]bool) for i := range m.Parameters { name := m.Parameters[i].Name if name == "" { @@ -211,7 +214,13 @@ func TemplateFromManifest(cfg Config, scTypeConverter func(string, smartcontract if pkg != "" { imports[pkg] = struct{}{} } - + if token.IsKeyword(name) { + name = name + "v" + } + for varnames[name] { + name = name + "_" + } + varnames[name] = true mtd.Arguments = append(mtd.Arguments, ParamTmpl{ Name: name, Type: typeStr,