reflect.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package abi
  17. import (
  18. "fmt"
  19. "reflect"
  20. )
  21. // indirect recursively dereferences the value until it either gets the value
  22. // or finds a big.Int
  23. func indirect(v reflect.Value) reflect.Value {
  24. if v.Kind() == reflect.Ptr && v.Elem().Type() != derefbigT {
  25. return indirect(v.Elem())
  26. }
  27. return v
  28. }
  29. // reflectIntKind returns the reflect using the given size and
  30. // unsignedness.
  31. func reflectIntKindAndType(unsigned bool, size int) (reflect.Kind, reflect.Type) {
  32. switch size {
  33. case 8:
  34. if unsigned {
  35. return reflect.Uint8, uint8T
  36. }
  37. return reflect.Int8, int8T
  38. case 16:
  39. if unsigned {
  40. return reflect.Uint16, uint16T
  41. }
  42. return reflect.Int16, int16T
  43. case 32:
  44. if unsigned {
  45. return reflect.Uint32, uint32T
  46. }
  47. return reflect.Int32, int32T
  48. case 64:
  49. if unsigned {
  50. return reflect.Uint64, uint64T
  51. }
  52. return reflect.Int64, int64T
  53. }
  54. return reflect.Ptr, bigT
  55. }
  56. // mustArrayToBytesSlice creates a new byte slice with the exact same size as value
  57. // and copies the bytes in value to the new slice.
  58. func mustArrayToByteSlice(value reflect.Value) reflect.Value {
  59. slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len())
  60. reflect.Copy(slice, value)
  61. return slice
  62. }
  63. // set attempts to assign src to dst by either setting, copying or otherwise.
  64. //
  65. // set is a bit more lenient when it comes to assignment and doesn't force an as
  66. // strict ruleset as bare `reflect` does.
  67. func set(dst, src reflect.Value, output Argument) error {
  68. dstType := dst.Type()
  69. srcType := src.Type()
  70. switch {
  71. case dstType.AssignableTo(srcType):
  72. dst.Set(src)
  73. case dstType.Kind() == reflect.Interface:
  74. dst.Set(src)
  75. case dstType.Kind() == reflect.Ptr:
  76. return set(dst.Elem(), src, output)
  77. default:
  78. return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
  79. }
  80. return nil
  81. }
  82. // requireAssignable assures that `dest` is a pointer and it's not an interface.
  83. func requireAssignable(dst, src reflect.Value) error {
  84. if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface {
  85. return fmt.Errorf("abi: cannot unmarshal %v into %v", src.Type(), dst.Type())
  86. }
  87. return nil
  88. }
  89. // requireUnpackKind verifies preconditions for unpacking `args` into `kind`
  90. func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind,
  91. args Arguments) error {
  92. switch k {
  93. case reflect.Struct:
  94. case reflect.Slice, reflect.Array:
  95. if minLen := args.LengthNonIndexed(); v.Len() < minLen {
  96. return fmt.Errorf("abi: insufficient number of elements in the list/array for unpack, want %d, got %d",
  97. minLen, v.Len())
  98. }
  99. default:
  100. return fmt.Errorf("abi: cannot unmarshal tuple into %v", t)
  101. }
  102. return nil
  103. }
  104. // requireUniqueStructFieldNames makes sure field names don't collide
  105. func requireUniqueStructFieldNames(args Arguments) error {
  106. exists := make(map[string]bool)
  107. for _, arg := range args {
  108. field := capitalise(arg.Name)
  109. if field == "" {
  110. return fmt.Errorf("abi: purely underscored output cannot unpack to struct")
  111. }
  112. if exists[field] {
  113. return fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", field)
  114. }
  115. exists[field] = true
  116. }
  117. return nil
  118. }