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