Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [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 | |
Joe Tsai | bc534a9 | 2018-12-18 16:07:48 -0800 | [diff] [blame] | 5 | // +build purego appengine |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 6 | |
| 7 | package impl |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | "reflect" |
| 12 | ) |
| 13 | |
| 14 | // offset represents the offset to a struct field, accessible from a pointer. |
| 15 | // The offset is the field index into a struct. |
| 16 | type offset []int |
| 17 | |
| 18 | // offsetOf returns a field offset for the struct field. |
| 19 | func offsetOf(f reflect.StructField) offset { |
| 20 | if len(f.Index) != 1 { |
| 21 | panic("embedded structs are not supported") |
| 22 | } |
| 23 | return f.Index |
| 24 | } |
| 25 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 26 | // IsValid reports whether the offset is valid. |
| 27 | func (f offset) IsValid() bool { return f != nil } |
| 28 | |
| 29 | // invalidOffset is an invalid field offset. |
| 30 | var invalidOffset = offset(nil) |
| 31 | |
| 32 | // zeroOffset is a noop when calling pointer.Apply. |
| 33 | var zeroOffset = offset([]int{0}) |
| 34 | |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 35 | // pointer is an abstract representation of a pointer to a struct or field. |
| 36 | type pointer struct{ v reflect.Value } |
| 37 | |
| 38 | // pointerOfValue returns v as a pointer. |
| 39 | func pointerOfValue(v reflect.Value) pointer { |
| 40 | return pointer{v: v} |
| 41 | } |
| 42 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 43 | // pointerOfIface returns the pointer portion of an interface. |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 44 | func pointerOfIface(v interface{}) pointer { |
| 45 | return pointer{v: reflect.ValueOf(v)} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 48 | // IsNil reports whether the pointer is nil. |
| 49 | func (p pointer) IsNil() bool { |
| 50 | return p.v.IsNil() |
| 51 | } |
| 52 | |
| 53 | // Apply adds an offset to the pointer to derive a new pointer |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 54 | // to a specified field. The current pointer must be pointing at a struct. |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 55 | func (p pointer) Apply(f offset) pointer { |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 56 | // TODO: Handle unexported fields in an API that hides XXX fields? |
| 57 | return pointer{v: p.v.Elem().FieldByIndex(f).Addr()} |
| 58 | } |
| 59 | |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 60 | // AsValueOf treats p as a pointer to an object of type t and returns the value. |
| 61 | // It is equivalent to reflect.ValueOf(p.AsIfaceOf(t)) |
| 62 | func (p pointer) AsValueOf(t reflect.Type) reflect.Value { |
Damien Neil | 927aaba | 2019-05-09 12:18:44 -0700 | [diff] [blame] | 63 | if got := p.v.Type().Elem(); got != t { |
| 64 | panic(fmt.Sprintf("invalid type: got %v, want %v", got, t)) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 65 | } |
| 66 | return p.v |
| 67 | } |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 68 | |
| 69 | // AsIfaceOf treats p as a pointer to an object of type t and returns the value. |
| 70 | // It is equivalent to p.AsValueOf(t).Interface() |
| 71 | func (p pointer) AsIfaceOf(t reflect.Type) interface{} { |
| 72 | return p.AsValueOf(t).Interface() |
| 73 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 74 | |
| 75 | func (p pointer) Bool() *bool { return p.v.Interface().(*bool) } |
| 76 | func (p pointer) BoolPtr() **bool { return p.v.Interface().(**bool) } |
| 77 | func (p pointer) BoolSlice() *[]bool { return p.v.Interface().(*[]bool) } |
| 78 | func (p pointer) Int32() *int32 { return p.v.Interface().(*int32) } |
| 79 | func (p pointer) Int32Ptr() **int32 { return p.v.Interface().(**int32) } |
| 80 | func (p pointer) Int32Slice() *[]int32 { return p.v.Interface().(*[]int32) } |
| 81 | func (p pointer) Int64() *int64 { return p.v.Interface().(*int64) } |
| 82 | func (p pointer) Int64Ptr() **int64 { return p.v.Interface().(**int64) } |
| 83 | func (p pointer) Int64Slice() *[]int64 { return p.v.Interface().(*[]int64) } |
| 84 | func (p pointer) Uint32() *uint32 { return p.v.Interface().(*uint32) } |
| 85 | func (p pointer) Uint32Ptr() **uint32 { return p.v.Interface().(**uint32) } |
| 86 | func (p pointer) Uint32Slice() *[]uint32 { return p.v.Interface().(*[]uint32) } |
| 87 | func (p pointer) Uint64() *uint64 { return p.v.Interface().(*uint64) } |
| 88 | func (p pointer) Uint64Ptr() **uint64 { return p.v.Interface().(**uint64) } |
| 89 | func (p pointer) Uint64Slice() *[]uint64 { return p.v.Interface().(*[]uint64) } |
| 90 | func (p pointer) Float32() *float32 { return p.v.Interface().(*float32) } |
| 91 | func (p pointer) Float32Ptr() **float32 { return p.v.Interface().(**float32) } |
| 92 | func (p pointer) Float32Slice() *[]float32 { return p.v.Interface().(*[]float32) } |
| 93 | func (p pointer) Float64() *float64 { return p.v.Interface().(*float64) } |
| 94 | func (p pointer) Float64Ptr() **float64 { return p.v.Interface().(**float64) } |
| 95 | func (p pointer) Float64Slice() *[]float64 { return p.v.Interface().(*[]float64) } |
| 96 | func (p pointer) String() *string { return p.v.Interface().(*string) } |
| 97 | func (p pointer) StringPtr() **string { return p.v.Interface().(**string) } |
| 98 | func (p pointer) StringSlice() *[]string { return p.v.Interface().(*[]string) } |
| 99 | func (p pointer) Bytes() *[]byte { return p.v.Interface().(*[]byte) } |
| 100 | func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) } |
| 101 | func (p pointer) Extensions() *legacyExtensionMap { |
| 102 | return (*legacyExtensionMap)(p.v.Interface().(*map[int32]ExtensionFieldV1)) |
| 103 | } |
| 104 | |
| 105 | func (p pointer) Elem() pointer { |
| 106 | return pointer{v: p.v.Elem()} |
| 107 | } |
| 108 | |
| 109 | // PointerSlice copies []*T from p as a new []pointer. |
| 110 | // This behavior differs from the implementation in pointer_unsafe.go. |
| 111 | func (p pointer) PointerSlice() []pointer { |
| 112 | // TODO: reconsider this |
| 113 | if p.v.IsNil() { |
| 114 | return nil |
| 115 | } |
| 116 | n := p.v.Elem().Len() |
| 117 | s := make([]pointer, n) |
| 118 | for i := 0; i < n; i++ { |
| 119 | s[i] = pointer{v: p.v.Elem().Index(i)} |
| 120 | } |
| 121 | return s |
| 122 | } |