pointer_reflect.go 9.9 KB

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