From 334ab967b2321045a3d9042a444c68b495e14582 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Mon, 1 Apr 2019 15:09:58 +0800 Subject: [PATCH] mobile: support integer slice --- accounts/abi/bind/bind.go | 4 ++ mobile/interface.go | 140 ++++++++++++++++++++++++++++++++++++-- mobile/interface_test.go | 8 +++ 3 files changed, 146 insertions(+), 6 deletions(-) diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go index 8de27abefe..e8504a8aed 100644 --- a/accounts/abi/bind/bind.go +++ b/accounts/abi/bind/bind.go @@ -189,6 +189,9 @@ func bindBasicTypeGo(kind abi.Type) string { } } +// bindTypeGo converts solidity types to Go ones. Since there is no clear mapping +// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly +// mapped will use an upscaled type (e.g. BigDecimal). func bindTypeGo(kind abi.Type) string { // todo(rjl493456442) tuple switch kind.T { @@ -251,6 +254,7 @@ func bindBasicTypeJava(kind abi.Type) string { func bindTypeJava(kind abi.Type) string { switch kind.T { case abi.ArrayTy, abi.SliceTy: + // Explicitly convert multidimensional types to predefined type in go side. inner := bindTypeJava(*kind.Elem) switch inner { case "boolean": diff --git a/mobile/interface.go b/mobile/interface.go index 8dcfe0baa8..d5200d5b1b 100644 --- a/mobile/interface.go +++ b/mobile/interface.go @@ -56,12 +56,68 @@ func (i *Interface) SetInt8(n int8) { i.object = &n } func (i *Interface) SetInt16(n int16) { i.object = &n } func (i *Interface) SetInt32(n int32) { i.object = &n } func (i *Interface) SetInt64(n int64) { i.object = &n } -func (i *Interface) SetUint8(bigint *BigInt) { n := uint8(bigint.bigint.Uint64()); i.object = &n } -func (i *Interface) SetUint16(bigint *BigInt) { n := uint16(bigint.bigint.Uint64()); i.object = &n } -func (i *Interface) SetUint32(bigint *BigInt) { n := uint32(bigint.bigint.Uint64()); i.object = &n } -func (i *Interface) SetUint64(bigint *BigInt) { n := bigint.bigint.Uint64(); i.object = &n } -func (i *Interface) SetBigInt(bigint *BigInt) { i.object = &bigint.bigint } -func (i *Interface) SetBigInts(bigints *BigInts) { i.object = &bigints.bigints } +func (i *Interface) SetInt8s(bigints *BigInts) { + ints := make([]int8, 0, bigints.Size()) + for _, bi := range bigints.bigints { + ints = append(ints, int8(bi.Int64())) + } + i.object = &ints +} +func (i *Interface) SetInt16s(bigints *BigInts) { + ints := make([]int16, 0, bigints.Size()) + for _, bi := range bigints.bigints { + ints = append(ints, int16(bi.Int64())) + } + i.object = &ints +} +func (i *Interface) SetInt32s(bigints *BigInts) { + ints := make([]int32, 0, bigints.Size()) + for _, bi := range bigints.bigints { + ints = append(ints, int32(bi.Int64())) + } + i.object = &ints +} +func (i *Interface) SetInt64s(bigints *BigInts) { + ints := make([]int64, 0, bigints.Size()) + for _, bi := range bigints.bigints { + ints = append(ints, bi.Int64()) + } + i.object = &ints +} +func (i *Interface) SetUint8(bigint *BigInt) { n := uint8(bigint.bigint.Uint64()); i.object = &n } +func (i *Interface) SetUint16(bigint *BigInt) { n := uint16(bigint.bigint.Uint64()); i.object = &n } +func (i *Interface) SetUint32(bigint *BigInt) { n := uint32(bigint.bigint.Uint64()); i.object = &n } +func (i *Interface) SetUint64(bigint *BigInt) { n := bigint.bigint.Uint64(); i.object = &n } +func (i *Interface) SetUint8s(bigints *BigInts) { + ints := make([]uint8, 0, bigints.Size()) + for _, bi := range bigints.bigints { + ints = append(ints, uint8(bi.Uint64())) + } + i.object = &ints +} +func (i *Interface) SetUint16s(bigints *BigInts) { + ints := make([]uint16, 0, bigints.Size()) + for _, bi := range bigints.bigints { + ints = append(ints, uint16(bi.Uint64())) + } + i.object = &ints +} +func (i *Interface) SetUint32s(bigints *BigInts) { + ints := make([]uint32, 0, bigints.Size()) + for _, bi := range bigints.bigints { + ints = append(ints, uint32(bi.Uint64())) + } + i.object = &ints +} +func (i *Interface) SetUint64s(bigints *BigInts) { + ints := make([]uint64, 0, bigints.Size()) + for _, bi := range bigints.bigints { + ints = append(ints, bi.Uint64()) + } + i.object = &ints +} +func (i *Interface) SetBigInt(bigint *BigInt) { i.object = &bigint.bigint } +func (i *Interface) SetBigInts(bigints *BigInts) { i.object = &bigints.bigints } func (i *Interface) SetDefaultBool() { i.object = new(bool) } func (i *Interface) SetDefaultBools() { i.object = new([]bool) } @@ -74,13 +130,21 @@ func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) } func (i *Interface) SetDefaultHash() { i.object = new(common.Hash) } func (i *Interface) SetDefaultHashes() { i.object = new([]common.Hash) } func (i *Interface) SetDefaultInt8() { i.object = new(int8) } +func (i *Interface) SetDefaultInt8s() { i.object = new([]int8) } func (i *Interface) SetDefaultInt16() { i.object = new(int16) } +func (i *Interface) SetDefaultInt16s() { i.object = new([]int16) } func (i *Interface) SetDefaultInt32() { i.object = new(int32) } +func (i *Interface) SetDefaultInt32s() { i.object = new([]int32) } func (i *Interface) SetDefaultInt64() { i.object = new(int64) } +func (i *Interface) SetDefaultInt64s() { i.object = new([]int64) } func (i *Interface) SetDefaultUint8() { i.object = new(uint8) } +func (i *Interface) SetDefaultUint8s() { i.object = new([]uint8) } func (i *Interface) SetDefaultUint16() { i.object = new(uint16) } +func (i *Interface) SetDefaultUint16s() { i.object = new([]uint16) } func (i *Interface) SetDefaultUint32() { i.object = new(uint32) } +func (i *Interface) SetDefaultUint32s() { i.object = new([]uint32) } func (i *Interface) SetDefaultUint64() { i.object = new(uint64) } +func (i *Interface) SetDefaultUint64s() { i.object = new([]uint64) } func (i *Interface) SetDefaultBigInt() { i.object = new(*big.Int) } func (i *Interface) SetDefaultBigInts() { i.object = new([]*big.Int) } @@ -98,6 +162,38 @@ func (i *Interface) GetInt8() int8 { return *i.object.(*int8) } func (i *Interface) GetInt16() int16 { return *i.object.(*int16) } func (i *Interface) GetInt32() int32 { return *i.object.(*int32) } func (i *Interface) GetInt64() int64 { return *i.object.(*int64) } +func (i *Interface) GetInt8s() *BigInts { + val := i.object.(*[]int8) + bigints := NewBigInts(len(*val)) + for i, v := range *val { + bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))}) + } + return bigints +} +func (i *Interface) GetInt16s() *BigInts { + val := i.object.(*[]int16) + bigints := NewBigInts(len(*val)) + for i, v := range *val { + bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))}) + } + return bigints +} +func (i *Interface) GetInt32s() *BigInts { + val := i.object.(*[]int32) + bigints := NewBigInts(len(*val)) + for i, v := range *val { + bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))}) + } + return bigints +} +func (i *Interface) GetInt64s() *BigInts { + val := i.object.(*[]int64) + bigints := NewBigInts(len(*val)) + for i, v := range *val { + bigints.Set(i, &BigInt{new(big.Int).SetInt64(v)}) + } + return bigints +} func (i *Interface) GetUint8() *BigInt { return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))} } @@ -110,6 +206,38 @@ func (i *Interface) GetUint32() *BigInt { func (i *Interface) GetUint64() *BigInt { return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))} } +func (i *Interface) GetUint8s() *BigInts { + val := i.object.(*[]uint8) + bigints := NewBigInts(len(*val)) + for i, v := range *val { + bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))}) + } + return bigints +} +func (i *Interface) GetUint16s() *BigInts { + val := i.object.(*[]uint16) + bigints := NewBigInts(len(*val)) + for i, v := range *val { + bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))}) + } + return bigints +} +func (i *Interface) GetUint32s() *BigInts { + val := i.object.(*[]uint32) + bigints := NewBigInts(len(*val)) + for i, v := range *val { + bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))}) + } + return bigints +} +func (i *Interface) GetUint64s() *BigInts { + val := i.object.(*[]uint64) + bigints := NewBigInts(len(*val)) + for i, v := range *val { + bigints.Set(i, &BigInt{new(big.Int).SetUint64(v)}) + } + return bigints +} func (i *Interface) GetBigInt() *BigInt { return &BigInt{*i.object.(**big.Int)} } func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} } diff --git a/mobile/interface_test.go b/mobile/interface_test.go index 8923d6adb4..4bd1af47aa 100644 --- a/mobile/interface_test.go +++ b/mobile/interface_test.go @@ -46,10 +46,18 @@ func TestInterfaceGetSet(t *testing.T) { {"Int16", int16(1), int16(1)}, {"Int32", int32(1), int32(1)}, {"Int64", int64(1), int64(1)}, + {"Int8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, + {"Int16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, + {"Int32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, + {"Int64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, {"Uint8", NewBigInt(1), NewBigInt(1)}, {"Uint16", NewBigInt(1), NewBigInt(1)}, {"Uint32", NewBigInt(1), NewBigInt(1)}, {"Uint64", NewBigInt(1), NewBigInt(1)}, + {"Uint8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, + {"Uint16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, + {"Uint32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, + {"Uint64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, {"BigInt", NewBigInt(1), NewBigInt(1)}, {"BigInts", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, }