pointer_reflect.go 10 KB

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