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