(layout: StructLayout, r: Reader)
| 4612 | } |
| 4613 | |
| 4614 | function decodeStruct(layout: StructLayout, r: Reader): unknown { |
| 4615 | // Only a run field with index signatures hands over an intern table. |
| 4616 | let keys: Array<unknown> | undefined |
| 4617 | if (layout.extra.length > 0) { |
| 4618 | keys = r.intern |
| 4619 | r.intern = undefined |
| 4620 | } |
| 4621 | const out: Record<string, unknown> = {} |
| 4622 | // Use bitmasks for common structs and allocate sets only when needed. |
| 4623 | let seenMask = 0 |
| 4624 | let presentMask = 0 |
| 4625 | let seenWide: Set<number> | undefined |
| 4626 | let presentWide: Set<number> | undefined |
| 4627 | let seenUnknown: Set<number> | undefined |
| 4628 | let seenExtra = false |
| 4629 | // Fast-path fields in encoder order and fall back to the id map. |
| 4630 | const fields = layout.fields |
| 4631 | let cursor = 0 |
| 4632 | while (r.pos < r.end) { |
| 4633 | const tag = r.fieldTag() |
| 4634 | const id = Math.floor(tag / FIELD_WIRE_FACTOR) |
| 4635 | const kind = tag - id * FIELD_WIRE_FACTOR |
| 4636 | if (id === 0) { |
| 4637 | if (seenExtra) invalid("unique field ids", undefined, r.options) |
| 4638 | seenExtra = true |
| 4639 | if (kind !== FIELD_WIRE_SIZED) invalid("extra field map", undefined, r.options) |
| 4640 | const saved = r.enter(r.uvarint()) |
| 4641 | decodeExtraPairs(layout, r, out, keys) |
| 4642 | r.exit(saved) |
| 4643 | continue |
| 4644 | } |
| 4645 | let field: Field | undefined |
| 4646 | if (cursor < fields.length && fields[cursor].id === id) { |
| 4647 | field = fields[cursor] |
| 4648 | cursor++ |
| 4649 | } else { |
| 4650 | field = layout.byId.get(id) |
| 4651 | if (field !== undefined) cursor = field.index + 1 |
| 4652 | } |
| 4653 | if (field === undefined) { |
| 4654 | if (r.rejectUnknownFields) invalid("a known field id", id, r.options) |
| 4655 | if (seenUnknown === undefined) seenUnknown = new Set() |
| 4656 | else if (seenUnknown.has(id)) invalid("unique field ids", undefined, r.options) |
| 4657 | seenUnknown.add(id) |
| 4658 | skipFieldPayload(kind, r) |
| 4659 | continue |
| 4660 | } |
| 4661 | const index = field.index |
| 4662 | if (index < 32) { |
| 4663 | const bit = 1 << index |
| 4664 | if ((seenMask & bit) !== 0) invalid("unique field ids", undefined, r.options) |
| 4665 | seenMask |= bit |
| 4666 | } else if (seenWide === undefined) { |
| 4667 | seenWide = new Set([index]) |
| 4668 | } else { |
| 4669 | if (seenWide.has(index)) invalid("unique field ids", undefined, r.options) |
| 4670 | seenWide.add(index) |
| 4671 | } |
no test coverage detected