blob: d076b9daa763d2944f31460f937cdf79fcaf3826 [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"
Joe Tsai82760ce2019-06-20 03:09:57 -070012 "sync"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070013)
14
Joe Tsaic0e4bb22019-07-06 13:05:11 -070015const UnsafeEnabled = false
16
Joe Tsai82760ce2019-06-20 03:09:57 -070017// Pointer is an opaque pointer type.
18type Pointer interface{}
19
Joe Tsaifa02f4e2018-09-12 16:20:37 -070020// offset represents the offset to a struct field, accessible from a pointer.
21// The offset is the field index into a struct.
Joe Tsaic0e4bb22019-07-06 13:05:11 -070022type offset struct {
23 index int
24 export exporter
25}
Joe Tsaifa02f4e2018-09-12 16:20:37 -070026
27// offsetOf returns a field offset for the struct field.
Joe Tsaic0e4bb22019-07-06 13:05:11 -070028func offsetOf(f reflect.StructField, x exporter) offset {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070029 if len(f.Index) != 1 {
30 panic("embedded structs are not supported")
31 }
Joe Tsaic0e4bb22019-07-06 13:05:11 -070032 if f.PkgPath == "" {
33 return offset{index: f.Index[0]} // field is already exported
34 }
35 if x == nil {
36 panic("exporter must be provided for unexported field")
37 }
38 return offset{index: f.Index[0], export: x}
Joe Tsaifa02f4e2018-09-12 16:20:37 -070039}
40
Damien Neilc37adef2019-04-01 13:49:56 -070041// IsValid reports whether the offset is valid.
Joe Tsaic0e4bb22019-07-06 13:05:11 -070042func (f offset) IsValid() bool { return f.index >= 0 }
Damien Neilc37adef2019-04-01 13:49:56 -070043
44// invalidOffset is an invalid field offset.
Joe Tsaic0e4bb22019-07-06 13:05:11 -070045var invalidOffset = offset{index: -1}
Damien Neilc37adef2019-04-01 13:49:56 -070046
47// zeroOffset is a noop when calling pointer.Apply.
Joe Tsaic0e4bb22019-07-06 13:05:11 -070048var zeroOffset = offset{index: 0}
Damien Neilc37adef2019-04-01 13:49:56 -070049
Joe Tsaifa02f4e2018-09-12 16:20:37 -070050// pointer is an abstract representation of a pointer to a struct or field.
51type pointer struct{ v reflect.Value }
52
Joe Tsai82760ce2019-06-20 03:09:57 -070053// pointerOf returns p as a pointer.
54func pointerOf(p Pointer) pointer {
55 return pointerOfIface(p)
56}
57
Joe Tsaifa02f4e2018-09-12 16:20:37 -070058// pointerOfValue returns v as a pointer.
59func pointerOfValue(v reflect.Value) pointer {
60 return pointer{v: v}
61}
62
Joe Tsaic6b75612018-09-13 14:24:37 -070063// pointerOfIface returns the pointer portion of an interface.
Joe Tsai6cf80c42018-12-01 04:57:09 -080064func pointerOfIface(v interface{}) pointer {
65 return pointer{v: reflect.ValueOf(v)}
Joe Tsaic6b75612018-09-13 14:24:37 -070066}
67
Joe Tsai6cf80c42018-12-01 04:57:09 -080068// IsNil reports whether the pointer is nil.
69func (p pointer) IsNil() bool {
70 return p.v.IsNil()
71}
72
73// Apply adds an offset to the pointer to derive a new pointer
Joe Tsaifa02f4e2018-09-12 16:20:37 -070074// to a specified field. The current pointer must be pointing at a struct.
Joe Tsai6cf80c42018-12-01 04:57:09 -080075func (p pointer) Apply(f offset) pointer {
Joe Tsaic0e4bb22019-07-06 13:05:11 -070076 if f.export != nil {
77 if v := reflect.ValueOf(f.export(p.v.Interface(), f.index)); v.IsValid() {
78 return pointer{v: v}
79 }
80 }
81 return pointer{v: p.v.Elem().Field(f.index).Addr()}
Joe Tsaifa02f4e2018-09-12 16:20:37 -070082}
83
Joe Tsai6cf80c42018-12-01 04:57:09 -080084// AsValueOf treats p as a pointer to an object of type t and returns the value.
85// It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
86func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
Damien Neil927aaba2019-05-09 12:18:44 -070087 if got := p.v.Type().Elem(); got != t {
88 panic(fmt.Sprintf("invalid type: got %v, want %v", got, t))
Joe Tsaifa02f4e2018-09-12 16:20:37 -070089 }
90 return p.v
91}
Joe Tsai6cf80c42018-12-01 04:57:09 -080092
93// AsIfaceOf treats p as a pointer to an object of type t and returns the value.
94// It is equivalent to p.AsValueOf(t).Interface()
95func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
96 return p.AsValueOf(t).Interface()
97}
Damien Neilc37adef2019-04-01 13:49:56 -070098
99func (p pointer) Bool() *bool { return p.v.Interface().(*bool) }
100func (p pointer) BoolPtr() **bool { return p.v.Interface().(**bool) }
101func (p pointer) BoolSlice() *[]bool { return p.v.Interface().(*[]bool) }
102func (p pointer) Int32() *int32 { return p.v.Interface().(*int32) }
103func (p pointer) Int32Ptr() **int32 { return p.v.Interface().(**int32) }
104func (p pointer) Int32Slice() *[]int32 { return p.v.Interface().(*[]int32) }
105func (p pointer) Int64() *int64 { return p.v.Interface().(*int64) }
106func (p pointer) Int64Ptr() **int64 { return p.v.Interface().(**int64) }
107func (p pointer) Int64Slice() *[]int64 { return p.v.Interface().(*[]int64) }
108func (p pointer) Uint32() *uint32 { return p.v.Interface().(*uint32) }
109func (p pointer) Uint32Ptr() **uint32 { return p.v.Interface().(**uint32) }
110func (p pointer) Uint32Slice() *[]uint32 { return p.v.Interface().(*[]uint32) }
111func (p pointer) Uint64() *uint64 { return p.v.Interface().(*uint64) }
112func (p pointer) Uint64Ptr() **uint64 { return p.v.Interface().(**uint64) }
113func (p pointer) Uint64Slice() *[]uint64 { return p.v.Interface().(*[]uint64) }
114func (p pointer) Float32() *float32 { return p.v.Interface().(*float32) }
115func (p pointer) Float32Ptr() **float32 { return p.v.Interface().(**float32) }
116func (p pointer) Float32Slice() *[]float32 { return p.v.Interface().(*[]float32) }
117func (p pointer) Float64() *float64 { return p.v.Interface().(*float64) }
118func (p pointer) Float64Ptr() **float64 { return p.v.Interface().(**float64) }
119func (p pointer) Float64Slice() *[]float64 { return p.v.Interface().(*[]float64) }
120func (p pointer) String() *string { return p.v.Interface().(*string) }
121func (p pointer) StringPtr() **string { return p.v.Interface().(**string) }
122func (p pointer) StringSlice() *[]string { return p.v.Interface().(*[]string) }
123func (p pointer) Bytes() *[]byte { return p.v.Interface().(*[]byte) }
124func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) }
Joe Tsai3d8e3692019-04-08 13:52:14 -0700125func (p pointer) WeakFields() *WeakFields { return p.v.Interface().(*WeakFields) }
Joe Tsai89d49632019-06-04 16:20:00 -0700126func (p pointer) Extensions() *map[int32]ExtensionField {
127 return p.v.Interface().(*map[int32]ExtensionField)
Damien Neilc37adef2019-04-01 13:49:56 -0700128}
129
130func (p pointer) Elem() pointer {
131 return pointer{v: p.v.Elem()}
132}
133
134// PointerSlice copies []*T from p as a new []pointer.
135// This behavior differs from the implementation in pointer_unsafe.go.
136func (p pointer) PointerSlice() []pointer {
137 // TODO: reconsider this
138 if p.v.IsNil() {
139 return nil
140 }
141 n := p.v.Elem().Len()
142 s := make([]pointer, n)
143 for i := 0; i < n; i++ {
144 s[i] = pointer{v: p.v.Elem().Index(i)}
145 }
146 return s
147}
Damien Neile91877d2019-06-27 10:54:42 -0700148
149// AppendPointerSlice appends v to p, which must be a []*T.
150func (p pointer) AppendPointerSlice(v pointer) {
151 sp := p.v.Elem()
152 sp.Set(reflect.Append(sp, v.v))
153}
154
155// SetPointer sets *p to v.
156func (p pointer) SetPointer(v pointer) {
157 p.v.Elem().Set(v.v)
158}
Joe Tsai82760ce2019-06-20 03:09:57 -0700159
160func (Export) MessageStateOf(p Pointer) *messageState { panic("not supported") }
161func (ms *messageState) pointer() pointer { panic("not supported") }
162func (ms *messageState) LoadMessageInfo() *MessageInfo { panic("not supported") }
163func (ms *messageState) StoreMessageInfo(mi *MessageInfo) { panic("not supported") }
164
165type atomicNilMessage struct {
166 once sync.Once
167 m messageReflectWrapper
168}
169
170func (m *atomicNilMessage) Init(mi *MessageInfo) *messageReflectWrapper {
171 m.once.Do(func() {
172 m.m.p = pointerOfIface(reflect.Zero(mi.GoType).Interface())
173 m.m.mi = mi
174 })
175 return &m.m
176}