pointer_reflect.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build purego
  5. // This file contains an implementation of proto field accesses using package reflect.
  6. // It is slower than the code in pointer_unsafe.go but it avoids package unsafe and can
  7. // be used on App Engine.
  8. package proto
  9. import (
  10. "reflect"
  11. "sync"
  12. )
  13. const unsafeAllowed = false
  14. // A field identifies a field in a struct, accessible from a pointer.
  15. // In this implementation, a field is identified by the sequence of field indices
  16. // passed to reflect's FieldByIndex.
  17. type field []int
  18. // toField returns a field equivalent to the given reflect field.
  19. func toField(f *reflect.StructField) field {
  20. return f.Index
  21. }
  22. // invalidField is an invalid field identifier.
  23. var invalidField = field(nil)
  24. // zeroField is a noop when calling pointer.offset.
  25. var zeroField = field([]int{})
  26. // IsValid reports whether the field identifier is valid.
  27. func (f field) IsValid() bool { return f != nil }
  28. // The pointer type is for the table-driven decoder.
  29. // The implementation here uses a reflect.Value of pointer type to
  30. // create a generic pointer. In pointer_unsafe.go we use unsafe
  31. // instead of reflect to implement the same (but faster) interface.
  32. type pointer struct {
  33. v reflect.Value
  34. }
  35. // toPointer converts an interface of pointer type to a pointer
  36. // that points to the same target.
  37. func toPointer(i *Message) pointer {
  38. return pointer{v: reflect.ValueOf(*i)}
  39. }
  40. // toAddrPointer converts an interface to a pointer that points to
  41. // the interface data.
  42. func toAddrPointer(i *interface{}, isptr, deref bool) pointer {
  43. v := reflect.ValueOf(*i)
  44. u := reflect.New(v.Type())
  45. u.Elem().Set(v)
  46. if deref {
  47. u = u.Elem()
  48. }
  49. return pointer{v: u}
  50. }
  51. // valToPointer converts v to a pointer. v must be of pointer type.
  52. func valToPointer(v reflect.Value) pointer {
  53. return pointer{v: v}
  54. }
  55. // offset converts from a pointer to a structure to a pointer to
  56. // one of its fields.
  57. func (p pointer) offset(f field) pointer {
  58. return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
  59. }
  60. func (p pointer) isNil() bool {
  61. return p.v.IsNil()
  62. }
  63. // grow updates the slice s in place to make it one element longer.
  64. // s must be addressable.
  65. // Returns the (addressable) new element.
  66. func grow(s reflect.Value) reflect.Value {
  67. n, m := s.Len(), s.Cap()
  68. if n < m {
  69. s.SetLen(n + 1)
  70. } else {
  71. s.Set(reflect.Append(s, reflect.Zero(s.Type().Elem())))
  72. }
  73. return s.Index(n)
  74. }
  75. func (p pointer) toInt64() *int64 {
  76. return p.v.Interface().(*int64)
  77. }
  78. func (p pointer) toInt64Ptr() **int64 {
  79. return p.v.Interface().(**int64)
  80. }
  81. func (p pointer) toInt64Slice() *[]int64 {
  82. return p.v.Interface().(*[]int64)
  83. }
  84. var int32ptr = reflect.TypeOf((*int32)(nil))
  85. func (p pointer) toInt32() *int32 {
  86. return p.v.Convert(int32ptr).Interface().(*int32)
  87. }
  88. // The toInt32Ptr/Slice methods don't work because of enums.
  89. // Instead, we must use set/get methods for the int32ptr/slice case.
  90. /*
  91. func (p pointer) toInt32Ptr() **int32 {
  92. return p.v.Interface().(**int32)
  93. }
  94. func (p pointer) toInt32Slice() *[]int32 {
  95. return p.v.Interface().(*[]int32)
  96. }
  97. */
  98. func (p pointer) getInt32Ptr() *int32 {
  99. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  100. // raw int32 type
  101. return p.v.Elem().Interface().(*int32)
  102. }
  103. // an enum
  104. return p.v.Elem().Convert(int32PtrType).Interface().(*int32)
  105. }
  106. func (p pointer) setInt32Ptr(v int32) {
  107. // Allocate value in a *int32. Possibly convert that to a *enum.
  108. // Then assign it to a **int32 or **enum.
  109. // Note: we can convert *int32 to *enum, but we can't convert
  110. // **int32 to **enum!
  111. p.v.Elem().Set(reflect.ValueOf(&v).Convert(p.v.Type().Elem()))
  112. }
  113. // getInt32Slice copies []int32 from p as a new slice.
  114. // This behavior differs from the implementation in pointer_unsafe.go.
  115. func (p pointer) getInt32Slice() []int32 {
  116. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  117. // raw int32 type
  118. return p.v.Elem().Interface().([]int32)
  119. }
  120. // an enum
  121. // Allocate a []int32, then assign []enum's values into it.
  122. // Note: we can't convert []enum to []int32.
  123. slice := p.v.Elem()
  124. s := make([]int32, slice.Len())
  125. for i := 0; i < slice.Len(); i++ {
  126. s[i] = int32(slice.Index(i).Int())
  127. }
  128. return s
  129. }
  130. // setInt32Slice copies []int32 into p as a new slice.
  131. // This behavior differs from the implementation in pointer_unsafe.go.
  132. func (p pointer) setInt32Slice(v []int32) {
  133. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  134. // raw int32 type
  135. p.v.Elem().Set(reflect.ValueOf(v))
  136. return
  137. }
  138. // an enum
  139. // Allocate a []enum, then assign []int32's values into it.
  140. // Note: we can't convert []enum to []int32.
  141. slice := reflect.MakeSlice(p.v.Type().Elem(), len(v), cap(v))
  142. for i, x := range v {
  143. slice.Index(i).SetInt(int64(x))
  144. }
  145. p.v.Elem().Set(slice)
  146. }
  147. func (p pointer) appendInt32Slice(v int32) {
  148. grow(p.v.Elem()).SetInt(int64(v))
  149. }
  150. func (p pointer) toUint64() *uint64 {
  151. return p.v.Interface().(*uint64)
  152. }
  153. func (p pointer) toUint64Ptr() **uint64 {
  154. return p.v.Interface().(**uint64)
  155. }
  156. func (p pointer) toUint64Slice() *[]uint64 {
  157. return p.v.Interface().(*[]uint64)
  158. }
  159. func (p pointer) toUint32() *uint32 {
  160. return p.v.Interface().(*uint32)
  161. }
  162. func (p pointer) toUint32Ptr() **uint32 {
  163. return p.v.Interface().(**uint32)
  164. }
  165. func (p pointer) toUint32Slice() *[]uint32 {
  166. return p.v.Interface().(*[]uint32)
  167. }
  168. func (p pointer) toBool() *bool {
  169. return p.v.Interface().(*bool)
  170. }
  171. func (p pointer) toBoolPtr() **bool {
  172. return p.v.Interface().(**bool)
  173. }
  174. func (p pointer) toBoolSlice() *[]bool {
  175. return p.v.Interface().(*[]bool)
  176. }
  177. func (p pointer) toFloat64() *float64 {
  178. return p.v.Interface().(*float64)
  179. }
  180. func (p pointer) toFloat64Ptr() **float64 {
  181. return p.v.Interface().(**float64)
  182. }
  183. func (p pointer) toFloat64Slice() *[]float64 {
  184. return p.v.Interface().(*[]float64)
  185. }
  186. func (p pointer) toFloat32() *float32 {
  187. return p.v.Interface().(*float32)
  188. }
  189. func (p pointer) toFloat32Ptr() **float32 {
  190. return p.v.Interface().(**float32)
  191. }
  192. func (p pointer) toFloat32Slice() *[]float32 {
  193. return p.v.Interface().(*[]float32)
  194. }
  195. func (p pointer) toString() *string {
  196. return p.v.Interface().(*string)
  197. }
  198. func (p pointer) toStringPtr() **string {
  199. return p.v.Interface().(**string)
  200. }
  201. func (p pointer) toStringSlice() *[]string {
  202. return p.v.Interface().(*[]string)
  203. }
  204. func (p pointer) toBytes() *[]byte {
  205. return p.v.Interface().(*[]byte)
  206. }
  207. func (p pointer) toBytesSlice() *[][]byte {
  208. return p.v.Interface().(*[][]byte)
  209. }
  210. func (p pointer) toExtensions() *XXX_InternalExtensions {
  211. return p.v.Interface().(*XXX_InternalExtensions)
  212. }
  213. func (p pointer) toOldExtensions() *map[int32]Extension {
  214. return p.v.Interface().(*map[int32]Extension)
  215. }
  216. func (p pointer) getPointer() pointer {
  217. return pointer{v: p.v.Elem()}
  218. }
  219. func (p pointer) setPointer(q pointer) {
  220. p.v.Elem().Set(q.v)
  221. }
  222. func (p pointer) appendPointer(q pointer) {
  223. grow(p.v.Elem()).Set(q.v)
  224. }
  225. // getPointerSlice copies []*T from p as a new []pointer.
  226. // This behavior differs from the implementation in pointer_unsafe.go.
  227. func (p pointer) getPointerSlice() []pointer {
  228. if p.v.IsNil() {
  229. return nil
  230. }
  231. n := p.v.Elem().Len()
  232. s := make([]pointer, n)
  233. for i := 0; i < n; i++ {
  234. s[i] = pointer{v: p.v.Elem().Index(i)}
  235. }
  236. return s
  237. }
  238. // setPointerSlice copies []pointer into p as a new []*T.
  239. // This behavior differs from the implementation in pointer_unsafe.go.
  240. func (p pointer) setPointerSlice(v []pointer) {
  241. if v == nil {
  242. p.v.Elem().Set(reflect.New(p.v.Elem().Type()).Elem())
  243. return
  244. }
  245. s := reflect.MakeSlice(p.v.Elem().Type(), 0, len(v))
  246. for _, p := range v {
  247. s = reflect.Append(s, p.v)
  248. }
  249. p.v.Elem().Set(s)
  250. }
  251. // getInterfacePointer returns a pointer that points to the
  252. // interface data of the interface pointed by p.
  253. func (p pointer) getInterfacePointer() pointer {
  254. if p.v.Elem().IsNil() {
  255. return pointer{v: p.v.Elem()}
  256. }
  257. return pointer{v: p.v.Elem().Elem().Elem().Field(0).Addr()} // *interface -> interface -> *struct -> struct
  258. }
  259. func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
  260. // TODO: check that p.v.Type().Elem() == t?
  261. return p.v
  262. }
  263. func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
  264. atomicLock.Lock()
  265. defer atomicLock.Unlock()
  266. return *p
  267. }
  268. func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
  269. atomicLock.Lock()
  270. defer atomicLock.Unlock()
  271. *p = v
  272. }
  273. func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
  274. atomicLock.Lock()
  275. defer atomicLock.Unlock()
  276. return *p
  277. }
  278. func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
  279. atomicLock.Lock()
  280. defer atomicLock.Unlock()
  281. *p = v
  282. }
  283. func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
  284. atomicLock.Lock()
  285. defer atomicLock.Unlock()
  286. return *p
  287. }
  288. func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
  289. atomicLock.Lock()
  290. defer atomicLock.Unlock()
  291. *p = v
  292. }
  293. func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
  294. atomicLock.Lock()
  295. defer atomicLock.Unlock()
  296. return *p
  297. }
  298. func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
  299. atomicLock.Lock()
  300. defer atomicLock.Unlock()
  301. *p = v
  302. }
  303. var atomicLock sync.Mutex