Skip to content

Primitives & Structs

Aegis.js provides a compile-time schema builder that mirrors low-level C struct declarations while providing full TypeScript type inference and runtime validation.


Available Primitives

All primitives map directly to IEEE floating-point standards, signed/unsigned two's complement integers, boolean bitfields, or fixed-length byte arrays:

PrimitiveByte SizeAlignmentTypeScript TypeDescription
uint81 byte1 bytenumberUnsigned 8-bit integer (0 to 255)
int81 byte1 bytenumberSigned 8-bit integer (-128 to 127)
uint162 bytes2 bytesnumberUnsigned 16-bit integer (0 to 65,535)
int162 bytes2 bytesnumberSigned 16-bit integer (-32,768 to 32,767)
uint324 bytes4 bytesnumberUnsigned 32-bit integer (0 to 4,294,967,295)
int324 bytes4 bytesnumberSigned 32-bit integer (-2,147,483,648 to 2,147,483,647)
bigint648 bytes8 bytesbigintSigned 64-bit integer
biguint648 bytes8 bytesbigintUnsigned 64-bit integer
float324 bytes4 bytesnumberIEEE 754 single-precision float (32-bit)
float648 bytes8 bytesnumberIEEE 754 double-precision float (64-bit)
boolean1 byte1 bytebooleanBoolean flag (0 = false, 1 = true)
fixedString(N)N bytes1 bytestringFixed-length UTF-8 / ASCII string
fixedBytes(N)N bytes1 byteUint8ArrayFixed-length raw binary byte slice

Defining a Struct

Use Aegis.struct({ ... }) to define a schema:

typescript
import { 
  Aegis, 
  uint32, 
  float64, 
  boolean, 
  fixedString, 
  biguint64 
} from '@aventine/aegis-js';

export const TradeRecord = Aegis.struct({
  tradeId: biguint64,
  timestamp: biguint64,
  price: float64,
  quantity: uint32,
  isBuyerMaker: boolean,
  symbol: fixedString(8),
});

Custom Domain Methods

You can attach custom computed properties and methods directly to your struct definition without increasing memory size:

typescript
export const Position = Aegis.struct(
  {
    assetId: uint32,
    shares: float64,
    entryPrice: float64,
    currentPrice: float64,
  },
  {
    // Custom domain methods accessible on any cursor
    marketValue(cursor) {
      return cursor.shares * cursor.currentPrice;
    },
    unrealizedPnL(cursor) {
      return cursor.shares * (cursor.currentPrice - cursor.entryPrice);
    },
    returnOnInvestment(cursor) {
      return ((cursor.currentPrice - cursor.entryPrice) / cursor.entryPrice) * 100;
    },
  }
);

When you inspect a position with a cursor, you call these domain methods directly:

typescript
const pos = positions.get(0);
console.log(`Market Value: $${pos.marketValue()}`);
console.log(`Unrealized PnL: $${pos.unrealizedPnL()}`);
console.log(`ROI: ${pos.returnOnInvestment().toFixed(2)}%`);

Zero heap allocation occurs. The methods execute against the existing cursor slider.

Released under the Apache 2.0 License. Built for sovereign silicon performance.