| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package state |
| 16 |
|
| 17 |
import ( |
| 18 |
"reflect" |
| 19 |
"sort" |
| 20 |
|
| 21 |
"inet.af/netstack/state/wire" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
func assertValidType(name string, fields []string) { |
| 26 |
if name == "" { |
| 27 |
Failf("type has empty name") |
| 28 |
} |
| 29 |
fieldsCopy := make([]string, len(fields)) |
| 30 |
for i := 0; i < len(fields); i++ { |
| 31 |
if fields[i] == "" { |
| 32 |
Failf("field has empty name for type %q", name) |
| 33 |
} |
| 34 |
fieldsCopy[i] = fields[i] |
| 35 |
} |
| 36 |
sort.Slice(fieldsCopy, func(i, j int) bool { |
| 37 |
return fieldsCopy[i] < fieldsCopy[j] |
| 38 |
}) |
| 39 |
for i := range fieldsCopy { |
| 40 |
if i > 0 && fieldsCopy[i-1] == fieldsCopy[i] { |
| 41 |
Failf("duplicate field %q for type %s", fieldsCopy[i], name) |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
type typeEntry struct { |
| 48 |
ID typeID |
| 49 |
wire.Type |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
type reconciledTypeEntry struct { |
| 54 |
wire.Type |
| 55 |
LocalType reflect.Type |
| 56 |
FieldOrder []int |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
type typeEncodeDatabase struct { |
| 61 |
|
| 62 |
byType map[reflect.Type]*typeEntry |
| 63 |
|
| 64 |
|
| 65 |
lastID typeID |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func makeTypeEncodeDatabase() typeEncodeDatabase { |
| 70 |
return typeEncodeDatabase{ |
| 71 |
byType: make(map[reflect.Type]*typeEntry), |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
type typeDecodeDatabase struct { |
| 77 |
|
| 78 |
byID []*reconciledTypeEntry |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pending []*wire.Type |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
func makeTypeDecodeDatabase() typeDecodeDatabase { |
| 89 |
return typeDecodeDatabase{} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
func lookupNameFields(typ reflect.Type) (string, []string, bool) { |
| 94 |
v := reflect.Zero(reflect.PtrTo(typ)).Interface() |
| 95 |
t, ok := v.(Type) |
| 96 |
if !ok { |
| 97 |
|
| 98 |
if typ.Kind() == reflect.Interface { |
| 99 |
return interfaceType, nil, true |
| 100 |
} |
| 101 |
name := typ.Name() |
| 102 |
if _, ok := primitiveTypeDatabase[name]; !ok { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
return "", nil, false |
| 107 |
} |
| 108 |
return name, nil, true |
| 109 |
} |
| 110 |
|
| 111 |
if raceEnabled { |
| 112 |
if _, ok := reverseTypeDatabase[typ]; !ok { |
| 113 |
|
| 114 |
|
| 115 |
return "", nil, false |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
name := t.StateTypeName() |
| 120 |
fields := t.StateFields() |
| 121 |
assertValidType(name, fields) |
| 122 |
return name, fields, true |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
func (tdb *typeEncodeDatabase) Lookup(typ reflect.Type) (*typeEntry, bool) { |
| 132 |
te, ok := tdb.byType[typ] |
| 133 |
if !ok { |
| 134 |
|
| 135 |
name, fields, ok := lookupNameFields(typ) |
| 136 |
if !ok { |
| 137 |
|
| 138 |
|
| 139 |
return nil, false |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
tdb.lastID++ |
| 144 |
te = &typeEntry{ |
| 145 |
ID: tdb.lastID, |
| 146 |
Type: wire.Type{ |
| 147 |
Name: name, |
| 148 |
Fields: fields, |
| 149 |
}, |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
tdb.byType[typ] = te |
| 154 |
return te, false |
| 155 |
} |
| 156 |
return te, true |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
func (tbd *typeDecodeDatabase) Register(typ *wire.Type) { |
| 161 |
assertValidType(typ.Name, typ.Fields) |
| 162 |
tbd.pending = append(tbd.pending, typ) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func (tbd *typeDecodeDatabase) LookupName(id typeID) string { |
| 167 |
if len(tbd.pending) < int(id) { |
| 168 |
|
| 169 |
Failf("type ID %d not available", id) |
| 170 |
} |
| 171 |
return tbd.pending[id-1].Name |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
func (tbd *typeDecodeDatabase) LookupType(id typeID) reflect.Type { |
| 176 |
name := tbd.LookupName(id) |
| 177 |
typ, ok := globalTypeDatabase[name] |
| 178 |
if !ok { |
| 179 |
|
| 180 |
typ, ok = primitiveTypeDatabase[name] |
| 181 |
if !ok && name == interfaceType { |
| 182 |
|
| 183 |
var i interface{} |
| 184 |
return reflect.TypeOf(&i).Elem() |
| 185 |
} |
| 186 |
if !ok { |
| 187 |
|
| 188 |
Failf("type name %q is not available", name) |
| 189 |
} |
| 190 |
return typ |
| 191 |
} |
| 192 |
return typ |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
var singleFieldOrder = []int{0} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
func (tbd *typeDecodeDatabase) Lookup(id typeID, typ reflect.Type) *reconciledTypeEntry { |
| 207 |
if len(tbd.byID) > int(id) && tbd.byID[id-1] != nil { |
| 208 |
|
| 209 |
return tbd.byID[id-1] |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
if len(tbd.pending) < int(id) { |
| 214 |
|
| 215 |
Failf("typeDatabase does not contain id %d", id) |
| 216 |
} |
| 217 |
|
| 218 |
pending := tbd.pending[id-1] |
| 219 |
|
| 220 |
if len(tbd.byID) < int(id) { |
| 221 |
tbd.byID = append(tbd.byID, make([]*reconciledTypeEntry, int(id)-len(tbd.byID))...) |
| 222 |
} |
| 223 |
|
| 224 |
name, fields, ok := lookupNameFields(typ) |
| 225 |
if !ok { |
| 226 |
|
| 227 |
|
| 228 |
Failf("unsupported type %q during decode; can't reconcile", pending.Name) |
| 229 |
} |
| 230 |
if name != pending.Name { |
| 231 |
|
| 232 |
|
| 233 |
Failf("typeDatabase contains conflicting definitions for id %d: %s->%v (current) and %s->%v (existing)", |
| 234 |
id, name, fields, pending.Name, pending.Fields) |
| 235 |
} |
| 236 |
rte := &reconciledTypeEntry{ |
| 237 |
Type: wire.Type{ |
| 238 |
Name: name, |
| 239 |
Fields: fields, |
| 240 |
}, |
| 241 |
LocalType: typ, |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if len(fields) != len(pending.Fields) { |
| 248 |
Failf("type %q contains different fields: %v (decode) and %v (encode)", |
| 249 |
name, fields, pending.Fields) |
| 250 |
} |
| 251 |
if len(fields) == 0 { |
| 252 |
tbd.byID[id-1] = rte |
| 253 |
return rte |
| 254 |
} |
| 255 |
if len(fields) == 1 && fields[0] == pending.Fields[0] { |
| 256 |
tbd.byID[id-1] = rte |
| 257 |
rte.FieldOrder = singleFieldOrder |
| 258 |
return rte |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
fieldOrder := make([]int, len(fields)) |
| 265 |
for i, name := range fields { |
| 266 |
fieldOrder[i] = -1 |
| 267 |
|
| 268 |
if pending.Fields[i] == name { |
| 269 |
fieldOrder[i] = i |
| 270 |
continue |
| 271 |
} |
| 272 |
|
| 273 |
for j, otherName := range pending.Fields { |
| 274 |
if name == otherName { |
| 275 |
fieldOrder[i] = j |
| 276 |
break |
| 277 |
} |
| 278 |
} |
| 279 |
if fieldOrder[i] == -1 { |
| 280 |
|
| 281 |
Failf("type %q has mismatched fields: %v (decode) and %v (encode)", |
| 282 |
name, fields, pending.Fields) |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
rte.FieldOrder = fieldOrder |
| 287 |
tbd.byID[id-1] = rte |
| 288 |
return rte |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
const interfaceType = "interface" |
| 293 |
|
| 294 |
|
| 295 |
var primitiveTypeDatabase = func() map[string]reflect.Type { |
| 296 |
r := make(map[string]reflect.Type) |
| 297 |
for _, t := range []reflect.Type{ |
| 298 |
reflect.TypeOf(false), |
| 299 |
reflect.TypeOf(int(0)), |
| 300 |
reflect.TypeOf(int8(0)), |
| 301 |
reflect.TypeOf(int16(0)), |
| 302 |
reflect.TypeOf(int32(0)), |
| 303 |
reflect.TypeOf(int64(0)), |
| 304 |
reflect.TypeOf(uint(0)), |
| 305 |
reflect.TypeOf(uintptr(0)), |
| 306 |
reflect.TypeOf(uint8(0)), |
| 307 |
reflect.TypeOf(uint16(0)), |
| 308 |
reflect.TypeOf(uint32(0)), |
| 309 |
reflect.TypeOf(uint64(0)), |
| 310 |
reflect.TypeOf(""), |
| 311 |
reflect.TypeOf(float32(0.0)), |
| 312 |
reflect.TypeOf(float64(0.0)), |
| 313 |
reflect.TypeOf(complex64(0.0)), |
| 314 |
reflect.TypeOf(complex128(0.0)), |
| 315 |
} { |
| 316 |
r[t.Name()] = t |
| 317 |
} |
| 318 |
return r |
| 319 |
}() |
| 320 |
|
| 321 |
|
| 322 |
var globalTypeDatabase = map[string]reflect.Type{} |
| 323 |
|
| 324 |
|
| 325 |
var reverseTypeDatabase = map[reflect.Type]string{} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
func Register(t Type) { |
| 331 |
name := t.StateTypeName() |
| 332 |
typ := reflect.TypeOf(t) |
| 333 |
if raceEnabled { |
| 334 |
assertValidType(name, t.StateFields()) |
| 335 |
|
| 336 |
if typ.Kind() != reflect.Ptr { |
| 337 |
Failf("Register must be called on pointers") |
| 338 |
} |
| 339 |
} |
| 340 |
typ = typ.Elem() |
| 341 |
if raceEnabled { |
| 342 |
if typ.Kind() == reflect.Struct { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
if _, ok := t.(SaverLoader); !ok { |
| 348 |
Failf("struct %T does not implement SaverLoader", t) |
| 349 |
} |
| 350 |
} else { |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if fields := t.StateFields(); len(fields) != 0 { |
| 356 |
Failf("non-struct %T has non-zero fields %v", t, fields) |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
if _, ok := t.(SaverLoader); ok { |
| 361 |
Failf("non-struct %T implements SaverLoader", t) |
| 362 |
} |
| 363 |
} |
| 364 |
if _, ok := primitiveTypeDatabase[name]; ok { |
| 365 |
Failf("conflicting primitiveTypeDatabase entry for %T: used by primitive", t) |
| 366 |
} |
| 367 |
if _, ok := globalTypeDatabase[name]; ok { |
| 368 |
Failf("conflicting globalTypeDatabase entries for %T: name conflict", t) |
| 369 |
} |
| 370 |
if name == interfaceType { |
| 371 |
Failf("conflicting name for %T: matches interfaceType", t) |
| 372 |
} |
| 373 |
reverseTypeDatabase[typ] = name |
| 374 |
} |
| 375 |
globalTypeDatabase[name] = typ |
| 376 |
} |