Skip to main content

Interface: StructDef

A struct described with defineStruct: packs a plain object into struct bytes and unpacks them back.

It is an AdvancedType, so it doubles as a dlopen argument or return type — its toBuffer is StructDef.pack and its fromBuffer is StructDef.unpack — and a struct is then passed and returned by value.

Extends

Properties

align

readonly align: number

Alignment of the struct in bytes.


ffiType

readonly ffiType: SimpleType

Inherited from

AdvancedType.ffiType


ffiTypeStruct

readonly ffiTypeStruct: SimpleType<StructValues>

Inherited from

AdvancedType.ffiTypeStruct


fields

readonly fields: [string, object][]

The fields as [ name, type ] pairs, with each type resolved to a type object — a string alias appears as the types entry it names, and a [ elementType ] field as an object whose name is '<element>[]'. Fields dropped by a StructFieldOptions.condition are not listed.


name

readonly name: string

Inherited from

AdvancedType.name


size

readonly size: number

Size of the struct in bytes, padding included.

Overrides

AdvancedType.size

Methods

describe()

describe(): StructFieldInfo[]

The layout, field by field: the offsets, sizes and type names libffi computed. The place to look when a struct does not agree with C.

Returns

StructFieldInfo[]


fromBuffer()

fromBuffer(buffer, ctx?): T

Parameters

buffer

Uint8Array

ctx?

Returns

T

Inherited from

AdvancedType.fromBuffer


pack()

pack(obj): Uint8Array

Struct bytes for obj, size bytes long.

Parameters

obj

StructValues

Returns

Uint8Array


packListInto()

packListInto(objects, buf, offset?): void

Pack a run of structs back to back into a buffer the caller owns, without a buffer per element. Padding between fields keeps whatever the buffer already held.

Parameters

objects

readonly StructValues[]

The structs to write.

buf

ArrayBuffer | ArrayBufferView<ArrayBufferLike>

Buffer to write into; must have room for all of them.

offset?

number

Byte offset to start at.

Returns

void


toBuffer()

toBuffer(data, ctx?): Uint8Array

Parameters

data

T

ctx?

Returns

Uint8Array

Inherited from

AdvancedType.toBuffer


unpack()

unpack(buf): StructValues

The object buf holds. Reads the struct at offset 0.

Parameters

buf

ArrayBuffer | ArrayBufferView<ArrayBufferLike>

Returns

StructValues


unpackInto()

unpackInto<T>(buf, target, offset?): T & StructValues

Write the fields into an object the caller owns and return it, rather than allocating a result object. Only the struct's own fields are assigned; anything else on target is left alone.

Type Parameters

T

T extends object

Parameters

buf

ArrayBuffer | ArrayBufferView<ArrayBufferLike>

Bytes to read.

target

T

Object to assign the fields to.

offset?

number

Byte offset of the struct within buf.

Returns

T & StructValues


unpackList()

unpackList(buf, count): StructValues[]

Unpack count structs packed back to back, from offset 0.

Parameters

buf

ArrayBuffer | ArrayBufferView<ArrayBufferLike>

count

number

Returns

StructValues[]