pointer_reflect.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2018 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 appengine
  5. package impl
  6. import (
  7. "fmt"
  8. "reflect"
  9. "sync"
  10. )
  11. const UnsafeEnabled = false
  12. // Pointer is an opaque pointer type.
  13. type Pointer interface{}
  14. // offset represents the offset to a struct field, accessible from a pointer.
  15. // The offset is the field index into a struct.
  16. type offset struct {
  17. index int
  18. export exporter
  19. }
  20. // offsetOf returns a field offset for the struct field.
  21. func offsetOf(f reflect.StructField, x exporter) offset {
  22. if len(f.Index) != 1 {
  23. panic("embedded structs are not supported")
  24. }
  25. if f.PkgPath == "" {
  26. return offset{index: f.Index[0]} // field is already exported
  27. }
  28. if x == nil {
  29. panic("exporter must be provided for unexported field")
  30. }
  31. return offset{index: f.Index[0], export: x}
  32. }
  33. // IsValid reports whether the offset is valid.
  34. func (f offset) IsValid() bool { return f.index >= 0 }
  35. // invalidOffset is an invalid field offset.
  36. var invalidOffset = offset{index: -1}
  37. // zeroOffset is a noop when calling pointer.Apply.
  38. var zeroOffset = offset{index: 0}
  39. // pointer is an abstract representation of a pointer to a struct or field.
  40. type pointer struct{ v reflect.Value }
  41. // pointerOf returns p as a pointer.
  42. func pointerOf(p Pointer) pointer {
  43. return pointerOfIface(p)
  44. }
  45. // pointerOfValue returns v as a pointer.
  46. func pointerOfValue(v reflect.Value) pointer {
  47. return pointer{v: v}
  48. }
  49. // pointerOfIface returns the pointer portion of an interface.
  50. func pointerOfIface(v interface{}) pointer {
  51. return pointer{v: reflect.ValueOf(v)}
  52. }
  53. // IsNil reports whether the pointer is nil.
  54. func (p pointer) IsNil() bool {
  55. return p.v.IsNil()
  56. }
  57. // Apply adds an offset to the pointer to derive a new pointer
  58. // to a specified field. The current pointer must be pointing at a struct.
  59. func (p pointer) Apply(f offset) pointer {
  60. if f.export != nil {
  61. if v := reflect.ValueOf(f.export(p.v.Interface(), f.index)); v.IsValid() {
  62. return pointer{v: v}
  63. }
  64. }
  65. return pointer{v: p.v.Elem().Field(f.index).Addr()}
  66. }
  67. // AsValueOf treats p as a pointer to an object of type t and returns the value.
  68. // It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
  69. func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
  70. if got := p.v.Type().Elem(); got != t {
  71. panic(fmt.Sprintf("invalid type: got %v, want %v", got, t))
  72. }
  73. return p.v
  74. }
  75. // AsIfaceOf treats p as a pointer to an object of type t and returns the value.
  76. // It is equivalent to p.AsValueOf(t).Interface()
  77. func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
  78. return p.AsValueOf(t).Interface()
  79. }
  80. func (p pointer) Bool() *bool { return p.v.Interface().(*bool) }
  81. func (p pointer) BoolPtr() **bool { return p.v.Interface().(**bool) }
  82. func (p pointer) BoolSlice() *[]bool { return p.v.Interface().(*[]bool) }
  83. func (p pointer) Int32() *int32 { return p.v.Interface().(*int32) }
  84. func (p pointer) Int32Ptr() **int32 { return p.v.Interface().(**int32) }
  85. func (p pointer) Int32Slice() *[]int32 { return p.v.Interface().(*[]int32) }
  86. func (p pointer) Int64() *int64 { return p.v.Interface().(*int64) }
  87. func (p pointer) Int64Ptr() **int64 { return p.v.Interface().(**int64) }
  88. func (p pointer) Int64Slice() *[]int64 { return p.v.Interface().(*[]int64) }
  89. func (p pointer) Uint32() *uint32 { return p.v.Interface().(*uint32) }
  90. func (p pointer) Uint32Ptr() **uint32 { return p.v.Interface().(**uint32) }
  91. func (p pointer) Uint32Slice() *[]uint32 { return p.v.Interface().(*[]uint32) }
  92. func (p pointer) Uint64() *uint64 { return p.v.Interface().(*uint64) }
  93. func (p pointer) Uint64Ptr() **uint64 { return p.v.Interface().(**uint64) }
  94. func (p pointer) Uint64Slice() *[]uint64 { return p.v.Interface().(*[]uint64) }
  95. func (p pointer) Float32() *float32 { return p.v.Interface().(*float32) }
  96. func (p pointer) Float32Ptr() **float32 { return p.v.Interface().(**float32) }
  97. func (p pointer) Float32Slice() *[]float32 { return p.v.Interface().(*[]float32) }
  98. func (p pointer) Float64() *float64 { return p.v.Interface().(*float64) }
  99. func (p pointer) Float64Ptr() **float64 { return p.v.Interface().(**float64) }
  100. func (p pointer) Float64Slice() *[]float64 { return p.v.Interface().(*[]float64) }
  101. func (p pointer) String() *string { return p.v.Interface().(*string) }
  102. func (p pointer) StringPtr() **string { return p.v.Interface().(**string) }
  103. func (p pointer) StringSlice() *[]string { return p.v.Interface().(*[]string) }
  104. func (p pointer) Bytes() *[]byte { return p.v.Interface().(*[]byte) }
  105. func (p pointer) BytesPtr() **[]byte { return p.v.Interface().(**[]byte) }
  106. func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) }
  107. func (p pointer) WeakFields() *weakFields { return (*weakFields)(p.v.Interface().(*WeakFields)) }
  108. func (p pointer) Extensions() *map[int32]ExtensionField {
  109. return p.v.Interface().(*map[int32]ExtensionField)
  110. }
  111. func (p pointer) Elem() pointer {
  112. return pointer{v: p.v.Elem()}
  113. }
  114. // PointerSlice copies []*T from p as a new []pointer.
  115. // This behavior differs from the implementation in pointer_unsafe.go.
  116. func (p pointer) PointerSlice() []pointer {
  117. // TODO: reconsider this
  118. if p.v.IsNil() {
  119. return nil
  120. }
  121. n := p.v.Elem().Len()
  122. s := make([]pointer, n)
  123. for i := 0; i < n; i++ {
  124. s[i] = pointer{v: p.v.Elem().Index(i)}
  125. }
  126. return s
  127. }
  128. // AppendPointerSlice appends v to p, which must be a []*T.
  129. func (p pointer) AppendPointerSlice(v pointer) {
  130. sp := p.v.Elem()
  131. sp.Set(reflect.Append(sp, v.v))
  132. }
  133. // SetPointer sets *p to v.
  134. func (p pointer) SetPointer(v pointer) {
  135. p.v.Elem().Set(v.v)
  136. }
  137. func (Export) MessageStateOf(p Pointer) *messageState { panic("not supported") }
  138. func (ms *messageState) pointer() pointer { panic("not supported") }
  139. func (ms *messageState) messageInfo() *MessageInfo { panic("not supported") }
  140. func (ms *messageState) LoadMessageInfo() *MessageInfo { panic("not supported") }
  141. func (ms *messageState) StoreMessageInfo(mi *MessageInfo) { panic("not supported") }
  142. type atomicNilMessage struct {
  143. once sync.Once
  144. m messageReflectWrapper
  145. }
  146. func (m *atomicNilMessage) Init(mi *MessageInfo) *messageReflectWrapper {
  147. m.once.Do(func() {
  148. m.m.p = pointerOfIface(reflect.Zero(mi.GoReflectType).Interface())
  149. m.m.mi = mi
  150. })
  151. return &m.m
  152. }