pointer_reflect.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 bool) pointer {
  70. v := reflect.ValueOf(*i)
  71. u := reflect.New(v.Type())
  72. u.Elem().Set(v)
  73. return pointer{v: u}
  74. }
  75. // valToPointer converts v to a pointer. v must be of pointer type.
  76. func valToPointer(v reflect.Value) pointer {
  77. return pointer{v: v}
  78. }
  79. // offset converts from a pointer to a structure to a pointer to
  80. // one of its fields.
  81. func (p pointer) offset(f field) pointer {
  82. return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
  83. }
  84. func (p pointer) isNil() bool {
  85. return p.v.IsNil()
  86. }
  87. // grow updates the slice s in place to make it one element longer.
  88. // s must be addressable.
  89. // Returns the (addressable) new element.
  90. func grow(s reflect.Value) reflect.Value {
  91. n, m := s.Len(), s.Cap()
  92. if n < m {
  93. s.SetLen(n + 1)
  94. } else {
  95. s.Set(reflect.Append(s, reflect.Zero(s.Type().Elem())))
  96. }
  97. return s.Index(n)
  98. }
  99. func (p pointer) toInt64() *int64 {
  100. return p.v.Interface().(*int64)
  101. }
  102. func (p pointer) toInt64Ptr() **int64 {
  103. return p.v.Interface().(**int64)
  104. }
  105. func (p pointer) toInt64Slice() *[]int64 {
  106. return p.v.Interface().(*[]int64)
  107. }
  108. var int32ptr = reflect.TypeOf((*int32)(nil))
  109. func (p pointer) toInt32() *int32 {
  110. return p.v.Convert(int32ptr).Interface().(*int32)
  111. }
  112. // The toInt32Ptr/Slice methods don't work because of enums.
  113. // Instead, we must use set/get methods for the int32ptr/slice case.
  114. /*
  115. func (p pointer) toInt32Ptr() **int32 {
  116. return p.v.Interface().(**int32)
  117. }
  118. func (p pointer) toInt32Slice() *[]int32 {
  119. return p.v.Interface().(*[]int32)
  120. }
  121. */
  122. func (p pointer) getInt32Ptr() *int32 {
  123. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  124. // raw int32 type
  125. return p.v.Elem().Interface().(*int32)
  126. }
  127. // an enum
  128. return p.v.Elem().Convert(int32PtrType).Interface().(*int32)
  129. }
  130. func (p pointer) setInt32Ptr(v int32) {
  131. // Allocate value in a *int32. Possibly convert that to a *enum.
  132. // Then assign it to a **int32 or **enum.
  133. // Note: we can convert *int32 to *enum, but we can't convert
  134. // **int32 to **enum!
  135. p.v.Elem().Set(reflect.ValueOf(&v).Convert(p.v.Type().Elem()))
  136. }
  137. // getInt32Slice copies []int32 from p as a new slice.
  138. // This behavior differs from the implementation in pointer_unsafe.go.
  139. func (p pointer) getInt32Slice() []int32 {
  140. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  141. // raw int32 type
  142. return p.v.Elem().Interface().([]int32)
  143. }
  144. // an enum
  145. // Allocate a []int32, then assign []enum's values into it.
  146. // Note: we can't convert []enum to []int32.
  147. slice := p.v.Elem()
  148. s := make([]int32, slice.Len())
  149. for i := 0; i < slice.Len(); i++ {
  150. s[i] = int32(slice.Index(i).Int())
  151. }
  152. return s
  153. }
  154. // setInt32Slice copies []int32 into p as a new slice.
  155. // This behavior differs from the implementation in pointer_unsafe.go.
  156. func (p pointer) setInt32Slice(v []int32) {
  157. if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {
  158. // raw int32 type
  159. p.v.Elem().Set(reflect.ValueOf(v))
  160. return
  161. }
  162. // an enum
  163. // Allocate a []enum, then assign []int32's values into it.
  164. // Note: we can't convert []enum to []int32.
  165. slice := reflect.MakeSlice(p.v.Type().Elem(), len(v), cap(v))
  166. for i, x := range v {
  167. slice.Index(i).SetInt(int64(x))
  168. }
  169. p.v.Elem().Set(slice)
  170. }
  171. func (p pointer) appendInt32Slice(v int32) {
  172. grow(p.v.Elem()).SetInt(int64(v))
  173. }
  174. func (p pointer) toUint64() *uint64 {
  175. return p.v.Interface().(*uint64)
  176. }
  177. func (p pointer) toUint64Ptr() **uint64 {
  178. return p.v.Interface().(**uint64)
  179. }
  180. func (p pointer) toUint64Slice() *[]uint64 {
  181. return p.v.Interface().(*[]uint64)
  182. }
  183. func (p pointer) toUint32() *uint32 {
  184. return p.v.Interface().(*uint32)
  185. }
  186. func (p pointer) toUint32Ptr() **uint32 {
  187. return p.v.Interface().(**uint32)
  188. }
  189. func (p pointer) toUint32Slice() *[]uint32 {
  190. return p.v.Interface().(*[]uint32)
  191. }
  192. func (p pointer) toBool() *bool {
  193. return p.v.Interface().(*bool)
  194. }
  195. func (p pointer) toBoolPtr() **bool {
  196. return p.v.Interface().(**bool)
  197. }
  198. func (p pointer) toBoolSlice() *[]bool {
  199. return p.v.Interface().(*[]bool)
  200. }
  201. func (p pointer) toFloat64() *float64 {
  202. return p.v.Interface().(*float64)
  203. }
  204. func (p pointer) toFloat64Ptr() **float64 {
  205. return p.v.Interface().(**float64)
  206. }
  207. func (p pointer) toFloat64Slice() *[]float64 {
  208. return p.v.Interface().(*[]float64)
  209. }
  210. func (p pointer) toFloat32() *float32 {
  211. return p.v.Interface().(*float32)
  212. }
  213. func (p pointer) toFloat32Ptr() **float32 {
  214. return p.v.Interface().(**float32)
  215. }
  216. func (p pointer) toFloat32Slice() *[]float32 {
  217. return p.v.Interface().(*[]float32)
  218. }
  219. func (p pointer) toString() *string {
  220. return p.v.Interface().(*string)
  221. }
  222. func (p pointer) toStringPtr() **string {
  223. return p.v.Interface().(**string)
  224. }
  225. func (p pointer) toStringSlice() *[]string {
  226. return p.v.Interface().(*[]string)
  227. }
  228. func (p pointer) toBytes() *[]byte {
  229. return p.v.Interface().(*[]byte)
  230. }
  231. func (p pointer) toBytesSlice() *[][]byte {
  232. return p.v.Interface().(*[][]byte)
  233. }
  234. func (p pointer) toExtensions() *XXX_InternalExtensions {
  235. return p.v.Interface().(*XXX_InternalExtensions)
  236. }
  237. func (p pointer) toOldExtensions() *map[int32]Extension {
  238. return p.v.Interface().(*map[int32]Extension)
  239. }
  240. func (p pointer) getPointer() pointer {
  241. return pointer{v: p.v.Elem()}
  242. }
  243. func (p pointer) setPointer(q pointer) {
  244. p.v.Elem().Set(q.v)
  245. }
  246. func (p pointer) appendPointer(q pointer) {
  247. grow(p.v.Elem()).Set(q.v)
  248. }
  249. // getPointerSlice copies []*T from p as a new []pointer.
  250. // This behavior differs from the implementation in pointer_unsafe.go.
  251. func (p pointer) getPointerSlice() []pointer {
  252. if p.v.IsNil() {
  253. return nil
  254. }
  255. n := p.v.Elem().Len()
  256. s := make([]pointer, n)
  257. for i := 0; i < n; i++ {
  258. s[i] = pointer{v: p.v.Elem().Index(i)}
  259. }
  260. return s
  261. }
  262. // setPointerSlice copies []pointer into p as a new []*T.
  263. // This behavior differs from the implementation in pointer_unsafe.go.
  264. func (p pointer) setPointerSlice(v []pointer) {
  265. if v == nil {
  266. p.v.Elem().Set(reflect.New(p.v.Elem().Type()).Elem())
  267. return
  268. }
  269. s := reflect.MakeSlice(p.v.Elem().Type(), 0, len(v))
  270. for _, p := range v {
  271. s = reflect.Append(s, p.v)
  272. }
  273. p.v.Elem().Set(s)
  274. }
  275. // getInterfacePointer returns a pointer that points to the
  276. // interface data of the interface pointed by p.
  277. func (p pointer) getInterfacePointer() pointer {
  278. if p.v.Elem().IsNil() {
  279. return pointer{v: p.v.Elem()}
  280. }
  281. return pointer{v: p.v.Elem().Elem().Elem().Field(0).Addr()} // *interface -> interface -> *struct -> struct
  282. }
  283. func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
  284. // TODO: check that p.v.Type().Elem() == t?
  285. return p.v
  286. }
  287. func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
  288. atomicLock.Lock()
  289. defer atomicLock.Unlock()
  290. return *p
  291. }
  292. func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
  293. atomicLock.Lock()
  294. defer atomicLock.Unlock()
  295. *p = v
  296. }
  297. func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
  298. atomicLock.Lock()
  299. defer atomicLock.Unlock()
  300. return *p
  301. }
  302. func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
  303. atomicLock.Lock()
  304. defer atomicLock.Unlock()
  305. *p = v
  306. }
  307. func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
  308. atomicLock.Lock()
  309. defer atomicLock.Unlock()
  310. return *p
  311. }
  312. func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
  313. atomicLock.Lock()
  314. defer atomicLock.Unlock()
  315. *p = v
  316. }
  317. func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
  318. atomicLock.Lock()
  319. defer atomicLock.Unlock()
  320. return *p
  321. }
  322. func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
  323. atomicLock.Lock()
  324. defer atomicLock.Unlock()
  325. *p = v
  326. }
  327. var atomicLock sync.Mutex