1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2012 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
32 // +build appengine js
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.
45 // A structPointer is a pointer to a struct.
46 type structPointer struct {
50 // toStructPointer returns a structPointer equivalent to the given reflect value.
51 // The reflect value must itself be a pointer to a struct.
52 func toStructPointer(v reflect.Value) structPointer {
53 return structPointer{v}
56 // IsNil reports whether p is nil.
57 func structPointer_IsNil(p structPointer) bool {
61 // Interface returns the struct pointer as an interface value.
62 func structPointer_Interface(p structPointer, _ reflect.Type) interface{} {
63 return p.v.Interface()
66 // A field identifies a field in a struct, accessible from a structPointer.
67 // In this implementation, a field is identified by the sequence of field indices
68 // passed to reflect's FieldByIndex.
71 // toField returns a field equivalent to the given reflect field.
72 func toField(f *reflect.StructField) field {
76 // invalidField is an invalid field identifier.
77 var invalidField = field(nil)
79 // IsValid reports whether the field identifier is valid.
80 func (f field) IsValid() bool { return f != nil }
82 // field returns the given field in the struct as a reflect value.
83 func structPointer_field(p structPointer, f field) reflect.Value {
84 // Special case: an extension map entry with a value of type T
85 // passes a *T to the struct-handling code with a zero field,
86 // expecting that it will be treated as equivalent to *struct{ X T },
87 // which has the same memory layout. We have to handle that case
88 // specially, because reflect will panic if we call FieldByIndex on a
94 return p.v.Elem().FieldByIndex(f)
97 // ifield returns the given field in the struct as an interface value.
98 func structPointer_ifield(p structPointer, f field) interface{} {
99 return structPointer_field(p, f).Addr().Interface()
102 // Bytes returns the address of a []byte field in the struct.
103 func structPointer_Bytes(p structPointer, f field) *[]byte {
104 return structPointer_ifield(p, f).(*[]byte)
107 // BytesSlice returns the address of a [][]byte field in the struct.
108 func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
109 return structPointer_ifield(p, f).(*[][]byte)
112 // Bool returns the address of a *bool field in the struct.
113 func structPointer_Bool(p structPointer, f field) **bool {
114 return structPointer_ifield(p, f).(**bool)
117 // BoolVal returns the address of a bool field in the struct.
118 func structPointer_BoolVal(p structPointer, f field) *bool {
119 return structPointer_ifield(p, f).(*bool)
122 // BoolSlice returns the address of a []bool field in the struct.
123 func structPointer_BoolSlice(p structPointer, f field) *[]bool {
124 return structPointer_ifield(p, f).(*[]bool)
127 // String returns the address of a *string field in the struct.
128 func structPointer_String(p structPointer, f field) **string {
129 return structPointer_ifield(p, f).(**string)
132 // StringVal returns the address of a string field in the struct.
133 func structPointer_StringVal(p structPointer, f field) *string {
134 return structPointer_ifield(p, f).(*string)
137 // StringSlice returns the address of a []string field in the struct.
138 func structPointer_StringSlice(p structPointer, f field) *[]string {
139 return structPointer_ifield(p, f).(*[]string)
142 // Extensions returns the address of an extension map field in the struct.
143 func structPointer_Extensions(p structPointer, f field) *XXX_InternalExtensions {
144 return structPointer_ifield(p, f).(*XXX_InternalExtensions)
147 // ExtMap returns the address of an extension map field in the struct.
148 func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
149 return structPointer_ifield(p, f).(*map[int32]Extension)
152 // NewAt returns the reflect.Value for a pointer to a field in the struct.
153 func structPointer_NewAt(p structPointer, f field, typ reflect.Type) reflect.Value {
154 return structPointer_field(p, f).Addr()
157 // SetStructPointer writes a *struct field in the struct.
158 func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
159 structPointer_field(p, f).Set(q.v)
162 // GetStructPointer reads a *struct field in the struct.
163 func structPointer_GetStructPointer(p structPointer, f field) structPointer {
164 return structPointer{structPointer_field(p, f)}
167 // StructPointerSlice the address of a []*struct field in the struct.
168 func structPointer_StructPointerSlice(p structPointer, f field) structPointerSlice {
169 return structPointerSlice{structPointer_field(p, f)}
172 // A structPointerSlice represents the address of a slice of pointers to structs
173 // (themselves messages or groups). That is, v.Type() is *[]*struct{...}.
174 type structPointerSlice struct {
178 func (p structPointerSlice) Len() int { return p.v.Len() }
179 func (p structPointerSlice) Index(i int) structPointer { return structPointer{p.v.Index(i)} }
180 func (p structPointerSlice) Append(q structPointer) {
181 p.v.Set(reflect.Append(p.v, q.v))
185 int32Type = reflect.TypeOf(int32(0))
186 uint32Type = reflect.TypeOf(uint32(0))
187 float32Type = reflect.TypeOf(float32(0))
188 int64Type = reflect.TypeOf(int64(0))
189 uint64Type = reflect.TypeOf(uint64(0))
190 float64Type = reflect.TypeOf(float64(0))
193 // A word32 represents a field of type *int32, *uint32, *float32, or *enum.
194 // That is, v.Type() is *int32, *uint32, *float32, or *enum and v is assignable.
199 // IsNil reports whether p is nil.
200 func word32_IsNil(p word32) bool {
204 // Set sets p to point at a newly allocated word with bits set to x.
205 func word32_Set(p word32, o *Buffer, x uint32) {
206 t := p.v.Type().Elem()
209 if len(o.int32s) == 0 {
210 o.int32s = make([]int32, uint32PoolSize)
212 o.int32s[0] = int32(x)
213 p.v.Set(reflect.ValueOf(&o.int32s[0]))
214 o.int32s = o.int32s[1:]
217 if len(o.uint32s) == 0 {
218 o.uint32s = make([]uint32, uint32PoolSize)
221 p.v.Set(reflect.ValueOf(&o.uint32s[0]))
222 o.uint32s = o.uint32s[1:]
225 if len(o.float32s) == 0 {
226 o.float32s = make([]float32, uint32PoolSize)
228 o.float32s[0] = math.Float32frombits(x)
229 p.v.Set(reflect.ValueOf(&o.float32s[0]))
230 o.float32s = o.float32s[1:]
235 p.v.Set(reflect.New(t))
236 p.v.Elem().SetInt(int64(int32(x)))
239 // Get gets the bits pointed at by p, as a uint32.
240 func word32_Get(p word32) uint32 {
244 return uint32(elem.Int())
246 return uint32(elem.Uint())
247 case reflect.Float32:
248 return math.Float32bits(float32(elem.Float()))
253 // Word32 returns a reference to a *int32, *uint32, *float32, or *enum field in the struct.
254 func structPointer_Word32(p structPointer, f field) word32 {
255 return word32{structPointer_field(p, f)}
258 // A word32Val represents a field of type int32, uint32, float32, or enum.
259 // That is, v.Type() is int32, uint32, float32, or enum and v is assignable.
260 type word32Val struct {
265 func word32Val_Set(p word32Val, x uint32) {
271 p.v.SetUint(uint64(x))
274 p.v.SetFloat(float64(math.Float32frombits(x)))
279 p.v.SetInt(int64(int32(x)))
282 // Get gets the bits pointed at by p, as a uint32.
283 func word32Val_Get(p word32Val) uint32 {
287 return uint32(elem.Int())
289 return uint32(elem.Uint())
290 case reflect.Float32:
291 return math.Float32bits(float32(elem.Float()))
296 // Word32Val returns a reference to a int32, uint32, float32, or enum field in the struct.
297 func structPointer_Word32Val(p structPointer, f field) word32Val {
298 return word32Val{structPointer_field(p, f)}
301 // A word32Slice is a slice of 32-bit values.
302 // That is, v.Type() is []int32, []uint32, []float32, or []enum.
303 type word32Slice struct {
307 func (p word32Slice) Append(x uint32) {
308 n, m := p.v.Len(), p.v.Cap()
312 t := p.v.Type().Elem()
313 p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
318 elem.SetInt(int64(int32(x)))
320 elem.SetUint(uint64(x))
321 case reflect.Float32:
322 elem.SetFloat(float64(math.Float32frombits(x)))
326 func (p word32Slice) Len() int {
330 func (p word32Slice) Index(i int) uint32 {
334 return uint32(elem.Int())
336 return uint32(elem.Uint())
337 case reflect.Float32:
338 return math.Float32bits(float32(elem.Float()))
343 // Word32Slice returns a reference to a []int32, []uint32, []float32, or []enum field in the struct.
344 func structPointer_Word32Slice(p structPointer, f field) word32Slice {
345 return word32Slice{structPointer_field(p, f)}
348 // word64 is like word32 but for 64-bit values.
353 func word64_Set(p word64, o *Buffer, x uint64) {
354 t := p.v.Type().Elem()
357 if len(o.int64s) == 0 {
358 o.int64s = make([]int64, uint64PoolSize)
360 o.int64s[0] = int64(x)
361 p.v.Set(reflect.ValueOf(&o.int64s[0]))
362 o.int64s = o.int64s[1:]
365 if len(o.uint64s) == 0 {
366 o.uint64s = make([]uint64, uint64PoolSize)
369 p.v.Set(reflect.ValueOf(&o.uint64s[0]))
370 o.uint64s = o.uint64s[1:]
373 if len(o.float64s) == 0 {
374 o.float64s = make([]float64, uint64PoolSize)
376 o.float64s[0] = math.Float64frombits(x)
377 p.v.Set(reflect.ValueOf(&o.float64s[0]))
378 o.float64s = o.float64s[1:]
384 func word64_IsNil(p word64) bool {
388 func word64_Get(p word64) uint64 {
392 return uint64(elem.Int())
395 case reflect.Float64:
396 return math.Float64bits(elem.Float())
401 func structPointer_Word64(p structPointer, f field) word64 {
402 return word64{structPointer_field(p, f)}
405 // word64Val is like word32Val but for 64-bit values.
406 type word64Val struct {
410 func word64Val_Set(p word64Val, o *Buffer, x uint64) {
419 p.v.SetFloat(math.Float64frombits(x))
425 func word64Val_Get(p word64Val) uint64 {
429 return uint64(elem.Int())
432 case reflect.Float64:
433 return math.Float64bits(elem.Float())
438 func structPointer_Word64Val(p structPointer, f field) word64Val {
439 return word64Val{structPointer_field(p, f)}
442 type word64Slice struct {
446 func (p word64Slice) Append(x uint64) {
447 n, m := p.v.Len(), p.v.Cap()
451 t := p.v.Type().Elem()
452 p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
457 elem.SetInt(int64(int64(x)))
459 elem.SetUint(uint64(x))
460 case reflect.Float64:
461 elem.SetFloat(float64(math.Float64frombits(x)))
465 func (p word64Slice) Len() int {
469 func (p word64Slice) Index(i int) uint64 {
473 return uint64(elem.Int())
475 return uint64(elem.Uint())
476 case reflect.Float64:
477 return math.Float64bits(float64(elem.Float()))
482 func structPointer_Word64Slice(p structPointer, f field) word64Slice {
483 return word64Slice{structPointer_field(p, f)}