-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathint.ts
More file actions
28 lines (23 loc) · 811 Bytes
/
int.ts
File metadata and controls
28 lines (23 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import * as assert from 'assert';
import { Node } from './base';
function buildName(field: string, bytes: number, signed: boolean, littleEndian: boolean) {
const type = signed ? 'int' : 'uint';
const bits = bytes * 8;
const endianness = littleEndian ? 'le' : 'be';
if (bytes > 1) {
return `${field}_${type}_${bits}_${endianness}`;
} else {
return `${field}_${type}_${bits}`;
}
}
export class Int extends Node {
/**
* @param field State's property name
*/
constructor(public readonly field: string, public readonly bytes: number, public readonly signed: boolean, public readonly littleEndian: boolean) {
super(buildName(field, bytes, signed, littleEndian));
if (/^_/.test(field)) {
throw new Error(`Can't use internal field in \`Int\`: "${field}"`);
}
}
}