Module: Layout

Support for translating between Buffer instances and JavaScript native types.

Layout is the basis of a class hierarchy that associates property names with sequences of encoded bytes.

Layouts are supported for these scalar (numeric) types:

and for these aggregate types:

  • Sequences of instances of a Layout, with JavaScript representation as an Array and constant or data-dependent length;
  • Structures that aggregate a heterogeneous sequence of Layout instances, with JavaScript representation as an Object;
  • Unions that support multiple variant layouts over a fixed (padded) or variable (not padded) span of bytes, using an unsigned integer at the start of the data or a separate layout element to determine which layout to use when interpreting the buffer contents;
  • BitStructures that contain a sequence of individual BitFields packed into an 8, 16, 24, or 32-bit unsigned integer starting at the least- or most-significant bit;
  • C strings of varying length;
  • Blobs of fixed- or variable-length raw data.

All Layout instances are immutable after construction, to prevent internal state from becoming inconsistent.

Author:
  • Peter A. Bigot
License:
  • MIT
Source:
See:

Classes

BitField
BitStructure
Blob
Constant
CString
Double
DoubleBE
ExternalLayout
Float
FloatBE
GreedyCount
Int
IntBE
Layout
NearInt64
NearInt64BE
NearUInt64
NearUInt64BE
OffsetLayout
Sequence
Structure
UInt
UIntBE
Union
UnionDiscriminator
UnionLayoutDiscriminator
UTF8
VariantLayout

Methods

(static) bits()

Factory for BitStructure values.

Source:

(static) blob()

Factory for Blob values.

Source:

(static) const()

Factory for Constant values.

Source:

(static) cstr()

Factory for CString values.

Source:

(static) f32()

Source:

(static) f32be()

Factory for big-endian 32-bit floating point values.

Source:

(static) f64()

Source:

(static) f64be()

Factory for big-endian 64-bit floating point values.

Source:

(static) greedy()

Factory for GreedyCount.

Source:

(static) ns64()

Factory for little-endian signed int layouts interpreted as Numbers.

Source:

(static) ns64be()

Factory for big-endian signed int layouts interpreted as Numbers.

Source:

(static) nu64()

Factory for little-endian unsigned int layouts interpreted as Numbers.

Source:

(static) nu64be()

Factory for big-endian unsigned int layouts interpreted as Numbers.

Source:

(static) offset()

Factory for OffsetLayout.

Source:

(static) s8()

Factory for signed int layouts spanning one byte.

Source:

(static) s16()

Factory for little-endian signed int layouts spanning two bytes.

Source:

(static) s16be()

Factory for big-endian signed int layouts spanning two bytes.

Source:

(static) s24()

Factory for little-endian signed int layouts spanning three bytes.

Source:

(static) s24be()

Factory for big-endian signed int layouts spanning three bytes.

Source:

(static) s32()

Factory for little-endian signed int layouts spanning four bytes.

Source:

(static) s32be()

Factory for big-endian signed int layouts spanning four bytes.

Source:

(static) s40()

Factory for little-endian signed int layouts spanning five bytes.

Source:

(static) s40be()

Factory for big-endian signed int layouts spanning five bytes.

Source:

(static) s48()

Factory for little-endian signed int layouts spanning six bytes.

Source:

(static) s48be()

Factory for big-endian signed int layouts spanning six bytes.

Source:

(static) seq()

Factory for Sequence values.

Source:

(static) struct()

Factory for Structure values.

Source:

(static) u8()

Factory for unsigned int layouts spanning one byte.

Source:

(static) u16()

Factory for little-endian unsigned int layouts spanning two bytes.

Source:

(static) u16be()

Factory for big-endian unsigned int layouts spanning two bytes.

Source:

(static) u24()

Factory for little-endian unsigned int layouts spanning three bytes.

Source:

(static) u24be()

Factory for big-endian unsigned int layouts spanning three bytes.

Source:

(static) u32()

Factory for little-endian unsigned int layouts spanning four bytes.

Source:

(static) u32be()

Factory for big-endian unsigned int layouts spanning four bytes.

Source:

(static) u40()

Factory for little-endian unsigned int layouts spanning five bytes.

Source:

(static) u40be()

Factory for big-endian unsigned int layouts spanning five bytes.

Source:

(static) u48()

Factory for little-endian unsigned int layouts spanning six bytes.

Source:

(static) u48be()

Factory for big-endian unsigned int layouts spanning six bytes.

Source:

(static) union()

Factory for Union values.

Source:

(static) unionLayoutDiscriminator()

Factory for UnionLayoutDiscriminator values.

Source:

(static) utf8()

Factory for UTF8 values.

Source:

(inner) bindConstructorLayout(Class, layout)

Augment a class so that instances can be encoded/decoded using a given layout.

Calling this function couples Class with layout in several ways:

  • Class.layout_ becomes a static member property equal to layout;
  • layout.boundConstructor_ becomes a static member property equal to Class;
  • The makeDestinationObject() property of layout is set to a function that returns a new Class();
  • Class.decode(b, offset) becomes a static member function that delegates to layout.decode. The synthesized function may be captured and extended.
  • Class.prototype.encode(b, offset) provides an instance member function that delegates to layout.encode with src set to this. The synthesized function may be captured and extended, but when the extension is invoked this must be explicitly bound to the instance.
Parameters:
Name Type Description
Class class

a JavaScript class with a nullary constructor.

layout module:Layout~Layout

the Layout instance used to encode instances of Class.

Source:

(inner) fixBitwiseResult()

JavaScript chose to define bitwise operations as operating on signed 32-bit values in 2's complement form, meaning any integer with bit 31 set is going to look negative. For right shifts that's not a problem, because >>> is a logical shift, but for every other bitwise operator we have to compensate for possible negative results.

Source: