blob: b851b1b04a0deed0b57c82658453431cb0053d5b [file] [log] [blame]
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001// 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 Tsaibc534a92018-12-18 16:07:48 -08005// +build purego appengine
Joe Tsaifa02f4e2018-09-12 16:20:37 -07006
7package impl
8
9import (
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.
16type offset []int
17
18// offsetOf returns a field offset for the struct field.
19func 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 Neilc37adef2019-04-01 13:49:56 -070026// IsValid reports whether the offset is valid.
27func (f offset) IsValid() bool { return f != nil }
28
29// invalidOffset is an invalid field offset.
30var invalidOffset = offset(nil)
31
32// zeroOffset is a noop when calling pointer.Apply.
33var zeroOffset = offset([]int{0})
34
Joe Tsaifa02f4e2018-09-12 16:20:37 -070035// pointer is an abstract representation of a pointer to a struct or field.
36type pointer struct{ v reflect.Value }
37
38// pointerOfValue returns v as a pointer.
39func pointerOfValue(v reflect.Value) pointer {
40 return pointer{v: v}
41}
42
Joe Tsaic6b75612018-09-13 14:24:37 -070043// pointerOfIface returns the pointer portion of an interface.
Joe Tsai6cf80c42018-12-01 04:57:09 -080044func pointerOfIface(v interface{}) pointer {
45 return pointer{v: reflect.ValueOf(v)}
Joe Tsaic6b75612018-09-13 14:24:37 -070046}
47
Joe Tsai6cf80c42018-12-01 04:57:09 -080048// IsNil reports whether the pointer is nil.
49func (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 Tsaifa02f4e2018-09-12 16:20:37 -070054// to a specified field. The current pointer must be pointing at a struct.
Joe Tsai6cf80c42018-12-01 04:57:09 -080055func (p pointer) Apply(f offset) pointer {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070056 // TODO: Handle unexported fields in an API that hides XXX fields?
57 return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
58}
59
Joe Tsai6cf80c42018-12-01 04:57:09 -080060// 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))
62func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
Damien Neil927aaba2019-05-09 12:18:44 -070063 if got := p.v.Type().Elem(); got != t {
64 panic(fmt.Sprintf("invalid type: got %v, want %v", got, t))
Joe Tsaifa02f4e2018-09-12 16:20:37 -070065 }
66 return p.v
67}
Joe Tsai6cf80c42018-12-01 04:57:09 -080068
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()
71func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
72 return p.AsValueOf(t).Interface()
73}
Damien Neilc37adef2019-04-01 13:49:56 -070074
75func (p pointer) Bool() *bool { return p.v.Interface().(*bool) }
76func (p pointer) BoolPtr() **bool { return p.v.Interface().(**bool) }
77func (p pointer) BoolSlice() *[]bool { return p.v.Interface().(*[]bool) }
78func (p pointer) Int32() *int32 { return p.v.Interface().(*int32) }
79func (p pointer) Int32Ptr() **int32 { return p.v.Interface().(**int32) }
80func (p pointer) Int32Slice() *[]int32 { return p.v.Interface().(*[]int32) }
81func (p pointer) Int64() *int64 { return p.v.Interface().(*int64) }
82func (p pointer) Int64Ptr() **int64 { return p.v.Interface().(**int64) }
83func (p pointer) Int64Slice() *[]int64 { return p.v.Interface().(*[]int64) }
84func (p pointer) Uint32() *uint32 { return p.v.Interface().(*uint32) }
85func (p pointer) Uint32Ptr() **uint32 { return p.v.Interface().(**uint32) }
86func (p pointer) Uint32Slice() *[]uint32 { return p.v.Interface().(*[]uint32) }
87func (p pointer) Uint64() *uint64 { return p.v.Interface().(*uint64) }
88func (p pointer) Uint64Ptr() **uint64 { return p.v.Interface().(**uint64) }
89func (p pointer) Uint64Slice() *[]uint64 { return p.v.Interface().(*[]uint64) }
90func (p pointer) Float32() *float32 { return p.v.Interface().(*float32) }
91func (p pointer) Float32Ptr() **float32 { return p.v.Interface().(**float32) }
92func (p pointer) Float32Slice() *[]float32 { return p.v.Interface().(*[]float32) }
93func (p pointer) Float64() *float64 { return p.v.Interface().(*float64) }
94func (p pointer) Float64Ptr() **float64 { return p.v.Interface().(**float64) }
95func (p pointer) Float64Slice() *[]float64 { return p.v.Interface().(*[]float64) }
96func (p pointer) String() *string { return p.v.Interface().(*string) }
97func (p pointer) StringPtr() **string { return p.v.Interface().(**string) }
98func (p pointer) StringSlice() *[]string { return p.v.Interface().(*[]string) }
99func (p pointer) Bytes() *[]byte { return p.v.Interface().(*[]byte) }
100func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) }
101func (p pointer) Extensions() *legacyExtensionMap {
102 return (*legacyExtensionMap)(p.v.Interface().(*map[int32]ExtensionFieldV1))
103}
104
105func (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.
111func (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}