-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (201 loc) · 6.33 KB
/
index.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* @flow */
import Backing from "backing";
import {TypedObject} from "../";
import {alignTo} from "../../util";
import type {Realm} from "../../";
import {
$ValueType,
$Backing,
$Address,
$CanBeEmbedded,
$CanBeReferenced,
$CanContainReferences
} from "../../symbols";
/**
* Makes a UnionType type class for the given realm.
*/
export function make (realm: Realm): TypeClass<UnionType<any>> {
const {TypeClass, registry} = realm;
const idRange = registry.range('UnionType');
return new TypeClass('UnionType', (...possibleTypes: Type[]) => {
return (Union: Function): Object => {
const name = possibleTypes.map(PossibleType => PossibleType.name).join(' | ');
const id = idRange.next();
Union[$CanBeEmbedded] = true;
Union[$CanBeReferenced] = false;
Union[$CanContainReferences] = possibleTypes.some((type: $Fixme<Type>): boolean => type[$CanContainReferences]);
const byteAlignment = Math.max(4, ...possibleTypes.map(type => type.byteAlignment));
const byteLength = alignTo(Math.max(...possibleTypes.map(type => type.byteLength)) + 4, byteAlignment);
const idOffset = byteLength - 4;
let UnionArray;
// @flowIssue 285
Object.defineProperties(Union, {
Array: {
get () {
if (UnionArray === undefined) {
UnionArray = new realm.ArrayType(Union);
}
return UnionArray;
}
}
});
const prototype = Object.create(null, {
// @flowIssue 285
value: {
enumerable: true,
get (): any {
const backing = this[$Backing];
const address = this[$Address];
const typeId = backing.getUint32(address + idOffset);
if (typeId === 0) {
return null;
}
else {
return realm.I[typeId].load(backing, address);
}
},
set (value: any): void {
const backing = this[$Backing];
const address = this[$Address];
storeUnion(backing, address, value);
}
},
// @flowIssue 285
type: {
enumerable: true,
get (): ?Type {
const typeId = this[$Backing].getUint32(this[$Address] + idOffset);
return typeId === 0 ? null : realm.I[typeId];
}
},
inspect: {
value () {
return this.value;
}
},
valueOf: {
value () {
return this.value;
}
},
toJSON: {
value () {
const value = this.value;
if (value != null && typeof value === 'object' && typeof value.toJSON === 'function') {
return value.toJSON();
}
else {
return value;
}
}
}
});
function constructor (backingOrInput: Backing|any, address?: float64) {
if (backingOrInput instanceof Backing) {
this[$Backing] = backingOrInput;
this[$Address] = address;
}
else {
const backing = realm.backing;
this[$Backing] = backing;
this[$Address] = backing.gc.alloc(byteLength, Union.id);
initializeUnion(backing, this[$Address], backingOrInput);
}
}
function unionAccepts (input: any): boolean {
for (let i = 0; i < possibleTypes.length; i++) {
const type = possibleTypes[i];
if (type.accepts(input)) {
return true;
}
}
return false;
}
function initializeUnion (backing: Backing, address: float64, initialValue?: any): void {
if (initialValue == null) {
backing.setUint32(address + idOffset, 0);
}
else {
const type = typeFor(initialValue);
type.initialize(backing, address, initialValue);
backing.setUint32(address + idOffset, type.id);
}
}
function storeUnion (backing: Backing, address: float64, value: any): void {
const existing = backing.getUint32(address + idOffset);
if (existing !== 0) {
realm.I[existing].clear(backing, address);
}
else if (value == null) {
// nothing to do.
return;
}
if (value == null) {
backing.setUint32(address + idOffset, 0);
}
else {
const type = typeFor(value);
type.initialize(backing, address, value);
backing.setUint32(address + idOffset, type.id);
}
}
function loadUnion (backing: Backing, address: float64): any {
const typeId = backing.getUint32(address + idOffset);
if (typeId === 0) {
return null;
}
else {
return realm.I[typeId].load(backing, address);
}
}
function clearUnion (backing: Backing, address: float64): void {
const typeId = backing.getUint32(address + idOffset);
if (typeId !== 0) {
backing.setUint32(backing, address + idOffset, 0);
realm.I[typeId].clear(backing, address);
}
}
function unionDestructor (backing: Backing, address: float64): void {
const typeId = backing.getUint32(address + idOffset);
if (typeId !== 0) {
realm.I[typeId].clear(backing, address);
}
}
function typeFor (input: any): Type {
for (let i = 0; i < possibleTypes.length; i++) {
const type = possibleTypes[i];
if (type.accepts(input)) {
return type;
}
}
throw new TypeError(`Union does not contain a type which can accept the given input.`);
}
return {
id,
name,
byteLength,
byteAlignment,
constructor,
prototype,
accepts: unionAccepts,
initialize: initializeUnion,
store: storeUnion,
load: loadUnion,
clear: clearUnion,
destructor: unionDestructor,
equal (a: any, b: any): any {
return typeFor(a).equal(a, b);
},
emptyValue (): null {
return null;
},
randomValue (): any {
return possibleTypes[Math.floor(Math.random() * possibleTypes.length)].randomValue();
},
hashValue (input: any): uint32 {
return typeFor(input).hashValue(input);
}
};
};
});
};