podman
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
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 proto39
40import (41"reflect"42"sync"43)
44
45const unsafeAllowed = false46
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 []int51
52// toField returns a field equivalent to the given reflect field.
53func toField(f *reflect.StructField) field {54return f.Index55}
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 {71v reflect.Value72}
73
74// toPointer converts an interface of pointer type to a pointer
75// that points to the same target.
76func toPointer(i *Message) pointer {77return 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 bool) pointer {83v := reflect.ValueOf(*i)84u := reflect.New(v.Type())85u.Elem().Set(v)86return pointer{v: u}87}
88
89// valToPointer converts v to a pointer. v must be of pointer type.
90func valToPointer(v reflect.Value) pointer {91return pointer{v: v}92}
93
94// offset converts from a pointer to a structure to a pointer to
95// one of its fields.
96func (p pointer) offset(f field) pointer {97return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}98}
99
100func (p pointer) isNil() bool {101return p.v.IsNil()102}
103
104// grow updates the slice s in place to make it one element longer.
105// s must be addressable.
106// Returns the (addressable) new element.
107func grow(s reflect.Value) reflect.Value {108n, m := s.Len(), s.Cap()109if n < m {110s.SetLen(n + 1)111} else {112s.Set(reflect.Append(s, reflect.Zero(s.Type().Elem())))113}114return s.Index(n)115}
116
117func (p pointer) toInt64() *int64 {118return p.v.Interface().(*int64)119}
120func (p pointer) toInt64Ptr() **int64 {121return p.v.Interface().(**int64)122}
123func (p pointer) toInt64Slice() *[]int64 {124return p.v.Interface().(*[]int64)125}
126
127var int32ptr = reflect.TypeOf((*int32)(nil))128
129func (p pointer) toInt32() *int32 {130return p.v.Convert(int32ptr).Interface().(*int32)131}
132
133// The toInt32Ptr/Slice methods don't work because of enums.
134// Instead, we must use set/get methods for the int32ptr/slice case.
135/*
136func (p pointer) toInt32Ptr() **int32 {
137return p.v.Interface().(**int32)
138}
139func (p pointer) toInt32Slice() *[]int32 {
140return p.v.Interface().(*[]int32)
141}
142*/
143func (p pointer) getInt32Ptr() *int32 {144if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {145// raw int32 type146return p.v.Elem().Interface().(*int32)147}148// an enum149return p.v.Elem().Convert(int32PtrType).Interface().(*int32)150}
151func (p pointer) setInt32Ptr(v int32) {152// Allocate value in a *int32. Possibly convert that to a *enum.153// Then assign it to a **int32 or **enum.154// Note: we can convert *int32 to *enum, but we can't convert155// **int32 to **enum!156p.v.Elem().Set(reflect.ValueOf(&v).Convert(p.v.Type().Elem()))157}
158
159// getInt32Slice copies []int32 from p as a new slice.
160// This behavior differs from the implementation in pointer_unsafe.go.
161func (p pointer) getInt32Slice() []int32 {162if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {163// raw int32 type164return p.v.Elem().Interface().([]int32)165}166// an enum167// Allocate a []int32, then assign []enum's values into it.168// Note: we can't convert []enum to []int32.169slice := p.v.Elem()170s := make([]int32, slice.Len())171for i := 0; i < slice.Len(); i++ {172s[i] = int32(slice.Index(i).Int())173}174return s175}
176
177// setInt32Slice copies []int32 into p as a new slice.
178// This behavior differs from the implementation in pointer_unsafe.go.
179func (p pointer) setInt32Slice(v []int32) {180if p.v.Type().Elem().Elem() == reflect.TypeOf(int32(0)) {181// raw int32 type182p.v.Elem().Set(reflect.ValueOf(v))183return184}185// an enum186// Allocate a []enum, then assign []int32's values into it.187// Note: we can't convert []enum to []int32.188slice := reflect.MakeSlice(p.v.Type().Elem(), len(v), cap(v))189for i, x := range v {190slice.Index(i).SetInt(int64(x))191}192p.v.Elem().Set(slice)193}
194func (p pointer) appendInt32Slice(v int32) {195grow(p.v.Elem()).SetInt(int64(v))196}
197
198func (p pointer) toUint64() *uint64 {199return p.v.Interface().(*uint64)200}
201func (p pointer) toUint64Ptr() **uint64 {202return p.v.Interface().(**uint64)203}
204func (p pointer) toUint64Slice() *[]uint64 {205return p.v.Interface().(*[]uint64)206}
207func (p pointer) toUint32() *uint32 {208return p.v.Interface().(*uint32)209}
210func (p pointer) toUint32Ptr() **uint32 {211return p.v.Interface().(**uint32)212}
213func (p pointer) toUint32Slice() *[]uint32 {214return p.v.Interface().(*[]uint32)215}
216func (p pointer) toBool() *bool {217return p.v.Interface().(*bool)218}
219func (p pointer) toBoolPtr() **bool {220return p.v.Interface().(**bool)221}
222func (p pointer) toBoolSlice() *[]bool {223return p.v.Interface().(*[]bool)224}
225func (p pointer) toFloat64() *float64 {226return p.v.Interface().(*float64)227}
228func (p pointer) toFloat64Ptr() **float64 {229return p.v.Interface().(**float64)230}
231func (p pointer) toFloat64Slice() *[]float64 {232return p.v.Interface().(*[]float64)233}
234func (p pointer) toFloat32() *float32 {235return p.v.Interface().(*float32)236}
237func (p pointer) toFloat32Ptr() **float32 {238return p.v.Interface().(**float32)239}
240func (p pointer) toFloat32Slice() *[]float32 {241return p.v.Interface().(*[]float32)242}
243func (p pointer) toString() *string {244return p.v.Interface().(*string)245}
246func (p pointer) toStringPtr() **string {247return p.v.Interface().(**string)248}
249func (p pointer) toStringSlice() *[]string {250return p.v.Interface().(*[]string)251}
252func (p pointer) toBytes() *[]byte {253return p.v.Interface().(*[]byte)254}
255func (p pointer) toBytesSlice() *[][]byte {256return p.v.Interface().(*[][]byte)257}
258func (p pointer) toExtensions() *XXX_InternalExtensions {259return p.v.Interface().(*XXX_InternalExtensions)260}
261func (p pointer) toOldExtensions() *map[int32]Extension {262return p.v.Interface().(*map[int32]Extension)263}
264func (p pointer) getPointer() pointer {265return pointer{v: p.v.Elem()}266}
267func (p pointer) setPointer(q pointer) {268p.v.Elem().Set(q.v)269}
270func (p pointer) appendPointer(q pointer) {271grow(p.v.Elem()).Set(q.v)272}
273
274// getPointerSlice copies []*T from p as a new []pointer.
275// This behavior differs from the implementation in pointer_unsafe.go.
276func (p pointer) getPointerSlice() []pointer {277if p.v.IsNil() {278return nil279}280n := p.v.Elem().Len()281s := make([]pointer, n)282for i := 0; i < n; i++ {283s[i] = pointer{v: p.v.Elem().Index(i)}284}285return s286}
287
288// setPointerSlice copies []pointer into p as a new []*T.
289// This behavior differs from the implementation in pointer_unsafe.go.
290func (p pointer) setPointerSlice(v []pointer) {291if v == nil {292p.v.Elem().Set(reflect.New(p.v.Elem().Type()).Elem())293return294}295s := reflect.MakeSlice(p.v.Elem().Type(), 0, len(v))296for _, p := range v {297s = reflect.Append(s, p.v)298}299p.v.Elem().Set(s)300}
301
302// getInterfacePointer returns a pointer that points to the
303// interface data of the interface pointed by p.
304func (p pointer) getInterfacePointer() pointer {305if p.v.Elem().IsNil() {306return pointer{v: p.v.Elem()}307}308return pointer{v: p.v.Elem().Elem().Elem().Field(0).Addr()} // *interface -> interface -> *struct -> struct309}
310
311func (p pointer) asPointerTo(t reflect.Type) reflect.Value {312// TODO: check that p.v.Type().Elem() == t?313return p.v314}
315
316func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {317atomicLock.Lock()318defer atomicLock.Unlock()319return *p320}
321func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {322atomicLock.Lock()323defer atomicLock.Unlock()324*p = v325}
326func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {327atomicLock.Lock()328defer atomicLock.Unlock()329return *p330}
331func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {332atomicLock.Lock()333defer atomicLock.Unlock()334*p = v335}
336func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {337atomicLock.Lock()338defer atomicLock.Unlock()339return *p340}
341func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {342atomicLock.Lock()343defer atomicLock.Unlock()344*p = v345}
346func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {347atomicLock.Lock()348defer atomicLock.Unlock()349return *p350}
351func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {352atomicLock.Lock()353defer atomicLock.Unlock()354*p = v355}
356
357var atomicLock sync.Mutex358