Files
go/list_ptr.go
zeekay d9bcc8711f The generator can say what the chains actually put on the wire
A list's encoding was one thing for every element type: a run of
{length, payload} pairs. The Lux chains write four different runs — a
run of numbers, a run of fixed-width byte records, a run of struct
payloads back to back, and a run of relative pointers to payloads
written elsewhere — and none of them was that one. So both chains that
have a C++ port wrote their own accessors by hand, and the generator
could not have replaced them.

The shape is now derived from the element type rather than fixed. A
number is a run of numbers at its own width; bytes_fixed[N] is those
runs back to back; a struct with no tail is its payload back to back at
its own width; a struct WITH a tail cannot sit in a run at all, because
its tail has nowhere to go between two neighbours, so it becomes a
pointer. The schema never spells any of this, which is what stops two
authors spelling one list two ways. bytes and text have no width, so a
run of them is refused rather than invented.

Order is the wire, too. Every payload a struct points at is written
before the struct's own object, because SetList needs an offset that
already exists — the order the reference has always written in, and the
one the emitted builder had backwards. An empty list now writes nothing
at all: the alignment pad that a start-then-discard would leave behind
moves every byte after it.

Three entry points per struct, because there are three places a struct
sits: New for a message of its own, Append for an object inside somebody
else's message, Encode for one element of an inline run. A nested struct
field is the nested type's own Input, not a pre-built buffer the caller
hands over unchecked.

Readers take the stride the schema knows, so a lying length word is
refused once instead of at every element, and a list of records answers
with the record's type instead of an offset the caller has to know.

Both backends emit version 2. That is the header every vector in the
chain corpus opens with, and the one the hardened runtime writes; a
generated builder that wrote version 1 wrote a header no chain has.

The Go runtime gains the pointer list it was missing — AddObjectPtr,
ObjectPtr and the strided read — so both backends can express the same
wire rather than one of them refusing.

Proven: one schema, both backends, byte-identical output over all four
list shapes, a bytes tail and a pointer list.
2026-09-06 09:09:54 -07:00

80 lines
2.6 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause-Eco
// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package zap
import "encoding/binary"
// The pointer list, and the strided read of any list.
//
// A list of records that are all the same width is those records back to
// back, and List.Object(i, stride) already reads one. A list of records that
// are NOT all the same width cannot be laid out that way, because the reader
// has no way to find the i'th; the wire answers with a run of 4-byte signed
// relative pointers, each naming a payload written elsewhere in the same
// message. That is the shape here, and it is the same shape the C++ runtime
// spells add_object_ptr / object_ptr.
// ptrElem is the width of one element of a pointer list.
const ptrElem = 4
// AddObjectPtr appends one 4-byte SIGNED relative pointer to an object
// already written at target, and counts ONE element. A target of 0 is a null
// element.
//
// Signed, because a builder finishes a child before its parent, so the
// payload usually sits EARLIER in the buffer than the pointer naming it and
// the offset runs backwards.
func (lb *ListBuilder) AddObjectPtr(target int) {
lb.b.grow(ptrElem)
if target == 0 {
binary.LittleEndian.PutUint32(lb.b.buf[lb.b.pos:], 0)
} else {
binary.LittleEndian.PutUint32(lb.b.buf[lb.b.pos:], uint32(int32(target-lb.b.pos)))
}
lb.b.pos += ptrElem
lb.count++
}
// ObjectPtr returns element i of a pointer list, dereferenced exactly as
// Object.Object dereferences a nested-object field: a target inside the wire
// header is refused, and so is one past the end.
func (l List) ObjectPtr(i int) Object {
if i < 0 || i >= l.length || l.msg == nil {
return Object{}
}
d := l.msg.data
pos := l.offset + i*ptrElem
if pos < 0 || pos+ptrElem > len(d) {
return Object{}
}
rel := int32(binary.LittleEndian.Uint32(d[pos:]))
if rel == 0 {
return Object{}
}
abs := pos + int(rel)
if abs < HeaderSize || abs >= len(d) {
return Object{}
}
return Object{msg: l.msg, offset: abs}
}
// ListStride is List with the tighter clamp a schema makes possible: length
// times stride has to fit what is left of the buffer, so a lying length word
// is refused once, here, instead of at every element accessor.
//
// List is this with stride 0 — the permissive bound, for a caller that does
// not know the width.
func (o Object) ListStride(fieldOffset int, stride uint32) List {
l := o.List(fieldOffset)
if l.msg == nil || stride == 0 {
return l
}
remaining := uint64(len(o.data()) - l.offset)
if uint64(l.length)*uint64(stride) > remaining {
return List{}
}
return l
}