Documentation
¶
Overview ¶
Package values defines two-word values shared by KVO adapters and EVM. Concrete types and presence are supplied separately; interface values encode their dynamic type using a program-local TypeTable.
Index ¶
- Constants
- Variables
- func CellAt(p unsafe.Pointer, n uint64) unsafe.Pointer
- func NativeTimeAllocBytes(t time.Time) uint64
- type CellStruct
- type Codec
- func BoolCodec[T ~bool]() Codec[T]
- func BytesCodec[T ~[]byte]() Codec[T]
- func Float32Codec[T ~float32]() Codec[T]
- func Float64Codec[T ~float64]() Codec[T]
- func SignedCodec[T Signed]() Codec[T]
- func StringCodec[T ~string]() Codec[T]
- func TimeCodec() Codec[time.Time]
- func UnsignedCodec[T Unsigned]() Codec[T]
- type ComparisonError
- type DynamicType
- type Field
- type Kind
- type Map
- func (m *Map) Clear()
- func (m *Map) Delete(k Value, h uint64) error
- func (m *Map) DeleteContext(ctx context.Context, k Value, h uint64) error
- func (m *Map) Entries() []*MapEntry
- func (m *Map) EntryCount() uint64
- func (m *Map) Find(k Value, h uint64) (*MapEntry, bool, error)
- func (m *Map) FindContext(ctx context.Context, k Value, h uint64) (*MapEntry, bool, error)
- func (m *Map) Len() int
- func (m *Map) ProbeCount(hash uint64) uint64
- func (m *Map) Put(k, v Value, h uint64) error
- func (m *Map) PutContext(ctx context.Context, k, v Value, h uint64) error
- type MapEntry
- type Method
- type NativeAddressCodec
- type NativeCompare
- type NativeCompareStep
- type NativeField
- type NativeLayout
- type NativeMapIterator
- type NativeMapLayout
- type NativeSliceLayout
- type NativeSliceView
- type NativeValueCodec
- type OwnFunc
- type OwnershipBudget
- type Repr
- type RepresentationID
- type Shape
- func (s *Shape) Aggregate() bool
- func (s *Shape) Comparable() bool
- func (s *Shape) Equal(a, b Value, ts *TypeTable) (bool, error)
- func (s *Shape) EqualContext(ctx context.Context, a, b Value, ts *TypeTable) (bool, error)
- func (s *Shape) FieldValue(v Value, i int) Value
- func (s *Shape) GetField(p unsafe.Pointer, i int) Value
- func (s *Shape) Hash(v Value, ts *TypeTable) (uint64, error)
- func (s *Shape) HashContext(ctx context.Context, v Value, ts *TypeTable) (uint64, error)
- func (s *Shape) LogicalKind() Kind
- func (s *Shape) NativeStorage() bool
- func (s *Shape) New() unsafe.Pointer
- func (s *Shape) SetField(p unsafe.Pointer, i int, v Value)
- func (s *Shape) Store(p unsafe.Pointer, v Value)
- func (s *Shape) Stored(p unsafe.Pointer) Value
- func (s *Shape) Work(v Value, limit uint64, ts *TypeTable) (uint64, bool)
- type Signed
- type Type
- type TypeTable
- type Unsigned
- type Value
- func Bool(v bool) Value
- func BorrowByteSlice(data []byte, layout *NativeLayout, budget OwnershipBudget) (Value, error)
- func BorrowNative(typ reflect.Type, p unsafe.Pointer) Value
- func Bytes(b []byte) Value
- func CellSlice(p unsafe.Pointer, n uint64) []Value
- func Float(v float64) Value
- func Float32(v float32) Value
- func Int(v int64) Value
- func NativeSliceValue(view *NativeSliceView) Value
- func NativeTime(t time.Time) Value
- func NativeValue(x any) Value
- func SliceValue(p unsafe.Pointer, length, capacity uint32) Value
- func String(v string) Value
- func Time(t time.Time) Value
- func TypedPair(id RepresentationID, p unsafe.Pointer, aux uint32) Value
- func Uint(v uint64) Value
- func (v Value) AsBool() bool
- func (v Value) AsBytes() []byte
- func (v Value) AsFloat() float64
- func (v Value) AsFloat32() float32
- func (v Value) AsInt() int64
- func (v Value) AsNativeValue() any
- func (v Value) AsString() string
- func (v Value) AsTime() time.Time
- func (v Value) AsUint() uint64
- func (v Value) Auxiliary() uint32
- func (v Value) BorrowedNative() reflect.Value
- func (v Value) HasNativeSlice() bool
- func (v Value) IsBorrowedByteView() bool
- func (v Value) NativeComparable() bool
- func (v Value) NativeSlice() *NativeSliceView
- func (v Value) NativeTimeAddress() unsafe.Pointer
- func (v Value) OwnBytes() error
- func (v Value) ReadNativeSlice() *NativeSliceView
- func (v Value) Representation() RepresentationID
- func (v Value) SliceCap() uint32
- func (v Value) SliceData() unsafe.Pointer
- func (v Value) SliceLen() uint32
- func (v Value) SliceReadData() unsafe.Pointer
- func (v Value) SliceView(lo, length, capacity uint32, b OwnershipBudget) (Value, error)
Constants ¶
const NativeAddress uint64 = 1
NativeAddress marks a pointer into live Go memory. A pointer with R=0 refers to VM cells. The pointer's static type determines whether it carries this discriminator; opaque interface receivers continue to use their own ABI.
const NativeBorrow uint64 = ^uint64(0)
NativeBorrow marks an aggregate borrowed from a host. A typed VM boundary materializes it before assigning it to an aggregate register.
Variables ¶
var ( IntType = NewType("int", Raw) UintType = NewType("uint", Raw) UintptrType = NewType("uintptr", Raw) Int8 = NewType("int8", Raw) Int16 = NewType("int16", Raw) Int32 = NewType("int32", Raw) Uint8 = NewType("uint8", Raw) Uint16 = NewType("uint16", Raw) Uint32 = NewType("uint32", Raw) Float32Type = NewType("float32", Raw) )
var ( Int64 = NewType("i64", Raw) Uint64 = NewType("u64", Raw) Float64 = NewType("f64", Raw) Boolean = NewType("bool", Raw) BoolType = Boolean StringType = NewOwnedType("string", Pair, ownString) AnyType = NewType("any", Pair) ErrorType = NewType("error", Pair) )
var ErrComparisonDepth = errors.New("value is nested too deeply to compare")
ComparisonError is a Go language panic condition, distinct from corrupt interface storage. EVM may recover it; invalid type indexes stay fatal. ErrComparisonDepth reports a value nested too deeply to compare or hash.
Functions ¶
func NativeTimeAllocBytes ¶
NativeTimeAllocBytes reports the allocation made by NativeTime.
Types ¶
type CellStruct ¶
type CellStruct struct {
// Untagged cells carry their logical type externally and box at any boundaries.
Untagged bool
Type *Type
Get func(Value, int) Value
Pack func([2]Value) Value
LoadNative func(unsafe.Pointer) Value
StoreNative func(unsafe.Pointer, Value)
}
CellStruct preserves a two-field Go struct while storing value copies in one cell. Addressable values use their native layout; Get reads immutable cells. Descriptors are immutable setup metadata, not per-value objects.
func CompactOptional ¶
func CompactOptional(name string, elem *Shape) *CellStruct
CompactOptional stores a Value/Present struct in one cell for numeric, boolean, string, and registered compact-struct payloads. Other payloads keep their ordinary struct representation. Markers belong to the descriptor, so no permanent representation IDs are used.
type Codec ¶
Codec packs Go values into EVM values and unpacks them back to Go. Its functions do not inspect runtime types. Type is declaration metadata, used only when binding a codec. Strings and bytes borrow their input storage in both directions.
func BytesCodec ¶
func Float32Codec ¶
func Float64Codec ¶
func SignedCodec ¶
func StringCodec ¶
func UnsignedCodec ¶
type ComparisonError ¶
func (*ComparisonError) Error ¶
func (e *ComparisonError) Error() string
func (*ComparisonError) Unwrap ¶
func (e *ComparisonError) Unwrap() error
type DynamicType ¶
type Kind ¶
type Kind uint8
const ( BoolKind Kind = iota + 1 IntKind UintKind FloatKind StringKind PointerKind StructKind ArrayKind SliceKind MapKind ComplexKind FunctionKind InterfaceKind // TypedPairKind carries a registered physical layout and auxiliary word. TypedPairKind TimestampKind // NativeValueKind retains an opaque Go interface with native equality. NativeValueKind )
type Map ¶
Map stores immutable key/value snapshots. Mutable values inside them (pointers, maps and slices) retain Go aliasing. Deleted entries remain only in iterators.
func (*Map) DeleteContext ¶
func (*Map) Entries ¶
Entries snapshots the existing iteration range; insertions may be skipped. Updates are observed and deleted entries are skipped by the iterator.
func (*Map) EntryCount ¶
func (*Map) FindContext ¶
func (*Map) ProbeCount ¶
type Method ¶
Method is a receiver-free interface requirement. ID includes the defining package path for unexported methods.
type NativeAddressCodec ¶
type NativeAddressCodec struct {
Encode func(unsafe.Pointer) Value
Decode func(Value) (unsafe.Pointer, error)
}
NativeAddressCodec preserves a logical pointer representation for native storage.
type NativeCompare ¶
type NativeCompare struct {
Comparable bool
Zero unsafe.Pointer
Steps []NativeCompareStep
}
NativeCompare preserves Go field order when an opaque snapshot supplies fields omitted from the script layout. A nonnegative Field selects VM cells.
type NativeCompareStep ¶
type NativeField ¶
type NativeField struct {
// Layout records physical slot storage when it differs from the logical shape.
Layout *NativeLayout
// Cells marks VM cells embedded in synthetic aggregate storage.
// Their addresses and values keep the ordinary guest representation.
Cells bool
Offset uintptr
Get func(unsafe.Pointer) Value
Set func(unsafe.Pointer, Value)
}
type NativeLayout ¶
type NativeLayout struct {
Address *NativeAddressCodec
Codec *NativeValueCodec
// OpaqueCell is the one-based cell holding an immutable native snapshot
// for fields omitted from the script type. Zero means no snapshot.
OpaqueCell uint64
Compare *NativeCompare
GoType reflect.Type
Array *NativeSliceLayout
Map *NativeMapLayout
Slice *NativeSliceLayout
Size uint64
New func() unsafe.Pointer
Load func(unsafe.Pointer) Value
Store func(unsafe.Pointer, Value)
Fields []NativeField
}
NativeLayout supplies operations on Go memory, independently of VM cells. Load returns decoded scalars/strings, native pointer targets, or a borrowed native aggregate address. VM aggregate conversion remains an explicit step.
type NativeMapIterator ¶
type NativeMapLayout ¶
type NativeMapLayout struct {
Key, Element *NativeLayout
Load func(unsafe.Pointer) Value
Store func(unsafe.Pointer, Value)
Make func() unsafe.Pointer
Len func(unsafe.Pointer) int
Lookup func(mapPtr, key, result unsafe.Pointer) bool
Update func(mapPtr, key, value unsafe.Pointer)
Delete func(mapPtr, key unsafe.Pointer)
Clear func(unsafe.Pointer)
Iter func(unsafe.Pointer) NativeMapIterator
}
NativeMapLayout operates on a Go map identity, not a field holding that map. Reassigning the source field therefore does not retarget existing aliases.
type NativeSliceLayout ¶
type NativeSliceLayout struct {
Element *NativeLayout
Array func(uint64) *NativeLayout
ElementSize uint64
Load func(unsafe.Pointer) Value
Store func(unsafe.Pointer, Value)
Make func(uint32) unsafe.Pointer
Copy func(dst, src unsafe.Pointer, count uint32)
Clear func(unsafe.Pointer, uint32)
}
NativeSliceLayout operates on Go slice storage. VM values retain the backing array pointer and Go length/capacity; element addresses use native byte strides. Copy and Clear must retain Go write barriers for pointer-bearing elements.
type NativeSliceView ¶
type NativeSliceView struct {
Data unsafe.Pointer
Layout *NativeLayout
Element *NativeLayout
Length, Capacity uint32
}
NativeSliceView carries physical storage per value. Linking a host slice declaration must not change the backing layout of ordinary guest slices. Length and Capacity retain the full uint32 range of ordinary slice headers.
type NativeValueCodec ¶
type NativeValueCodec struct {
Decode func(Value) (Value, error)
Encode func(Value, func(uint64) error) (Value, error)
}
NativeValueCodec converts a native slot to its logical VM value. The encode callback charges any new native storage before allocating it.
type OwnFunc ¶
type OwnFunc func(Value, OwnershipBudget) (Value, error)
OwnFunc discharges borrowed-storage dependencies while preserving the logical value. It may return already independent values unchanged. Mutable identity needs an explicit contract; a shallow copy is not generally sufficient.
type OwnershipBudget ¶
OwnershipBudget is supplied at ownership boundaries, never on ordinary reads. ChargeAllocation must succeed before allocating a clone's backing storage.
type RepresentationID ¶
type RepresentationID uint32
RepresentationID identifies immutable physical-layout metadata. Several representations may share a logical Type. IDs are process-local, never reused, and must not be persisted or allocated per value or transaction.
func RegisterRepresentation ¶
func RegisterRepresentation(shape *Shape) RepresentationID
RegisterRepresentation publishes a physical layout for a compact typed pair. The supplied shape and its reachable metadata must remain immutable.
func (RepresentationID) Shape ¶
func (id RepresentationID) Shape() *Shape
type Shape ¶
type Shape struct {
CellStruct *CellStruct
Native *NativeLayout // optional live Go representation, distinct from Cells
NativeAllocation *NativeLayout // backing for newly allocated collections; does not change stored values
// NativePointerType marks a typed-pair physical representation of a live Go
// pointer. Representations of the same logical type and Go pointer type
// compare by address, independently of their field-access plans.
NativePointerType reflect.Type
// DisplayName is optional source-language spelling, prepared at compile time.
DisplayName string
Methods []Method
Params, Results []*Shape
// PointerLike gives opaque typed pairs pointer nil semantics: P == nil is
// the zero value regardless of representation metadata. Other pairs may
// legitimately carry nonzero auxiliary data with a nil pointer.
PointerLike bool
Variadic bool
Type *Type
Kind Kind
Bits uint8
Elem, Key *Shape
Fields []Field
Len uint64
Cells uint64
}
Shape describes typed VM storage. Descriptors, including recursive graphs, are built before use and never mutated by execution. Link snapshots them. Scalar cells hold one Value. Structs/arrays flatten their members into cells; their register values point to immutable snapshots (nil means all zero).
func NewPointerShape ¶
NewPointerShape declares a pointer to VM cell storage. Reuse the result when declaring signatures and TypeDecls. Live native Go pointers instead use the NativeSchema produced by bindings, which supplies their native layout.
func NewStructShape ¶
NewStructShape declares a nominal VM struct using the same Shape and Field metadata as compiled structs. It copies the fields and computes cell offsets; supplied offsets are ignored. Child shapes must be fully laid out already. Build and retain shapes during setup. Type identity is by pointer, not name.
func (*Shape) Comparable ¶
func (*Shape) EqualContext ¶
EqualContext allows cancellation during traversal of compound values.
func (*Shape) FieldValue ¶
FieldValue reads a field of a value copy, including compact structs.
func (*Shape) GetField ¶
GetField borrows a field of VM cell storage. Aggregate fields point into that storage; use the field shape's Store to copy them. p must be nonnil storage for this struct shape, never a pointer to a native Go struct.
func (*Shape) HashContext ¶
HashContext allows cancellation during traversal of compound values.
func (*Shape) LogicalKind ¶
LogicalKind hides a compact physical representation from reflection consumers.
func (*Shape) NativeStorage ¶
NativeStorage reports whether this shape supports live native allocation. VM collection and interface layouts remain distinct from Go storage.
func (*Shape) New ¶
New allocates zeroed VM cell storage for this shape, not a native Go object. The extra cell gives even a trailing zero-sized field an address in the allocation, matching VM allocations. All writes use Go write barriers.
type Type ¶
type Type struct {
// contains filtered or unexported fields
}
Type is external metadata. Identity is by pointer, not by name.
type TypeTable ¶
type TypeTable struct {
// contains filtered or unexported fields
}
TypeTable owns the program-local identities and encoding of interface values. Entry zero is nil. The table and its descriptors are immutable after creation.
func NewTypeTable ¶
func NewTypeTable(types []DynamicType) *TypeTable
func (*TypeTable) Comparable ¶
func (*TypeTable) Index ¶
Index extracts the dynamic type ID; Unpack validates the payload. Scalar markers must be checked first: their R contains all 64 bits.
type Value ¶
func BorrowByteSlice ¶
func BorrowByteSlice(data []byte, layout *NativeLayout, budget OwnershipBudget) (Value, error)
BorrowByteSlice exposes exactly len(data) bytes. Binding declarations must describe the result's borrow domain so the compiler can own escaping values.
func BorrowNative ¶
BorrowNative retains the Go type with its address until a host value is imported.
func Bytes ¶
Bytes borrows an immutable byte span. R is its byte length, unlike a mutable VM slice whose header packs length and capacity. The external type distinguishes byte spans, strings and VM slices. The backing bytes must outlive this value.
func NativeSliceValue ¶
func NativeSliceValue(view *NativeSliceView) Value
NativeSliceValue retains a native view. Callers account for its allocation.
func NativeTime ¶
NativeTime preserves Go value semantics. Canonical UTC values keep the compact timestamp encoding; all other values retain a full native copy.
func NativeValue ¶
NativeValue retains an opaque Go value. Callers must account for the allocated interface cell. The payload is immutable; snapshots share it without copying the underlying Go value or changing pointer identity.
func Time ¶
Time stores nanoseconds in one word when they fit, with zero reserved for time.Time{}. Dates outside that range use a 12-byte integer payload (uint32 nanoseconds, int64 Unix seconds, big endian). No precision or date range is lost. Like database timestamps, the value denotes an instant, without a monotonic clock reading or timezone. AsTime returns UTC.
func TypedPair ¶
func TypedPair(id RepresentationID, p unsafe.Pointer, aux uint32) Value
TypedPair keeps a real GC-visible pointer and a 32-bit auxiliary word in one cell. The ID must come from RegisterRepresentation. Auxiliary data commonly holds the length of an immutable byte span; it is not a second pointer.
func (Value) AsNativeValue ¶
AsNativeValue requires a NativeValueKind payload.
func (Value) BorrowedNative ¶
BorrowedNative returns the typed pointer created by BorrowNative.
func (Value) HasNativeSlice ¶
HasNativeSlice reports whether the value carries per-slice physical metadata.
func (Value) IsBorrowedByteView ¶
func (Value) NativeComparable ¶
NativeComparable reports Go type comparability. A comparable struct can still contain an interface holding an uncomparable value, just as in native Go.
func (Value) NativeSlice ¶
func (v Value) NativeSlice() *NativeSliceView
NativeSlice returns a view suitable for exposing writable native memory.
func (Value) NativeTimeAddress ¶
NativeTimeAddress returns the immutable native payload, or nil for compact instants. Callers may read or copy it, but must never mutate it.
func (Value) OwnBytes ¶
OwnBytes switches all aliases to one writable array. A failed reservation leaves every view attached to the original storage.
func (Value) ReadNativeSlice ¶
func (v Value) ReadNativeSlice() *NativeSliceView
ReadNativeSlice resolves physical metadata for an immediate read. Its Data pointer is valid until an alias detaches.
func (Value) Representation ¶
func (v Value) Representation() RepresentationID
Representation returns zero for an untagged value. Concrete scalar callers must not interpret arbitrary numeric bits as tags: their external type wins.
func (Value) SliceReadData ¶
SliceReadData returns a transient address for reads or nil tests.