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:
- Unsigned integers in little-endian format with 8-bit, 16-bit, 24-bit, 32-bit, 40-bit, and 48-bit representation ranges;
- Unsigned integers in big-endian format with 16-bit, 24-bit, 32-bit, 40-bit, and 48-bit representation ranges;
- Signed integers in little-endian format with 8-bit, 16-bit, 24-bit, 32-bit, 40-bit, and 48-bit representation ranges;
- Signed integers in big-endian format with 16-bit, 24-bit, 32-bit, 40-bit, and 48-bit representation ranges;
- 64-bit integral values that decode to an exact (if magnitude is less than 2^53) or nearby integral Number in unsigned little-endian, unsigned big-endian, signed little-endian, and unsigned big-endian encodings;
- 32-bit floating point values with little-endian and big-endian representations;
- 64-bit floating point values with little-endian and big-endian representations;
- Constants that take no space in the encoded expression.
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.
- 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.
(static) blob()
Factory for Blob values.
(static) const()
Factory for Constant values.
(static) cstr()
Factory for CString values.
(static) f32()
Factory for little-endian 32-bit floating point values.
(static) f32be()
Factory for big-endian 32-bit floating point values.
(static) f64()
Factory for little-endian 64-bit floating point values.
(static) f64be()
Factory for big-endian 64-bit floating point values.
(static) greedy()
Factory for GreedyCount.
(static) ns64()
Factory for little-endian signed int layouts interpreted as Numbers.
(static) ns64be()
Factory for big-endian signed int layouts interpreted as Numbers.
(static) nu64()
Factory for little-endian unsigned int layouts interpreted as Numbers.
(static) nu64be()
Factory for big-endian unsigned int layouts interpreted as Numbers.
(static) offset()
Factory for OffsetLayout.
(static) s8()
Factory for signed int layouts spanning one byte.
(static) s16()
Factory for little-endian signed int layouts spanning two bytes.
(static) s16be()
Factory for big-endian signed int layouts spanning two bytes.
(static) s24()
Factory for little-endian signed int layouts spanning three bytes.
(static) s24be()
Factory for big-endian signed int layouts spanning three bytes.
(static) s32()
Factory for little-endian signed int layouts spanning four bytes.
(static) s32be()
Factory for big-endian signed int layouts spanning four bytes.
(static) s40()
Factory for little-endian signed int layouts spanning five bytes.
(static) s40be()
Factory for big-endian signed int layouts spanning five bytes.
(static) s48()
Factory for little-endian signed int layouts spanning six bytes.
(static) s48be()
Factory for big-endian signed int layouts spanning six bytes.
(static) seq()
Factory for Sequence values.
(static) struct()
Factory for Structure values.
(static) u8()
Factory for unsigned int layouts spanning one byte.
(static) u16()
Factory for little-endian unsigned int layouts spanning two bytes.
(static) u16be()
Factory for big-endian unsigned int layouts spanning two bytes.
(static) u24()
Factory for little-endian unsigned int layouts spanning three bytes.
(static) u24be()
Factory for big-endian unsigned int layouts spanning three bytes.
(static) u32()
Factory for little-endian unsigned int layouts spanning four bytes.
(static) u32be()
Factory for big-endian unsigned int layouts spanning four bytes.
(static) u40()
Factory for little-endian unsigned int layouts spanning five bytes.
(static) u40be()
Factory for big-endian unsigned int layouts spanning five bytes.
(static) u48()
Factory for little-endian unsigned int layouts spanning six bytes.
(static) u48be()
Factory for big-endian unsigned int layouts spanning six bytes.
(static) union()
Factory for Union values.
(static) unionLayoutDiscriminator()
Factory for UnionLayoutDiscriminator values.
(static) utf8()
Factory for UTF8 values.
(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 tolayout
;layout.boundConstructor_
becomes a static member property equal toClass
;- The makeDestinationObject()
property of
layout
is set to a function that returns anew 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 withsrc
set tothis
. The synthesized function may be captured and extended, but when the extension is invokedthis
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 |
(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.