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 | "reflect" |
| 11 | "unsafe" |
| 12 | ) |
| 13 | |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 14 | const UnsafeEnabled = true |
| 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 byte offset to the field from the start of the struct. |
| 18 | type offset uintptr |
| 19 | |
| 20 | // offsetOf returns a field offset for the struct field. |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 21 | func offsetOf(f reflect.StructField, x exporter) offset { |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 22 | return offset(f.Offset) |
| 23 | } |
| 24 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 25 | // IsValid reports whether the offset is valid. |
| 26 | func (f offset) IsValid() bool { return f != invalidOffset } |
| 27 | |
| 28 | // invalidOffset is an invalid field offset. |
| 29 | var invalidOffset = ^offset(0) |
| 30 | |
| 31 | // zeroOffset is a noop when calling pointer.Apply. |
| 32 | var zeroOffset = offset(0) |
| 33 | |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 34 | // pointer is a pointer to a message struct or field. |
| 35 | type pointer struct{ p unsafe.Pointer } |
| 36 | |
| 37 | // pointerOfValue returns v as a pointer. |
| 38 | func pointerOfValue(v reflect.Value) pointer { |
| 39 | return pointer{p: unsafe.Pointer(v.Pointer())} |
| 40 | } |
| 41 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 42 | // pointerOfIface returns the pointer portion of an interface. |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 43 | func pointerOfIface(v interface{}) pointer { |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 44 | type ifaceHeader struct { |
| 45 | Type unsafe.Pointer |
| 46 | Data unsafe.Pointer |
| 47 | } |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 48 | return pointer{p: (*ifaceHeader)(unsafe.Pointer(&v)).Data} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 51 | // IsNil reports whether the pointer is nil. |
| 52 | func (p pointer) IsNil() bool { |
| 53 | return p.p == nil |
| 54 | } |
| 55 | |
| 56 | // Apply adds an offset to the pointer to derive a new pointer |
| 57 | // to a specified field. The pointer must be valid and pointing at a struct. |
| 58 | func (p pointer) Apply(f offset) pointer { |
| 59 | if p.IsNil() { |
| 60 | panic("invalid nil pointer") |
| 61 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 62 | return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))} |
| 63 | } |
| 64 | |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 65 | // AsValueOf treats p as a pointer to an object of type t and returns the value. |
| 66 | // It is equivalent to reflect.ValueOf(p.AsIfaceOf(t)) |
| 67 | func (p pointer) AsValueOf(t reflect.Type) reflect.Value { |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 68 | return reflect.NewAt(t, p.p) |
| 69 | } |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 70 | |
| 71 | // AsIfaceOf treats p as a pointer to an object of type t and returns the value. |
| 72 | // It is equivalent to p.AsValueOf(t).Interface() |
| 73 | func (p pointer) AsIfaceOf(t reflect.Type) interface{} { |
| 74 | // TODO: Use tricky unsafe magic to directly create ifaceHeader. |
| 75 | return p.AsValueOf(t).Interface() |
| 76 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 77 | |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 78 | func (p pointer) Bool() *bool { return (*bool)(p.p) } |
| 79 | func (p pointer) BoolPtr() **bool { return (**bool)(p.p) } |
| 80 | func (p pointer) BoolSlice() *[]bool { return (*[]bool)(p.p) } |
| 81 | func (p pointer) Int32() *int32 { return (*int32)(p.p) } |
| 82 | func (p pointer) Int32Ptr() **int32 { return (**int32)(p.p) } |
| 83 | func (p pointer) Int32Slice() *[]int32 { return (*[]int32)(p.p) } |
| 84 | func (p pointer) Int64() *int64 { return (*int64)(p.p) } |
| 85 | func (p pointer) Int64Ptr() **int64 { return (**int64)(p.p) } |
| 86 | func (p pointer) Int64Slice() *[]int64 { return (*[]int64)(p.p) } |
| 87 | func (p pointer) Uint32() *uint32 { return (*uint32)(p.p) } |
| 88 | func (p pointer) Uint32Ptr() **uint32 { return (**uint32)(p.p) } |
| 89 | func (p pointer) Uint32Slice() *[]uint32 { return (*[]uint32)(p.p) } |
| 90 | func (p pointer) Uint64() *uint64 { return (*uint64)(p.p) } |
| 91 | func (p pointer) Uint64Ptr() **uint64 { return (**uint64)(p.p) } |
| 92 | func (p pointer) Uint64Slice() *[]uint64 { return (*[]uint64)(p.p) } |
| 93 | func (p pointer) Float32() *float32 { return (*float32)(p.p) } |
| 94 | func (p pointer) Float32Ptr() **float32 { return (**float32)(p.p) } |
| 95 | func (p pointer) Float32Slice() *[]float32 { return (*[]float32)(p.p) } |
| 96 | func (p pointer) Float64() *float64 { return (*float64)(p.p) } |
| 97 | func (p pointer) Float64Ptr() **float64 { return (**float64)(p.p) } |
| 98 | func (p pointer) Float64Slice() *[]float64 { return (*[]float64)(p.p) } |
| 99 | func (p pointer) String() *string { return (*string)(p.p) } |
| 100 | func (p pointer) StringPtr() **string { return (**string)(p.p) } |
| 101 | func (p pointer) StringSlice() *[]string { return (*[]string)(p.p) } |
| 102 | func (p pointer) Bytes() *[]byte { return (*[]byte)(p.p) } |
| 103 | func (p pointer) BytesSlice() *[][]byte { return (*[][]byte)(p.p) } |
| 104 | func (p pointer) Extensions() *map[int32]ExtensionField { return (*map[int32]ExtensionField)(p.p) } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 105 | |
| 106 | func (p pointer) Elem() pointer { |
| 107 | return pointer{p: *(*unsafe.Pointer)(p.p)} |
| 108 | } |
| 109 | |
| 110 | // PointerSlice loads []*T from p as a []pointer. |
| 111 | // The value returned is aliased with the original slice. |
| 112 | // This behavior differs from the implementation in pointer_reflect.go. |
| 113 | func (p pointer) PointerSlice() []pointer { |
| 114 | // Super-tricky - p should point to a []*T where T is a |
| 115 | // message type. We load it as []pointer. |
| 116 | return *(*[]pointer)(p.p) |
| 117 | } |