pointer_reflect.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. "math"
  38. "reflect"
  39. )
  40. // A structPointer is a pointer to a struct.
  41. type structPointer struct {
  42. v reflect.Value
  43. }
  44. // toStructPointer returns a structPointer equivalent to the given reflect value.
  45. // The reflect value must itself be a pointer to a struct.
  46. func toStructPointer(v reflect.Value) structPointer {
  47. return structPointer{v}
  48. }
  49. // IsNil reports whether p is nil.
  50. func structPointer_IsNil(p structPointer) bool {
  51. return p.v.IsNil()
  52. }
  53. // Interface returns the struct pointer as an interface value.
  54. func structPointer_Interface(p structPointer, _ reflect.Type) interface{} {
  55. return p.v.Interface()
  56. }
  57. // A field identifies a field in a struct, accessible from a structPointer.
  58. // In this implementation, a field is identified by the sequence of field indices
  59. // passed to reflect's FieldByIndex.
  60. type field []int
  61. // toField returns a field equivalent to the given reflect field.
  62. func toField(f *reflect.StructField) field {
  63. return f.Index
  64. }
  65. // invalidField is an invalid field identifier.
  66. var invalidField = field(nil)
  67. // IsValid reports whether the field identifier is valid.
  68. func (f field) IsValid() bool { return f != nil }
  69. // field returns the given field in the struct as a reflect value.
  70. func structPointer_field(p structPointer, f field) reflect.Value {
  71. // Special case: an extension map entry with a value of type T
  72. // passes a *T to the struct-handling code with a zero field,
  73. // expecting that it will be treated as equivalent to *struct{ X T },
  74. // which has the same memory layout. We have to handle that case
  75. // specially, because reflect will panic if we call FieldByIndex on a
  76. // non-struct.
  77. if f == nil {
  78. return p.v.Elem()
  79. }
  80. return p.v.Elem().FieldByIndex(f)
  81. }
  82. // ifield returns the given field in the struct as an interface value.
  83. func structPointer_ifield(p structPointer, f field) interface{} {
  84. return structPointer_field(p, f).Addr().Interface()
  85. }
  86. // Bytes returns the address of a []byte field in the struct.
  87. func structPointer_Bytes(p structPointer, f field) *[]byte {
  88. return structPointer_ifield(p, f).(*[]byte)
  89. }
  90. // BytesSlice returns the address of a [][]byte field in the struct.
  91. func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
  92. return structPointer_ifield(p, f).(*[][]byte)
  93. }
  94. // Bool returns the address of a *bool field in the struct.
  95. func structPointer_Bool(p structPointer, f field) **bool {
  96. return structPointer_ifield(p, f).(**bool)
  97. }
  98. // BoolVal returns the address of a bool field in the struct.
  99. func structPointer_BoolVal(p structPointer, f field) *bool {
  100. return structPointer_ifield(p, f).(*bool)
  101. }
  102. // BoolSlice returns the address of a []bool field in the struct.
  103. func structPointer_BoolSlice(p structPointer, f field) *[]bool {
  104. return structPointer_ifield(p, f).(*[]bool)
  105. }
  106. // String returns the address of a *string field in the struct.
  107. func structPointer_String(p structPointer, f field) **string {
  108. return structPointer_ifield(p, f).(**string)
  109. }
  110. // StringVal returns the address of a string field in the struct.
  111. func structPointer_StringVal(p structPointer, f field) *string {
  112. return structPointer_ifield(p, f).(*string)
  113. }
  114. // StringSlice returns the address of a []string field in the struct.
  115. func structPointer_StringSlice(p structPointer, f field) *[]string {
  116. return structPointer_ifield(p, f).(*[]string)
  117. }
  118. // Extensions returns the address of an extension map field in the struct.
  119. func structPointer_Extensions(p structPointer, f field) *XXX_InternalExtensions {
  120. return structPointer_ifield(p, f).(*XXX_InternalExtensions)
  121. }
  122. // ExtMap returns the address of an extension map field in the struct.
  123. func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
  124. return structPointer_ifield(p, f).(*map[int32]Extension)
  125. }
  126. // NewAt returns the reflect.Value for a pointer to a field in the struct.
  127. func structPointer_NewAt(p structPointer, f field, typ reflect.Type) reflect.Value {
  128. return structPointer_field(p, f).Addr()
  129. }
  130. // SetStructPointer writes a *struct field in the struct.
  131. func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
  132. structPointer_field(p, f).Set(q.v)
  133. }
  134. // GetStructPointer reads a *struct field in the struct.
  135. func structPointer_GetStructPointer(p structPointer, f field) structPointer {
  136. return structPointer{structPointer_field(p, f)}
  137. }
  138. // StructPointerSlice the address of a []*struct field in the struct.
  139. func structPointer_StructPointerSlice(p structPointer, f field) structPointerSlice {
  140. return structPointerSlice{structPointer_field(p, f)}
  141. }
  142. // A structPointerSlice represents the address of a slice of pointers to structs
  143. // (themselves messages or groups). That is, v.Type() is *[]*struct{...}.
  144. type structPointerSlice struct {
  145. v reflect.Value
  146. }
  147. func (p structPointerSlice) Len() int { return p.v.Len() }
  148. func (p structPointerSlice) Index(i int) structPointer { return structPointer{p.v.Index(i)} }
  149. func (p structPointerSlice) Append(q structPointer) {
  150. p.v.Set(reflect.Append(p.v, q.v))
  151. }
  152. var (
  153. int32Type = reflect.TypeOf(int32(0))
  154. uint32Type = reflect.TypeOf(uint32(0))
  155. float32Type = reflect.TypeOf(float32(0))
  156. int64Type = reflect.TypeOf(int64(0))
  157. uint64Type = reflect.TypeOf(uint64(0))
  158. float64Type = reflect.TypeOf(float64(0))
  159. )
  160. // A word32 represents a field of type *int32, *uint32, *float32, or *enum.
  161. // That is, v.Type() is *int32, *uint32, *float32, or *enum and v is assignable.
  162. type word32 struct {
  163. v reflect.Value
  164. }
  165. // IsNil reports whether p is nil.
  166. func word32_IsNil(p word32) bool {
  167. return p.v.IsNil()
  168. }
  169. // Set sets p to point at a newly allocated word with bits set to x.
  170. func word32_Set(p word32, o *Buffer, x uint32) {
  171. t := p.v.Type().Elem()
  172. switch t {
  173. case int32Type:
  174. if len(o.int32s) == 0 {
  175. o.int32s = make([]int32, uint32PoolSize)
  176. }
  177. o.int32s[0] = int32(x)
  178. p.v.Set(reflect.ValueOf(&o.int32s[0]))
  179. o.int32s = o.int32s[1:]
  180. return
  181. case uint32Type:
  182. if len(o.uint32s) == 0 {
  183. o.uint32s = make([]uint32, uint32PoolSize)
  184. }
  185. o.uint32s[0] = x
  186. p.v.Set(reflect.ValueOf(&o.uint32s[0]))
  187. o.uint32s = o.uint32s[1:]
  188. return
  189. case float32Type:
  190. if len(o.float32s) == 0 {
  191. o.float32s = make([]float32, uint32PoolSize)
  192. }
  193. o.float32s[0] = math.Float32frombits(x)
  194. p.v.Set(reflect.ValueOf(&o.float32s[0]))
  195. o.float32s = o.float32s[1:]
  196. return
  197. }
  198. // must be enum
  199. p.v.Set(reflect.New(t))
  200. p.v.Elem().SetInt(int64(int32(x)))
  201. }
  202. // Get gets the bits pointed at by p, as a uint32.
  203. func word32_Get(p word32) uint32 {
  204. elem := p.v.Elem()
  205. switch elem.Kind() {
  206. case reflect.Int32:
  207. return uint32(elem.Int())
  208. case reflect.Uint32:
  209. return uint32(elem.Uint())
  210. case reflect.Float32:
  211. return math.Float32bits(float32(elem.Float()))
  212. }
  213. panic("unreachable")
  214. }
  215. // Word32 returns a reference to a *int32, *uint32, *float32, or *enum field in the struct.
  216. func structPointer_Word32(p structPointer, f field) word32 {
  217. return word32{structPointer_field(p, f)}
  218. }
  219. // A word32Val represents a field of type int32, uint32, float32, or enum.
  220. // That is, v.Type() is int32, uint32, float32, or enum and v is assignable.
  221. type word32Val struct {
  222. v reflect.Value
  223. }
  224. // Set sets *p to x.
  225. func word32Val_Set(p word32Val, x uint32) {
  226. switch p.v.Type() {
  227. case int32Type:
  228. p.v.SetInt(int64(x))
  229. return
  230. case uint32Type:
  231. p.v.SetUint(uint64(x))
  232. return
  233. case float32Type:
  234. p.v.SetFloat(float64(math.Float32frombits(x)))
  235. return
  236. }
  237. // must be enum
  238. p.v.SetInt(int64(int32(x)))
  239. }
  240. // Get gets the bits pointed at by p, as a uint32.
  241. func word32Val_Get(p word32Val) uint32 {
  242. elem := p.v
  243. switch elem.Kind() {
  244. case reflect.Int32:
  245. return uint32(elem.Int())
  246. case reflect.Uint32:
  247. return uint32(elem.Uint())
  248. case reflect.Float32:
  249. return math.Float32bits(float32(elem.Float()))
  250. }
  251. panic("unreachable")
  252. }
  253. // Word32Val returns a reference to a int32, uint32, float32, or enum field in the struct.
  254. func structPointer_Word32Val(p structPointer, f field) word32Val {
  255. return word32Val{structPointer_field(p, f)}
  256. }
  257. // A word32Slice is a slice of 32-bit values.
  258. // That is, v.Type() is []int32, []uint32, []float32, or []enum.
  259. type word32Slice struct {
  260. v reflect.Value
  261. }
  262. func (p word32Slice) Append(x uint32) {
  263. n, m := p.v.Len(), p.v.Cap()
  264. if n < m {
  265. p.v.SetLen(n + 1)
  266. } else {
  267. t := p.v.Type().Elem()
  268. p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
  269. }
  270. elem := p.v.Index(n)
  271. switch elem.Kind() {
  272. case reflect.Int32:
  273. elem.SetInt(int64(int32(x)))
  274. case reflect.Uint32:
  275. elem.SetUint(uint64(x))
  276. case reflect.Float32:
  277. elem.SetFloat(float64(math.Float32frombits(x)))
  278. }
  279. }
  280. func (p word32Slice) Len() int {
  281. return p.v.Len()
  282. }
  283. func (p word32Slice) Index(i int) uint32 {
  284. elem := p.v.Index(i)
  285. switch elem.Kind() {
  286. case reflect.Int32:
  287. return uint32(elem.Int())
  288. case reflect.Uint32:
  289. return uint32(elem.Uint())
  290. case reflect.Float32:
  291. return math.Float32bits(float32(elem.Float()))
  292. }
  293. panic("unreachable")
  294. }
  295. // Word32Slice returns a reference to a []int32, []uint32, []float32, or []enum field in the struct.
  296. func structPointer_Word32Slice(p structPointer, f field) word32Slice {
  297. return word32Slice{structPointer_field(p, f)}
  298. }
  299. // word64 is like word32 but for 64-bit values.
  300. type word64 struct {
  301. v reflect.Value
  302. }
  303. func word64_Set(p word64, o *Buffer, x uint64) {
  304. t := p.v.Type().Elem()
  305. switch t {
  306. case int64Type:
  307. if len(o.int64s) == 0 {
  308. o.int64s = make([]int64, uint64PoolSize)
  309. }
  310. o.int64s[0] = int64(x)
  311. p.v.Set(reflect.ValueOf(&o.int64s[0]))
  312. o.int64s = o.int64s[1:]
  313. return
  314. case uint64Type:
  315. if len(o.uint64s) == 0 {
  316. o.uint64s = make([]uint64, uint64PoolSize)
  317. }
  318. o.uint64s[0] = x
  319. p.v.Set(reflect.ValueOf(&o.uint64s[0]))
  320. o.uint64s = o.uint64s[1:]
  321. return
  322. case float64Type:
  323. if len(o.float64s) == 0 {
  324. o.float64s = make([]float64, uint64PoolSize)
  325. }
  326. o.float64s[0] = math.Float64frombits(x)
  327. p.v.Set(reflect.ValueOf(&o.float64s[0]))
  328. o.float64s = o.float64s[1:]
  329. return
  330. }
  331. panic("unreachable")
  332. }
  333. func word64_IsNil(p word64) bool {
  334. return p.v.IsNil()
  335. }
  336. func word64_Get(p word64) uint64 {
  337. elem := p.v.Elem()
  338. switch elem.Kind() {
  339. case reflect.Int64:
  340. return uint64(elem.Int())
  341. case reflect.Uint64:
  342. return elem.Uint()
  343. case reflect.Float64:
  344. return math.Float64bits(elem.Float())
  345. }
  346. panic("unreachable")
  347. }
  348. func structPointer_Word64(p structPointer, f field) word64 {
  349. return word64{structPointer_field(p, f)}
  350. }
  351. // word64Val is like word32Val but for 64-bit values.
  352. type word64Val struct {
  353. v reflect.Value
  354. }
  355. func word64Val_Set(p word64Val, o *Buffer, x uint64) {
  356. switch p.v.Type() {
  357. case int64Type:
  358. p.v.SetInt(int64(x))
  359. return
  360. case uint64Type:
  361. p.v.SetUint(x)
  362. return
  363. case float64Type:
  364. p.v.SetFloat(math.Float64frombits(x))
  365. return
  366. }
  367. panic("unreachable")
  368. }
  369. func word64Val_Get(p word64Val) uint64 {
  370. elem := p.v
  371. switch elem.Kind() {
  372. case reflect.Int64:
  373. return uint64(elem.Int())
  374. case reflect.Uint64:
  375. return elem.Uint()
  376. case reflect.Float64:
  377. return math.Float64bits(elem.Float())
  378. }
  379. panic("unreachable")
  380. }
  381. func structPointer_Word64Val(p structPointer, f field) word64Val {
  382. return word64Val{structPointer_field(p, f)}
  383. }
  384. type word64Slice struct {
  385. v reflect.Value
  386. }
  387. func (p word64Slice) Append(x uint64) {
  388. n, m := p.v.Len(), p.v.Cap()
  389. if n < m {
  390. p.v.SetLen(n + 1)
  391. } else {
  392. t := p.v.Type().Elem()
  393. p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
  394. }
  395. elem := p.v.Index(n)
  396. switch elem.Kind() {
  397. case reflect.Int64:
  398. elem.SetInt(int64(int64(x)))
  399. case reflect.Uint64:
  400. elem.SetUint(uint64(x))
  401. case reflect.Float64:
  402. elem.SetFloat(float64(math.Float64frombits(x)))
  403. }
  404. }
  405. func (p word64Slice) Len() int {
  406. return p.v.Len()
  407. }
  408. func (p word64Slice) Index(i int) uint64 {
  409. elem := p.v.Index(i)
  410. switch elem.Kind() {
  411. case reflect.Int64:
  412. return uint64(elem.Int())
  413. case reflect.Uint64:
  414. return uint64(elem.Uint())
  415. case reflect.Float64:
  416. return math.Float64bits(float64(elem.Float()))
  417. }
  418. panic("unreachable")
  419. }
  420. func structPointer_Word64Slice(p structPointer, f field) word64Slice {
  421. return word64Slice{structPointer_field(p, f)}
  422. }