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