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 | |
| 26 | // pointer is an abstract representation of a pointer to a struct or field. |
| 27 | type pointer struct{ v reflect.Value } |
| 28 | |
| 29 | // pointerOfValue returns v as a pointer. |
| 30 | func pointerOfValue(v reflect.Value) pointer { |
| 31 | return pointer{v: v} |
| 32 | } |
| 33 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 34 | // pointerOfIface returns the pointer portion of an interface. |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 35 | func pointerOfIface(v interface{}) pointer { |
| 36 | return pointer{v: reflect.ValueOf(v)} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 39 | // IsNil reports whether the pointer is nil. |
| 40 | func (p pointer) IsNil() bool { |
| 41 | return p.v.IsNil() |
| 42 | } |
| 43 | |
| 44 | // Apply adds an offset to the pointer to derive a new pointer |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 45 | // 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] | 46 | func (p pointer) Apply(f offset) pointer { |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 47 | // TODO: Handle unexported fields in an API that hides XXX fields? |
| 48 | return pointer{v: p.v.Elem().FieldByIndex(f).Addr()} |
| 49 | } |
| 50 | |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 51 | // AsValueOf treats p as a pointer to an object of type t and returns the value. |
| 52 | // It is equivalent to reflect.ValueOf(p.AsIfaceOf(t)) |
| 53 | func (p pointer) AsValueOf(t reflect.Type) reflect.Value { |
Damien Neil | 927aaba | 2019-05-09 12:18:44 -0700 | [diff] [blame^] | 54 | if got := p.v.Type().Elem(); got != t { |
| 55 | panic(fmt.Sprintf("invalid type: got %v, want %v", got, t)) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 56 | } |
| 57 | return p.v |
| 58 | } |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 59 | |
| 60 | // AsIfaceOf treats p as a pointer to an object of type t and returns the value. |
| 61 | // It is equivalent to p.AsValueOf(t).Interface() |
| 62 | func (p pointer) AsIfaceOf(t reflect.Type) interface{} { |
| 63 | return p.AsValueOf(t).Interface() |
| 64 | } |