Skip to content

Commit 7337145

Browse files
committed
Use the type registry to allocate id ranges
1 parent 857c084 commit 7337145

File tree

11 files changed

+42
-74
lines changed

11 files changed

+42
-74
lines changed

src/type-class/array-type/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export type MapVisitor = (element: any, index: uint32, context: BaseArray) => an
2222
export type FilterVisitor = (element: any, index: uint32, context: BaseArray) => boolean;
2323
export type Reducer = (accumulator: any, element: any, index: uint32, context: BaseArray) => any;
2424

25-
export const MIN_TYPE_ID = Math.pow(2, 20) * 4;
26-
2725
export class BaseArray extends TypedObject {
2826

2927
BYTES_PER_ELEMENT: uint32;
@@ -195,8 +193,8 @@ function ensureSlots (min: uint32) {
195193
* Makes a TypedArray type class for a given realm.
196194
*/
197195
export function make (realm: Realm): TypeClass<ArrayType<any>> {
198-
const {TypeClass, ReferenceType, backing} = realm;
199-
let typeCounter = 0;
196+
const {TypeClass, ReferenceType, backing, registry} = realm;
197+
const idRange = registry.range('ArrayType');
200198
return new TypeClass('ArrayType', (ElementType: Type, config: Object = {}): Function => {
201199
return (Partial: Class<TypedArray<ElementType>>): Object => {
202200
// @flowIssue 252
@@ -208,12 +206,11 @@ export function make (realm: Realm): TypeClass<ArrayType<any>> {
208206
// @flowIssue 252
209207
Partial[$CanContainReferences] = ElementType[$CanContainReferences];
210208
let MultidimensionalArray;
211-
const name = typeof config.name === 'string' ? config.name : (typeof ElementType.name === 'string' && ElementType.name.length) ? `Array<${ElementType.name}>` : `%Array<0x${typeCounter.toString(16)}>`;
209+
const name = typeof config.name === 'string' ? config.name : (typeof ElementType.name === 'string' && ElementType.name.length) ? `Array<${ElementType.name}>` : `%Array<0x${idRange.value.toString(16)}>`;
212210
if (realm.T[name]) {
213211
return realm.T[name];
214212
}
215-
typeCounter++;
216-
const id = typeof config.id === 'number' && config.id > 0 ? config.id : MIN_TYPE_ID + typeCounter;
213+
const id = typeof config.id === 'number' && config.id > 0 ? config.id : idRange.next();
217214

218215

219216
// @flowIssue 285

src/type-class/enum-type/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ import {
1616
$CanContainReferences
1717
} from "../../symbols";
1818

19-
export const MIN_TYPE_ID = Math.pow(2, 20) * 9;
20-
2119
/**
2220
* Makes a EnumType type class for the given realm.
2321
*/
2422
export function make (realm: Realm): TypeClass<EnumType<any>> {
25-
const {TypeClass} = realm;
26-
let typeCounter = 0;
23+
const {TypeClass, registry} = realm;
24+
const idRange = registry.range('EnumType');
2725

2826
function createTinyEnumType (Enum: Function, possibleValues: Type[]): Object {
2927
const name = possibleValues.map(value => inspect(value)).join(' | ');
30-
const id = MIN_TYPE_ID + typeCounter;
28+
const id = idRange.next();
3129
const byteAlignment = 1;
3230
const byteLength = 1;
3331

@@ -114,7 +112,7 @@ export function make (realm: Realm): TypeClass<EnumType<any>> {
114112

115113
function createLargeEnumType (Enum: Function, possibleValues: Type[]): Object {
116114
const name = possibleValues.map(value => inspect(value)).join(' | ');
117-
const id = MIN_TYPE_ID + typeCounter;
115+
const id = idRange.next();
118116
const byteAlignment = 2;
119117
const byteLength = 2;
120118
const valueMap = new Map(possibleValues.map((value, index) => [value, index]));
@@ -196,8 +194,6 @@ export function make (realm: Realm): TypeClass<EnumType<any>> {
196194

197195
return new TypeClass('EnumType', (...possibleValues: Type[]) => {
198196
return (Enum: Function): Object => {
199-
typeCounter++;
200-
201197
let EnumArray;
202198
// @flowIssue 285
203199
Object.defineProperties(Enum, {

src/type-class/hash-map-type/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "../../symbols";
1919

2020

21+
2122
export class HashMap<K, V> extends TypedObject {
2223

2324
/**
@@ -66,15 +67,12 @@ const CARDINALITY_OFFSET = 12;
6667

6768
const INITIAL_BUCKET_COUNT = 16;
6869

69-
export const MIN_TYPE_ID = Math.pow(2, 20) * 6;
70-
71-
7270
/**
7371
* Makes a HashMapType type class for the given realm.
7472
*/
7573
export function make (realm: Realm): TypeClass<HashMapType<Type, Type>> {
76-
const {TypeClass, StructType, T, backing} = realm;
77-
let typeCounter = 0;
74+
const {TypeClass, StructType, T, backing, registry} = realm;
75+
const idRange = registry.range('HashMapType');
7876
return new TypeClass('HashMapType', (KeyType: Type, ValueType: Type, config: Object = {}): Function => {
7977
return (Partial: Function) => {
8078

@@ -85,8 +83,7 @@ export function make (realm: Realm): TypeClass<HashMapType<Type, Type>> {
8583
return realm.T[name];
8684
}
8785

88-
typeCounter++;
89-
const id = typeof config.id === 'number' ? config.id : MIN_TYPE_ID + typeCounter;
86+
const id = typeof config.id === 'number' ? config.id : idRange.next();
9087

9188
type AcceptableInput = Map|TypedHashMap<KeyType, ValueType>|Array<[KeyType, ValueType]>|Object;
9289

src/type-class/hash-set-type/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,12 @@ const CARDINALITY_OFFSET = 12;
6262

6363
const INITIAL_BUCKET_COUNT = 16;
6464

65-
export const MIN_TYPE_ID = Math.pow(2, 20) * 7;
66-
67-
6865
/**
6966
* Makes a HashSetType type class for the given realm.
7067
*/
7168
export function make (realm: Realm): TypeClass<HashSetType<Type, Type>> {
72-
const {TypeClass, StructType, ReferenceType, T, backing} = realm;
73-
let typeCounter = 0;
69+
const {TypeClass, StructType, ReferenceType, T, backing, registry} = realm;
70+
const idRange = registry.range('HashSetType');
7471
return new TypeClass('HashSetType', (EntryType: Type, config: Object = {}): Function => {
7572
return (Partial: Function) => {
7673

@@ -79,8 +76,7 @@ export function make (realm: Realm): TypeClass<HashSetType<Type, Type>> {
7976
return realm.T[name];
8077
}
8178

82-
typeCounter++;
83-
const id = typeof config.id === 'number' ? config.id : MIN_TYPE_ID + typeCounter;
79+
const id = typeof config.id === 'number' ? config.id : idRange.next();
8480

8581
type AcceptableInput = Set|TypedHashSet<EntryType>|EntryType|Object;
8682

src/type-class/object-type/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ import {
1111
$CanContainReferences
1212
} from "../../symbols";
1313

14-
export const MIN_TYPE_ID = Math.pow(2, 20) * 5;
15-
1614
/**
1715
* Makes an ObjectType type class for the given realm.
1816
*/
1917
export function make (realm: Realm): TypeClass<ObjectType<Object>> {
20-
const {TypeClass, ReferenceType} = realm;
21-
let typeCounter = 0;
18+
const {TypeClass, ReferenceType, registry} = realm;
19+
const idRange = registry.range('ObjectType');
2220
return new TypeClass('ObjectType', (config: Object): Function => {
2321
return (Partial: Function): Object => {
24-
typeCounter++;
25-
const name = config.name || `%Object<0x${typeCounter.toString(16)}>`;
26-
const id = config.id || (MIN_TYPE_ID + typeCounter);
22+
const name = config.name || `%Object<0x${idRange.value.toString(16)}>`;
23+
const id = config.id || idRange.next();
2724

2825
Partial[$CanBeEmbedded] = false;
2926
Partial[$CanBeReferenced] = true;

src/type-class/primitive-type/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@ import {
1313
$CanContainReferences
1414
} from "../../symbols";
1515

16-
export const MIN_TYPE_ID = 1;
17-
1816
export class Primitive extends TypedObject {}
1917

2018
/**
2119
* Makes a PrimitiveType type class for a given realm.
2220
*/
2321
export function make (realm: Realm): TypeClass<PrimitiveType<any>> {
24-
const {TypeClass, backing} = realm;
25-
let typeCounter = 0;
22+
const {TypeClass, backing, registry} = realm;
23+
const idRange = registry.range('PrimitiveType');
2624

2725
return new TypeClass('PrimitiveType', (config: Object): Function => {
2826
return (primitive: PrimitiveType<any>): Object => {
29-
typeCounter++;
30-
const name = config.name || `%Primitive<0x${typeCounter.toString(16)}>`;
31-
const id = config.id || (MIN_TYPE_ID + typeCounter);
27+
const name = config.name || `%Primitive<0x${idRange.value.toString(16)}>`;
28+
const id = config.id || idRange.next();
3229

3330
// @flowIssue 252
3431
primitive[$CanBeEmbedded] = true;

src/type-class/reference-type/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@ import {
1212
$CanContainReferences
1313
} from "../../symbols";
1414

15-
export const MIN_TYPE_ID = Math.pow(2, 20) * 3;
16-
1715
/**
1816
* Makes a ReferenceType type class for the given realm.
1917
*/
2018
export function make (realm: Realm): TypeClass<ReferenceType<any>> {
21-
const {TypeClass} = realm;
22-
let typeCounter = 0;
19+
const {TypeClass, registry} = realm;
20+
const idRange = registry.range('ReferenceType');
2321
return new TypeClass('ReferenceType', (Target: Function): Function => {
2422

2523
return (Reference: Function): Object => {
26-
typeCounter++;
27-
const name = typeof Target.name === 'string' && Target.name.length > 0 ? `Reference<${Target.name}>` : `%Reference<0x${typeCounter.toString(16)}>`;
28-
const id = MIN_TYPE_ID + typeCounter;
24+
const id = idRange.next();
25+
const name = typeof Target.name === 'string' && Target.name.length > 0 ? `Reference<${Target.name}>` : `%Reference<0x${id.toString(16)}>`;
2926

3027
Reference[$CanBeEmbedded] = true;
3128
Reference[$CanBeReferenced] = false;

src/type-class/string-type/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ import {
1212
$CanContainReferences
1313
} from "../../symbols";
1414

15-
export const MIN_TYPE_ID = Math.pow(2, 20) * 1;
16-
1715
/**
1816
* Makes a StringType type class for the given realm.
1917
*/
2018
export function make (realm: Realm): TypeClass<PrimitiveType<string>> {
21-
const {TypeClass} = realm;
22-
let typeCounter = 0;
19+
const {TypeClass, registry} = realm;
20+
const idRange = registry.range('StringType');
2321
return new TypeClass('StringType', (config: Object): Function => {
2422
return (Partial: Function): Object => {
25-
typeCounter++;
26-
const name = config.name || `%String<0x${typeCounter.toString(16)}>`;
27-
const id = config.id || (MIN_TYPE_ID + typeCounter);
23+
const id = config.id || idRange.next();
24+
const name = config.name || `%String<0x${id.toString(16)}>`;
2825

2926
Partial[$CanBeEmbedded] = false;
3027
Partial[$CanBeReferenced] = true;

src/type-class/struct-type/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,16 @@ import {
4141
$CanContainReferences
4242
} from "../../symbols";
4343

44-
export const MIN_TYPE_ID = Math.pow(2, 20) * 2;
45-
4644
export class Struct extends TypedObject {}
4745

4846
export function make (realm: Realm): TypeClass<StructType<any>> {
49-
const {TypeClass, ReferenceType, backing} = realm;
50-
let typeCounter = 0;
47+
const {TypeClass, ReferenceType, backing, registry} = realm;
48+
const idRange = registry.range('StructType');
5149
return new TypeClass('StructType', function (fields?: Type|StructFieldsConfig, lengthOrOptions?: number| StructOptions, options?: StructOptions) {
5250

5351
return (Partial: Function) => {
5452

55-
typeCounter++;
56-
57-
const capturedTypeCount = typeCounter;
53+
const capturedId = idRange.next();
5854

5955
type Metadata = {
6056
byteLength: uint32;
@@ -69,7 +65,7 @@ export function make (realm: Realm): TypeClass<StructType<any>> {
6965
Object.defineProperties(Partial, {
7066
name: {
7167
configurable: true,
72-
value: `%Struct<0x${typeCounter.toString(16)}>`
68+
value: `%Struct<0x${capturedId.toString(16)}>`
7369
},
7470
flowType: {
7571
configurable: true,
@@ -184,10 +180,10 @@ export function make (realm: Realm): TypeClass<StructType<any>> {
184180

185181
Object.defineProperties(Partial, {
186182
id: {
187-
value: options.id || (MIN_TYPE_ID + capturedTypeCount)
183+
value: options.id || capturedId
188184
},
189185
name: {
190-
value: options.name || `%StructType<0x${capturedTypeCount.toString(16)}>`
186+
value: options.name || `%StructType<0x${capturedId.toString(16)}>`
191187
},
192188
byteLength: {
193189
value: metadata.byteLength

src/type-class/union-type/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ import {
1515
$CanContainReferences
1616
} from "../../symbols";
1717

18-
export const MIN_TYPE_ID = Math.pow(2, 20) * 8;
19-
2018
/**
2119
* Makes a UnionType type class for the given realm.
2220
*/
2321
export function make (realm: Realm): TypeClass<UnionType<any>> {
24-
const {TypeClass} = realm;
25-
let typeCounter = 0;
22+
const {TypeClass, registry} = realm;
23+
const idRange = registry.range('UnionType');
24+
2625
return new TypeClass('UnionType', (...possibleTypes: Type[]) => {
2726
return (Union: Function): Object => {
28-
typeCounter++;
2927
const name = possibleTypes.map(PossibleType => PossibleType.name).join(' | ');
30-
const id = MIN_TYPE_ID + typeCounter;
28+
const id = idRange.next();
3129

3230
Union[$CanBeEmbedded] = true;
3331
Union[$CanBeReferenced] = false;

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Backing from "backing";
22
import {Realm} from "../src";
33

4-
describeBacking('TypeRealm', function (options) {
4+
describeBacking('Realm', function (options) {
55
let backing;
66
let realm;
77
let T;

0 commit comments

Comments
 (0)