Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | |
| 11 | "google.golang.org/protobuf/internal/pragma" |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 12 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | // MessageState is a data structure that is nested as the first field in a |
| 16 | // concrete message. It provides a way to implement the ProtoReflect method |
| 17 | // in an allocation-free way without needing to have a shadow Go type generated |
| 18 | // for every message type. This technique only works using unsafe. |
| 19 | // |
| 20 | // |
| 21 | // Example generated code: |
| 22 | // |
| 23 | // type M struct { |
| 24 | // state protoimpl.MessageState |
| 25 | // |
| 26 | // Field1 int32 |
| 27 | // Field2 string |
| 28 | // Field3 *BarMessage |
| 29 | // ... |
| 30 | // } |
| 31 | // |
| 32 | // func (m *M) ProtoReflect() protoreflect.Message { |
| 33 | // mi := &file_fizz_buzz_proto_msgInfos[5] |
| 34 | // if protoimpl.UnsafeEnabled && m != nil { |
| 35 | // ms := protoimpl.X.MessageStateOf(Pointer(m)) |
| 36 | // if ms.LoadMessageInfo() == nil { |
| 37 | // ms.StoreMessageInfo(mi) |
| 38 | // } |
| 39 | // return ms |
| 40 | // } |
| 41 | // return mi.MessageOf(m) |
| 42 | // } |
| 43 | // |
| 44 | // The MessageState type holds a *MessageInfo, which must be atomically set to |
| 45 | // the message info associated with a given message instance. |
| 46 | // By unsafely converting a *M into a *MessageState, the MessageState object |
| 47 | // has access to all the information needed to implement protobuf reflection. |
| 48 | // It has access to the message info as its first field, and a pointer to the |
| 49 | // MessageState is identical to a pointer to the concrete message value. |
| 50 | // |
| 51 | // |
| 52 | // Requirements: |
| 53 | // • The type M must implement protoreflect.ProtoMessage. |
| 54 | // • The address of m must not be nil. |
| 55 | // • The address of m and the address of m.state must be equal, |
| 56 | // even though they are different Go types. |
| 57 | type MessageState struct { |
| 58 | pragma.NoUnkeyedLiterals |
| 59 | pragma.DoNotCompare |
| 60 | pragma.DoNotCopy |
| 61 | |
| 62 | mi *MessageInfo |
| 63 | } |
| 64 | |
| 65 | type messageState MessageState |
| 66 | |
| 67 | var ( |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 68 | _ pref.Message = (*messageState)(nil) |
| 69 | _ Unwrapper = (*messageState)(nil) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 70 | ) |
| 71 | |
| 72 | // messageDataType is a tuple of a pointer to the message data and |
| 73 | // a pointer to the message type. It is a generalized way of providing a |
| 74 | // reflective view over a message instance. The disadvantage of this approach |
| 75 | // is the need to allocate this tuple of 16B. |
| 76 | type messageDataType struct { |
| 77 | p pointer |
| 78 | mi *MessageInfo |
| 79 | } |
| 80 | |
| 81 | type ( |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 82 | messageReflectWrapper messageDataType |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 83 | messageIfaceWrapper messageDataType |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 84 | ) |
| 85 | |
| 86 | var ( |
| 87 | _ pref.Message = (*messageReflectWrapper)(nil) |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 88 | _ Unwrapper = (*messageReflectWrapper)(nil) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 89 | _ pref.ProtoMessage = (*messageIfaceWrapper)(nil) |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 90 | _ Unwrapper = (*messageIfaceWrapper)(nil) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 91 | ) |
| 92 | |
| 93 | // MessageOf returns a reflective view over a message. The input must be a |
| 94 | // pointer to a named Go struct. If the provided type has a ProtoReflect method, |
| 95 | // it must be implemented by calling this method. |
| 96 | func (mi *MessageInfo) MessageOf(m interface{}) pref.Message { |
| 97 | // TODO: Switch the input to be an opaque Pointer. |
| 98 | if reflect.TypeOf(m) != mi.GoType { |
| 99 | panic(fmt.Sprintf("type mismatch: got %T, want %v", m, mi.GoType)) |
| 100 | } |
| 101 | p := pointerOfIface(m) |
| 102 | if p.IsNil() { |
| 103 | return mi.nilMessage.Init(mi) |
| 104 | } |
| 105 | return &messageReflectWrapper{p, mi} |
| 106 | } |
| 107 | |
Joe Tsai | 2aea614 | 2019-07-31 12:27:30 -0700 | [diff] [blame] | 108 | func (m *messageReflectWrapper) pointer() pointer { return m.p } |
| 109 | func (m *messageReflectWrapper) messageInfo() *MessageInfo { return m.mi } |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 110 | |
| 111 | func (m *messageIfaceWrapper) ProtoReflect() pref.Message { |
| 112 | return (*messageReflectWrapper)(m) |
| 113 | } |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 114 | func (m *messageIfaceWrapper) ProtoUnwrap() interface{} { |
| 115 | return m.p.AsIfaceOf(m.mi.GoType.Elem()) |
| 116 | } |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 117 | |
| 118 | type extensionMap map[int32]ExtensionField |
| 119 | |
| 120 | func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) { |
| 121 | if m != nil { |
| 122 | for _, x := range *m { |
| 123 | xt := x.GetType() |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 124 | if !f(xt.Descriptor(), xt.ValueOf(x.GetValue())) { |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 125 | return |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) { |
| 131 | if m != nil { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 132 | _, ok = (*m)[int32(xt.Descriptor().Number())] |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 133 | } |
| 134 | return ok |
| 135 | } |
| 136 | func (m *extensionMap) Clear(xt pref.ExtensionType) { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 137 | delete(*m, int32(xt.Descriptor().Number())) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 138 | } |
| 139 | func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 140 | xd := xt.Descriptor() |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 141 | if m != nil { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 142 | if x, ok := (*m)[int32(xd.Number())]; ok { |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 143 | return xt.ValueOf(x.GetValue()) |
| 144 | } |
| 145 | } |
Damien Neil | d4f0800 | 2019-08-07 12:21:41 -0700 | [diff] [blame] | 146 | return xt.Zero() |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 147 | } |
| 148 | func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) { |
| 149 | if *m == nil { |
| 150 | *m = make(map[int32]ExtensionField) |
| 151 | } |
| 152 | var x ExtensionField |
| 153 | x.SetType(xt) |
| 154 | x.SetEagerValue(xt.InterfaceOf(v)) |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 155 | (*m)[int32(xt.Descriptor().Number())] = x |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 156 | } |
| 157 | func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 158 | xd := xt.Descriptor() |
| 159 | if !isComposite(xd) { |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 160 | panic("invalid Mutable on field with non-composite type") |
| 161 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 162 | if x, ok := (*m)[int32(xd.Number())]; ok { |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 163 | return xt.ValueOf(x.GetValue()) |
| 164 | } |
| 165 | v := xt.New() |
| 166 | m.Set(xt, v) |
| 167 | return v |
| 168 | } |
| 169 | |
| 170 | func isComposite(fd pref.FieldDescriptor) bool { |
| 171 | return fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind || fd.IsList() || fd.IsMap() |
| 172 | } |
| 173 | |
| 174 | // checkField verifies that the provided field descriptor is valid. |
| 175 | // Exactly one of the returned values is populated. |
| 176 | func (mi *MessageInfo) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) { |
| 177 | if fi := mi.fields[fd.Number()]; fi != nil { |
| 178 | if fi.fieldDesc != fd { |
| 179 | panic("mismatching field descriptor") |
| 180 | } |
| 181 | return fi, nil |
| 182 | } |
| 183 | if fd.IsExtension() { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 184 | if fd.ContainingMessage().FullName() != mi.PBType.Descriptor().FullName() { |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 185 | // TODO: Should this be exact containing message descriptor match? |
| 186 | panic("mismatching containing message") |
| 187 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 188 | if !mi.PBType.Descriptor().ExtensionRanges().Has(fd.Number()) { |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 189 | panic("invalid extension field") |
| 190 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 191 | xtd, ok := fd.(pref.ExtensionTypeDescriptor) |
| 192 | if !ok { |
| 193 | panic("extension descriptor does not implement ExtensionTypeDescriptor") |
| 194 | } |
| 195 | return nil, xtd.Type() |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 196 | } |
| 197 | panic("invalid field descriptor") |
| 198 | } |