|
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) BytesPtr() **[]byte { return p.v.Interface().(**[]byte) } |
|
125 func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) } |
|
126 func (p pointer) WeakFields() *weakFields { return (*weakFields)(p.v.Interface().(*WeakFields)) } |
|
127 func (p pointer) Extensions() *map[int32]ExtensionField { |
|
128 return p.v.Interface().(*map[int32]ExtensionField) |
|
129 } |
|
130 |
|
131 func (p pointer) Elem() pointer { |
|
132 return pointer{v: p.v.Elem()} |
|
133 } |
|
134 |
|
135 // PointerSlice copies []*T from p as a new []pointer. |
|
136 // This behavior differs from the implementation in pointer_unsafe.go. |
|
137 func (p pointer) PointerSlice() []pointer { |
|
138 // TODO: reconsider this |
|
139 if p.v.IsNil() { |
|
140 return nil |
|
141 } |
|
142 n := p.v.Elem().Len() |
|
143 s := make([]pointer, n) |
|
144 for i := 0; i < n; i++ { |
|
145 s[i] = pointer{v: p.v.Elem().Index(i)} |
|
146 } |
|
147 return s |
|
148 } |
|
149 |
|
150 // AppendPointerSlice appends v to p, which must be a []*T. |
|
151 func (p pointer) AppendPointerSlice(v pointer) { |
|
152 sp := p.v.Elem() |
|
153 sp.Set(reflect.Append(sp, v.v)) |
|
154 } |
|
155 |
|
156 // SetPointer sets *p to v. |
|
157 func (p pointer) SetPointer(v pointer) { |
|
158 p.v.Elem().Set(v.v) |
|
159 } |
|
160 |
|
161 func (Export) MessageStateOf(p Pointer) *messageState { panic("not supported") } |
|
162 func (ms *messageState) pointer() pointer { panic("not supported") } |
|
163 func (ms *messageState) messageInfo() *MessageInfo { panic("not supported") } |
|
164 func (ms *messageState) LoadMessageInfo() *MessageInfo { panic("not supported") } |
|
165 func (ms *messageState) StoreMessageInfo(mi *MessageInfo) { panic("not supported") } |
|
166 |
|
167 type atomicNilMessage struct { |
|
168 once sync.Once |
|
169 m messageReflectWrapper |
|
170 } |
|
171 |
|
172 func (m *atomicNilMessage) Init(mi *MessageInfo) *messageReflectWrapper { |
|
173 m.once.Do(func() { |
|
174 m.m.p = pointerOfIface(reflect.Zero(mi.GoReflectType).Interface()) |
|
175 m.m.mi = mi |
|
176 }) |
|
177 return &m.m |
|
178 } |