reflect.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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() != big_t {
  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, uint8_t
  36. }
  37. return reflect.Int8, int8_t
  38. case 16:
  39. if unsigned {
  40. return reflect.Uint16, uint16_t
  41. }
  42. return reflect.Int16, int16_t
  43. case 32:
  44. if unsigned {
  45. return reflect.Uint32, uint32_t
  46. }
  47. return reflect.Int32, int32_t
  48. case 64:
  49. if unsigned {
  50. return reflect.Uint64, uint64_t
  51. }
  52. return reflect.Int64, int64_t
  53. }
  54. return reflect.Ptr, big_t
  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(src.Type()):
  72. dst.Set(src)
  73. case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice:
  74. if dst.Len() < output.Type.SliceSize {
  75. return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.SliceSize, dst.Len())
  76. }
  77. reflect.Copy(dst, src)
  78. case dstType.Kind() == reflect.Interface:
  79. dst.Set(src)
  80. case dstType.Kind() == reflect.Ptr:
  81. return set(dst.Elem(), src, output)
  82. default:
  83. return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
  84. }
  85. return nil
  86. }