mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-01 03:14:16 +00:00
internal/impl: defer evaluation of weak reference until actual use
Suppose some init logic performs protobuf reflection. If so, it will cause the table-driven logic for protobuf reflection to be initialized. This is problematic for weak fields since we can not be certain that all weak references have been registered at this point in time. This is a fundamental issue with with weak dependencies, since it means that we cannot enforce any ordering constraints on the weak dependency unless we directly import the weakly referenced package (which would defeat the point of weak imports). Alleviate the problem by pushing evaluation of weak reference to actual usage. This does not completely fix the problem, but signifcantly reduces the probability of it being problematic. In general, people should avoid interacting with weak fields at init time. Change-Id: Iebaefddde8cf07b5cd7dee49b7015b05b5428618 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188980 Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
f98dd98c15
commit
afd3633ce3
@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"google.golang.org/protobuf/internal/flags"
|
||||
pvalue "google.golang.org/protobuf/internal/value"
|
||||
@ -296,30 +297,21 @@ func fieldInfoForWeakMessage(fd pref.FieldDescriptor, weakOffset offset) fieldIn
|
||||
panic("no support for proto1 weak fields")
|
||||
}
|
||||
|
||||
messageName := fd.Message().FullName()
|
||||
messageType, _ := preg.GlobalTypes.FindMessageByName(messageName)
|
||||
if messageType == nil {
|
||||
return fieldInfo{
|
||||
fieldDesc: fd,
|
||||
has: func(p pointer) bool { return false },
|
||||
clear: func(p pointer) {},
|
||||
get: func(p pointer) pref.Value {
|
||||
var once sync.Once
|
||||
var messageType pref.MessageType
|
||||
var frozenEmpty pref.Value
|
||||
lazyInit := func() {
|
||||
once.Do(func() {
|
||||
messageName := fd.Message().FullName()
|
||||
messageType, _ = preg.GlobalTypes.FindMessageByName(messageName)
|
||||
if messageType == nil {
|
||||
panic(fmt.Sprintf("weak message %v is not linked in", messageName))
|
||||
},
|
||||
set: func(p pointer, v pref.Value) {
|
||||
panic(fmt.Sprintf("weak message %v is not linked in", messageName))
|
||||
},
|
||||
mutable: func(p pointer) pref.Value {
|
||||
panic(fmt.Sprintf("weak message %v is not linked in", messageName))
|
||||
},
|
||||
newMessage: func() pref.Message {
|
||||
panic(fmt.Sprintf("weak message %v is not linked in", messageName))
|
||||
},
|
||||
}
|
||||
}
|
||||
frozenEmpty = pref.ValueOf(frozenMessage{messageType.New()})
|
||||
})
|
||||
}
|
||||
|
||||
num := int32(fd.Number())
|
||||
frozenEmpty := pref.ValueOf(frozenMessage{messageType.New()})
|
||||
return fieldInfo{
|
||||
fieldDesc: fd,
|
||||
has: func(p pointer) bool {
|
||||
@ -335,6 +327,7 @@ func fieldInfoForWeakMessage(fd pref.FieldDescriptor, weakOffset offset) fieldIn
|
||||
delete(*fs, num)
|
||||
},
|
||||
get: func(p pointer) pref.Value {
|
||||
lazyInit()
|
||||
if p.IsNil() {
|
||||
return frozenEmpty
|
||||
}
|
||||
@ -346,6 +339,7 @@ func fieldInfoForWeakMessage(fd pref.FieldDescriptor, weakOffset offset) fieldIn
|
||||
return pref.ValueOf(m.(pref.ProtoMessage).ProtoReflect())
|
||||
},
|
||||
set: func(p pointer, v pref.Value) {
|
||||
lazyInit()
|
||||
m := v.Message()
|
||||
if m.Descriptor() != messageType.Descriptor() {
|
||||
panic("mismatching message descriptor")
|
||||
@ -357,6 +351,7 @@ func fieldInfoForWeakMessage(fd pref.FieldDescriptor, weakOffset offset) fieldIn
|
||||
(*fs)[num] = m.Interface().(piface.MessageV1)
|
||||
},
|
||||
mutable: func(p pointer) pref.Value {
|
||||
lazyInit()
|
||||
fs := p.Apply(weakOffset).WeakFields()
|
||||
if *fs == nil {
|
||||
*fs = make(WeakFields)
|
||||
@ -369,6 +364,7 @@ func fieldInfoForWeakMessage(fd pref.FieldDescriptor, weakOffset offset) fieldIn
|
||||
return pref.ValueOf(m.(pref.ProtoMessage).ProtoReflect())
|
||||
},
|
||||
newMessage: func() pref.Message {
|
||||
lazyInit()
|
||||
return messageType.New()
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user