Skip to content

Commit

Permalink
Updated an example and how endianness is handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza committed Feb 27, 2024
1 parent 2278e9d commit 51214b9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 25 deletions.
8 changes: 4 additions & 4 deletions examples/customSchema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

import { Parsed, object } from 'typed-binary';
import { writeAndRead } from '../__util';
import { RADIANS } from './radians';
import { radians } from './radians';

/*
* ROTATION
*/

type Rotation = Parsed<typeof Rotation>;
const Rotation = object({
roll: RADIANS,
pitch: RADIANS,
yaw: RADIANS,
roll: radians,
pitch: radians,
yaw: radians,
});

console.log(
Expand Down
9 changes: 2 additions & 7 deletions examples/customSchema/radians.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ISerialInput,
ISerialOutput,
Schema,
IRefResolver,
MaxValue,
IMeasurer,
Measurer,
Expand All @@ -12,10 +11,6 @@ import {
* A schema storing radians with 2 bytes of precision.
*/
class RadiansSchema extends Schema<number> {
resolveReferences(ctx: IRefResolver): void {
// No inner references to resolve
}

read(input: ISerialInput): number {
const low = input.readByte();
const high = input.readByte();
Expand All @@ -27,7 +22,7 @@ class RadiansSchema extends Schema<number> {
write(output: ISerialOutput, value: number): void {
// The value will be wrapped to be in range of [0, Math.PI)
const wrapped = ((value % Math.PI) + Math.PI) % Math.PI;
// Discretising the value to be ints in range of [0, 65535]
// Clipping the value to be ints in range of [0, 65535]
const discrete = Math.min(Math.floor((wrapped / Math.PI) * 65535), 65535);

const low = discrete & 0xff;
Expand All @@ -47,4 +42,4 @@ class RadiansSchema extends Schema<number> {
}
}

export const RADIANS = new RadiansSchema();
export const radians = new RadiansSchema();
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './describe';
export * from './io';
export * from './error';

export { getSystemEndianness } from './util';
export type { Parsed, ParseUnwrapped } from './utilityTypes';
20 changes: 13 additions & 7 deletions src/io/bufferIOBase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isBigEndian } from '../util';
import { getSystemEndianness } from '../util';
import { Endianness } from './types';

export type BufferIOOptions = {
/**
Expand All @@ -8,7 +9,7 @@ export type BufferIOOptions = {
/**
* @default 'system'
*/
endianness: 'big' | 'little' | 'system';
endianness: Endianness | 'system';
};

export class BufferIOBase {
Expand All @@ -21,6 +22,8 @@ export class BufferIOBase {

protected byteOffset = 0;

public readonly endianness: Endianness;

constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
this.byteOffset = options?.byteOffset ?? 0;

Expand All @@ -38,11 +41,14 @@ export class BufferIOBase {
this.helperFloatView = new Float32Array(helperBuffer);
this.helperByteView = new Uint8Array(helperBuffer);

const isSystemBigEndian = isBigEndian();
const endianness = options?.endianness ?? 'system';
this.switchEndianness =
(endianness === 'big' && !isSystemBigEndian) ||
(endianness === 'little' && isSystemBigEndian);
const systemEndianness = getSystemEndianness();

this.endianness =
!options || options.endianness === 'system'
? systemEndianness
: options.endianness;

this.switchEndianness = this.endianness !== systemEndianness;
}

get currentByteOffset() {
Expand Down
3 changes: 1 addition & 2 deletions src/io/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { ISerialInput, ISerialOutput, IMeasurer } from './types';
export { Endianness, ISerialInput, ISerialOutput, IMeasurer } from './types';
export { BufferWriter } from './bufferWriter';
export { BufferReader } from './bufferReader';
export { Measurer } from './measurer';
export { isBigEndian } from '../util';
4 changes: 4 additions & 0 deletions src/io/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Endianness = 'big' | 'little';

export interface ISerialInput {
readBool(): boolean;
readByte(): number;
Expand All @@ -7,6 +9,7 @@ export interface ISerialInput {
readString(): string;
seekTo(offset: number): void;
skipBytes(bytes: number): void;
readonly endianness: Endianness;
readonly currentByteOffset: number;
}

Expand All @@ -19,6 +22,7 @@ export interface ISerialOutput {
writeString(value: string): void;
seekTo(offset: number): void;
skipBytes(bytes: number): void;
readonly endianness: Endianness;
readonly currentByteOffset: number;
}

Expand Down
14 changes: 9 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/**
* @returns {Boolean} true if system is big endian
*/
export function isBigEndian(): boolean {
const array = new Uint8Array(4);
const view = new Uint32Array(array.buffer);
function isSystemBigEndian(): boolean {
const array = new Uint8Array(4);
const view = new Uint32Array(array.buffer);

return !((view[0] = 1) & array[0]);
}
return !((view[0] = 1) & array[0]);
}

export function getSystemEndianness() {
return isSystemBigEndian() ? 'big' : 'little';
}

0 comments on commit 51214b9

Please sign in to comment.