You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pointer_reflect.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2012 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. // +build purego appengine js
  32. // This file contains an implementation of proto field accesses using package reflect.
  33. // It is slower than the code in pointer_unsafe.go but it avoids package unsafe and can
  34. // be used on App Engine.
  35. package proto
  36. import (
  37. "reflect"
  38. "sync"
  39. )
  40. const unsafeAllowed = false
  41. // A field identifies a field in a struct, accessible from a pointer.
  42. // In this implementation, a field is identified by the sequence of field indices
  43. // passed to reflect's FieldByIndex.
  44. type field []int
  45. // toField returns a field equivalent to the given reflect field.
  46. func toField(f *reflect.StructField) field {
  47. return f.Index
  48. }
  49. // invalidField is an invalid field identifier.
  50. var invalidField = field(nil)
  51. // zeroField is a noop when calling pointer.offset.
  52. var zeroField = field([]int{})
  53. // IsValid reports whether the field identifier is valid.
  54. func (f field) IsValid() bool { return f != nil }
  55. // The pointer type is for the table-driven decoder.
  56. // The implementation here uses a reflect.Value of pointer type to
  57. // create a generic pointer. In pointer_unsafe.go we use unsafe
  58. // instead of reflect to implement the same (but faster) interface.
  59. type pointer struct {
  60. v reflect.Value
  61. }
  62. // toPointer converts an interface of pointer type to a pointer
  63. // that points to the same target.
  64. func toPointer(i *Message) pointer {
  65. return pointer{v: reflect.ValueOf(*i)}
  66. }
  67. // toAddrPointer converts an interface to a pointer that points to
  68. // the interface data.
  69. func toAddrPointer(i *interface{}, isptr, deref bool) pointer {
  70. v := reflect.ValueOf(*i)
  71. u := reflect.New(v.Type())
  72. u.Elem().Set(v)
  73. if deref {
  74. u = u.Elem()
  75. }
  76. return pointer{v: u}
  77. }
  78. // valToPointer converts v to a pointer. v must be of pointer type.
  79. func valToPointer(v reflect.Value) pointer {
  80. return pointer{v: v}
  81. }
  82. // offset converts from a pointer to a structure to a pointer to
  83. // one of its fields.
  84. func (p pointer) offset(f field) pointer {
  85. return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
  86. }
  87. func (p pointer) isNil() bool {
  88. return p.v.IsNil()
  89. }
  90. // grow updates the slice s in place to make it one element longer.
  91. // s must be addressable.
  92. // Returns the (addressable) new element.
  93. func grow(s reflect.Value) reflect.Value {
  94. n, m := s.Len(), s.Cap()
  95. if n < m {
  96. s.SetLen(n + 1)
  97. } else {
  98. s.Set(reflect.Append(s, reflect.Zero(s.Type().Elem())))
  99. }
  100. return s.Index(n)
  101. }
  102. func (p pointer) toInt64() *int64 {
  103. return p.v.Interface().(*int64)
  104. }
  105. func (p pointer) toInt64Ptr() **int64 {
  106. return p.v.Interface().(**int64)
  107. }
  108. func (p pointer) toInt64Slice() *[]int64 {
  109. return p.v.Interface().(*[]int64)
  110. }
  111. var int32ptr = reflect.TypeOf((*int32)(nil))
  112. func (p pointer) toInt32() *int32 {
  113. return p.v.Convert(int32ptr).Interface().(*int32)
  114. }
  115. // The toInt32Ptr/Slice methods don't work because of enums.
  116. // Instead, we must use set/get methods for the int32ptr/slice case.
  117. /*
  118. func (p pointer) toInt32Ptr() **int32 {
  119. return p.v.Interface().(**int32)
  120. }
  121. func (p pointer) toInt32Slice() *[]int32 {
  122. return p.v.Interface().(*[]int32)
  123. }
  124. */
  125. func (p pointer) getInt32Ptr() *int32 {
  126. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  127. // raw int32 type
  128. return p.v.Elem().Interface().(*int32)
  129. }
  130. // an enum
  131. return p.v.Elem().Convert(int32PtrType).Interface().(*int32)
  132. }
  133. func (p pointer) setInt32Ptr(v int32) {
  134. // Allocate value in a *int32. Possibly convert that to a *enum.
  135. // Then assign it to a **int32 or **enum.
  136. // Note: we can convert *int32 to *enum, but we can't convert
  137. // **int32 to **enum!
  138. p.v.Elem().Set(reflect.ValueOf(&v).Convert(p.v.Type().Elem()))
  139. }
  140. // getInt32Slice copies []int32 from p as a new slice.
  141. // This behavior differs from the implementation in pointer_unsafe.go.
  142. func (p pointer) getInt32Slice() []int32 {
  143. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  144. // raw int32 type
  145. return p.v.Elem().Interface().([]int32)
  146. }
  147. // an enum
  148. // Allocate a []int32, then assign []enum's values into it.
  149. // Note: we can't convert []enum to []int32.
  150. slice := p.v.Elem()
  151. s := make([]int32, slice.Len())
  152. for i := 0; i < slice.Len(); i++ {
  153. s[i] = int32(slice.Index(i).Int())
  154. }
  155. return s
  156. }
  157. // setInt32Slice copies []int32 into p as a new slice.
  158. // This behavior differs from the implementation in pointer_unsafe.go.
  159. func (p pointer) setInt32Slice(v []int32) {
  160. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  161. // raw int32 type
  162. p.v.Elem().Set(reflect.ValueOf(v))
  163. return
  164. }
  165. // an enum
  166. // Allocate a []enum, then assign []int32's values into it.
  167. // Note: we can't convert []enum to []int32.
  168. slice := reflect.MakeSlice(p.v.Type().Elem(), len(v), cap(v))
  169. for i, x := range v {
  170. slice.Index(i).SetInt(int64(x))
  171. }
  172. p.v.Elem().Set(slice)
  173. }
  174. func (p pointer) appendInt32Slice(v int32) {
  175. grow(p.v.Elem()).SetInt(int64(v))
  176. }
  177. func (p pointer) toUint64() *uint64 {
  178. return p.v.Interface().(*uint64)
  179. }
  180. func (p pointer) toUint64Ptr() **uint64 {
  181. return p.v.Interface().(**uint64)
  182. }
  183. func (p pointer) toUint64Slice() *[]uint64 {
  184. return p.v.Interface().(*[]uint64)
  185. }
  186. func (p pointer) toUint32() *uint32 {
  187. return p.v.Interface().(*uint32)
  188. }
  189. func (p pointer) toUint32Ptr() **uint32 {
  190. return p.v.Interface().(**uint32)
  191. }
  192. func (p pointer) toUint32Slice() *[]uint32 {
  193. return p.v.Interface().(*[]uint32)
  194. }
  195. func (p pointer) toBool() *bool {
  196. return p.v.Interface().(*bool)
  197. }
  198. func (p pointer) toBoolPtr() **bool {
  199. return p.v.Interface().(**bool)
  200. }
  201. func (p pointer) toBoolSlice() *[]bool {
  202. return p.v.Interface().(*[]bool)
  203. }
  204. func (p pointer) toFloat64() *float64 {
  205. return p.v.Interface().(*float64)
  206. }
  207. func (p pointer) toFloat64Ptr() **float64 {
  208. return p.v.Interface().(**float64)
  209. }
  210. func (p pointer) toFloat64Slice() *[]float64 {
  211. return p.v.Interface().(*[]float64)
  212. }
  213. func (p pointer) toFloat32() *float32 {
  214. return p.v.Interface().(*float32)
  215. }
  216. func (p pointer) toFloat32Ptr() **float32 {
  217. return p.v.Interface().(**float32)
  218. }
  219. func (p pointer) toFloat32Slice() *[]float32 {
  220. return p.v.Interface().(*[]float32)
  221. }
  222. func (p pointer) toString() *string {
  223. return p.v.Interface().(*string)
  224. }
  225. func (p pointer) toStringPtr() **string {
  226. return p.v.Interface().(**string)
  227. }
  228. func (p pointer) toStringSlice() *[]string {
  229. return p.v.Interface().(*[]string)
  230. }
  231. func (p pointer) toBytes() *[]byte {
  232. return p.v.Interface().(*[]byte)
  233. }
  234. func (p pointer) toBytesSlice() *[][]byte {
  235. return p.v.Interface().(*[][]byte)
  236. }
  237. func (p pointer) toExtensions() *XXX_InternalExtensions {
  238. return p.v.Interface().(*XXX_InternalExtensions)
  239. }
  240. func (p pointer) toOldExtensions() *map[int32]Extension {
  241. return p.v.Interface().(*map[int32]Extension)
  242. }
  243. func (p pointer) getPointer() pointer {
  244. return pointer{v: p.v.Elem()}
  245. }
  246. func (p pointer) setPointer(q pointer) {
  247. p.v.Elem().Set(q.v)
  248. }
  249. func (p pointer) appendPointer(q pointer) {
  250. grow(p.v.Elem()).Set(q.v)
  251. }
  252. // getPointerSlice copies []*T from p as a new []pointer.
  253. // This behavior differs from the implementation in pointer_unsafe.go.
  254. func (p pointer) getPointerSlice() []pointer {
  255. if p.v.IsNil() {
  256. return nil
  257. }
  258. n := p.v.Elem().Len()
  259. s := make([]pointer, n)
  260. for i := 0; i < n; i++ {
  261. s[i] = pointer{v: p.v.Elem().Index(i)}
  262. }
  263. return s
  264. }
  265. // setPointerSlice copies []pointer into p as a new []*T.
  266. // This behavior differs from the implementation in pointer_unsafe.go.
  267. func (p pointer) setPointerSlice(v []pointer) {
  268. if v == nil {
  269. p.v.Elem().Set(reflect.New(p.v.Elem().Type()).Elem())
  270. return
  271. }
  272. s := reflect.MakeSlice(p.v.Elem().Type(), 0, len(v))
  273. for _, p := range v {
  274. s = reflect.Append(s, p.v)
  275. }
  276. p.v.Elem().Set(s)
  277. }
  278. // getInterfacePointer returns a pointer that points to the
  279. // interface data of the interface pointed by p.
  280. func (p pointer) getInterfacePointer() pointer {
  281. if p.v.Elem().IsNil() {
  282. return pointer{v: p.v.Elem()}
  283. }
  284. return pointer{v: p.v.Elem().Elem().Elem().Field(0).Addr()} // *interface -> interface -> *struct -> struct
  285. }
  286. func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
  287. // TODO: check that p.v.Type().Elem() == t?
  288. return p.v
  289. }
  290. func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
  291. atomicLock.Lock()
  292. defer atomicLock.Unlock()
  293. return *p
  294. }
  295. func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
  296. atomicLock.Lock()
  297. defer atomicLock.Unlock()
  298. *p = v
  299. }
  300. func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
  301. atomicLock.Lock()
  302. defer atomicLock.Unlock()
  303. return *p
  304. }
  305. func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
  306. atomicLock.Lock()
  307. defer atomicLock.Unlock()
  308. *p = v
  309. }
  310. func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
  311. atomicLock.Lock()
  312. defer atomicLock.Unlock()
  313. return *p
  314. }
  315. func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
  316. atomicLock.Lock()
  317. defer atomicLock.Unlock()
  318. *p = v
  319. }
  320. func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
  321. atomicLock.Lock()
  322. defer atomicLock.Unlock()
  323. return *p
  324. }
  325. func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
  326. atomicLock.Lock()
  327. defer atomicLock.Unlock()
  328. *p = v
  329. }
  330. var atomicLock sync.Mutex