This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 353
/
bindata.js
394 lines (353 loc) · 13.5 KB
/
bindata.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// http://wiki.ecmascript.org/doku.php?id=harmony:binary_data
(function(global){
function ASSERT(c, m) { if (!c) { throw Error(m); } }
(function() {
var orig = Object.prototype.toString;
Object.prototype.toString = function toString() {
if (Object(this) === this && '__Class__' in this) {
return '[object ' + this.__Class__ + ']';
}
return orig.call(this, arguments);
};
}());
function Data() { throw TypeError(); }
Data.prototype = {};
Object.defineProperties(Data.prototype, {
buffer: { get: function() { return this.__Value__.buffer; }},
byteOffset: { get: function() { return this.__Value__.byteOffset; }},
byteLength: { get: function() { return this.__Value__.byteLength; }},
update: { value: function(val) {
if (this !== Object(this) || !(this instanceof Data)) { throw TypeError(); }
var r = this.__DataType__.__Convert__(val);
viewCopy(r, this.__Value__, 0, r.byteLength);
}}
});
global.Data = Data;
function Type() { throw TypeError(); }
Type.prototype = Data;
global.Type = Type;
// "block" is defined as an object with a __Value__ property (which is an ArrayBufferView)
function isBlockObject(v) { return '__Value__' in Object(v); }
function isBlockType(t) { return 'bytes' in t && '__Convert__' in t && '__Reify__' in t; }
var littleEndian = true;
// Intrinsic types
[
{name: 'int8', type: 'Int8', domain: function(x) { return -0x80 <= x && x < 0x80; } },
{name: 'int16', type: 'Int16', domain: function(x) { return -0x8000 <= x && x < 0x8000; } },
{name: 'int32', type: 'Int32', domain: function(x) { return -0x80000000 <= x && x < 0x80000000; } },
{name: 'uint8', type: 'Uint8', domain: function(x) { return 0 <= x && x <= 0xff; } },
{name: 'uint16', type: 'Uint16', domain: function(x) { return 0 <= x && x <= 0xffff; } },
{name: 'uint32', type: 'Uint32', domain: function(x) { return 0 <= x && x <= 0xffffffff; } },
{name: 'float32', type: 'Float32', domain: function(x) { return true; } },
{name: 'float64', type: 'Float64', domain: function(x) { return true; } }
].forEach(function(desc) {
var proto = Object.create(Data.prototype);
var arrayType = global[desc.type + 'Array'],
getter = 'get' + desc.type,
setter = 'set' + desc.type,
bytes = arrayType.BYTES_PER_ELEMENT;
var t = function SomeNumericType(val) {
if (this instanceof SomeNumericType) { throw TypeError("Numeric types should have value semantics"); }
var x = t.__Cast__(val);
return t.__Reify__(x);
};
//t.__proto__ = Type.prototype; // Needed for uint8 instanceof Type, but contradicts spec?
t.__DataType__ = desc.name;
t.__Convert__ = function Convert(value) {
var block = Object.create(proto);
block.__Value__ = new Uint8Array(bytes);
if (value === true) {
value = 1;
} else if (value === false) {
value = 0;
} else if (typeof value === 'number' && desc.domain(value)) {
// ok
} else {
throw TypeError("Value " + value + " is not a " + desc.name);
}
(new DataView(block.__Value__.buffer, 0, bytes))[setter](0, value, littleEndian);
return block;
};
t.__IsSame__ = function IsSame(u) {
return t.__DataType__ === Object(u).__DataType__;
};
t.__Cast__ = function Cast(val) {
var v;
try {
v = t.__Convert__(val);
return t.__Reify(v);
} catch (e) {}
if (val === Infinity || val === -Infinity || val !== val) {
return 0; // TODO: Per spec, but bogus for float types?
}
if (typeof val === 'number') {
return t.__CCast__(val);
}
if (typeof val === 'string') {
v = Number(val);
return t.__CCast__(v);
}
throw TypeError("Cannot cast " + val + " to " + desc.name);
};
t.__CCast__ = function CCast(n) {
var a = new arrayType(1);
a[0] = n;
n = a[0];
return t.__Convert__(n);
};
t.__Reify__ = function Reify(block) {
var view = block.__Value__;
return (new DataView(view.buffer, view.byteOffset, bytes))[getter](0, littleEndian);
};
t.__Class__ = 'DataType'; // TODO: Not in spec?
t.prototype = proto; // TODO: Not in spec?
t.prototype.constructor = t; // TODO: Not in spec?
Object.defineProperty(t, 'bytes', { get: function() { return bytes; }});
global[desc.name] = t;
});
function viewCopy(src, dst, offset, length) {
var srcv = new Uint8Array(src.buffer, src.byteOffset, length);
var dstv = new Uint8Array(dst.buffer, dst.byteOffset + offset, length);
for (var i = 0; i < length; ++i) {
dstv[i] = srcv[i];
}
}
function ArrayType(elementType, length) {
if (!(this instanceof ArrayType)) { throw TypeError("ArrayType cannot be called as a function."); }
var proto = Object.create(ArrayType.prototype.prototype);
length = length | 0;
if (!isBlockType(elementType)) { throw TypeError("Type is not a block type"); }
var bytes = elementType.bytes * length;
function getter(thisobj, index) {
var view = thisobj.__Value__;
var offset = elementType.bytes * index;
return elementType.__Reify__({__Value__: new Uint8Array(view.buffer, view.byteOffset + offset, elementType.bytes)});
}
function setter(thisobj, index, value) {
var src = elementType.__Convert__(value).__Value__;
var dst = thisobj.__Value__;
viewCopy(src, dst, elementType.bytes * index, elementType.bytes);
}
for (var i = 0; i < length; ++i) {
(function(index) {
Object.defineProperty(proto, index, {
get: function() {
return getter(this, index);
},
set: function(value) {
setter(this, index, value);
}
});
}(i));
}
var t = function SomeArrayType(value) {
if (!(this instanceof SomeArrayType)) { throw TypeError("Cannot call as a function"); }
if (value === void 0 || Array.isArray(value)) {
var a = t.__Construct__();
if (value !== void 0) {
var r = t.__Convert__(value);
viewCopy(r.__Value__, a.__Value__, 0, bytes);
}
return a;
} else {
var length = Number(value);
return new (new ArrayType(elementType, length)); // TODO: Return instance or type?
}
};
t.__Class__ = 'DataType';
t.__proto__ = ArrayType.prototype;
t.__DataType__ = 'ArrayType';
t.__ElementType__ = elementType;
t.__Length__ = length;
t.__Convert__ = function Convert(value) {
// TODO: Precondition checks from spec.
var block = t.__Construct__();
for (var i = 0; i < length; ++i) {
setter(block, i, value ? value[i] : void 0);
};
return block;
};
t.__IsSame__ = function IsSame(u) {
u = Object(u);
return u.__DataType__ === 'ArrayType' &&
t.__ElementType__ === u.__ElementType__ &&
t.__Length__ === u.__Length___;
};
t.__Construct__ = function Construct() {
var block = Object.create(proto);
block.__Class__ = 'Data';
block.__Value__ = new Uint8Array(bytes);
block.__DataType__ = t;
block.length = length;
return block;
};
t.__Reify__ = function Reify(block) {
var result = [];
for (var i = 0; i < length; ++i) {
result[i] = getter(block, i);
}
return result;
};
t.prototype = proto;
t.prototype.constructor = t;
t.prototype.forEach = Array.prototype.forEach;
t.prototype.fill = function fill(value) {
if (this !== Object(this) || this.__Class__ !== 'Data' || !(this.__DataType__.__IsSame__(t))) {
throw TypeError();
}
for (var i = 0; i < t.__Length__; ++i) {
setter(this, i, value);
}
};
t.prototype.cursor = function cursor(fields) {
var view = new StructView(elementType);
var index = 0;
var array = this;
return {
'next': function() {
if (index >= array.length) { throw global.StopIteration; } // TODO: Real iterator
view.setView.apply(view, [array, index].concat(fields));
++index;
return view;
}
};
};
Object.defineProperty(t, 'elementType', { get: function() { return elementType; }});
t.length = length; // TODO: Fails because t is a Function
Object.defineProperty(t, 'bytes', { get: function() { return bytes; }});
return t;
}
ArrayType.prototype = Object.create(Type.prototype);
ArrayType.prototype.constructor = ArrayType;
// TODO: ArrayType.prototype.repeat
// TODO: ArrayType.prototype.prototype.forEach (indirection?)
// TODO: ArrayType.prototype.prototype.subarray (indirection?)
function StructType(fields) {
if (!(this instanceof StructType)) { throw TypeError("StructType cannot be called as a function."); }
var proto = Object.create(Data.prototype);
var desc = {};
var bytes = 0;
Object.keys(fields).forEach(function(name) {
var type = fields[name];
if (!isBlockType(type)) { throw TypeError("Type for '" + name + "' is not a block type"); }
desc[name] = {
type: type,
offset: bytes
};
bytes += type.bytes;
});
function getter(thisobj, key) {
var field = desc[key];
var view = thisobj.__Value__;
return field.type.__Reify__({__Value__: new Uint8Array(view.buffer, view.byteOffset + field.offset, field.type.bytes)});
}
function setter(thisobj, key, value) {
var field = desc[key];
var src = field.type.__Convert__(value).__Value__;
var dst = thisobj.__Value__;
viewCopy(src, dst, field.offset, field.type.bytes);
}
Object.keys(desc).forEach(function(name) {
Object.defineProperty(proto, name, {
get: function() {
return getter(this, name);
},
set: function(value) {
setter(this, name, value);
}
});
});
var t = function SomeStructType(value) {
if (!(this instanceof SomeStructType)) { throw TypeError("objects have reference semantics"); }
return t.__Convert__(value);
};
t.__Class__ = 'DataType';
t.__proto__ = StructType.prototype;
t.__DataType__ = 'StructType';
t.__Convert__ = function Convert(value) {
var block = t.__Construct__();
Object.keys(desc).forEach(function(name) {
setter(block, name, value ? value[name] : void 0);
});
return block;
};
t.__IsSame__ = function IsSame(u) {
return t === u;
};
t.__Construct__ = function Construct() {
var block = Object.create(proto);
block.__Value__ = new Uint8Array(bytes);
block.__Class__ = 'Block';
block.__DataType__ = t;
return block;
};
t.__Reify__ = function Reify(block) {
var result = {};
Object.keys(desc).forEach(function(name) {
result[name] = getter(block, name);
});
return result;
};
t.prototype = proto;
t.prototype.constructor = t;
Object.defineProperty(t, 'fields', { get: function() {
var result = {};
Object.keys(desc).forEach(function(name) {
result[name] = desc[name].type;
});
return result;
}});
Object.defineProperty(t, 'bytes', { get: function() { return bytes; }});
// For StructView
t.__Fields__ = desc;
return t;
}
StructType.prototype = Object.create(Type.prototype);
StructType.prototype.constructor = StructType;
global.ArrayType = ArrayType;
global.StructType = StructType;
function StructView(type) {
if (!(this instanceof StructView)) { throw TypeError("StructView cannot be called as a function."); }
if (Object(type).__DataType__ !== 'StructType') { throw TypeError("Type is not a StructType"); }
// TODO: fields in StructView.prototype.setView(array, index, [...fields])
// TODO: (Lazily) define as StructType.ViewType?
function getter(array, index, key) {
var field = type.__Fields__[key];
var view = array.__Value__;
return field.type.__Reify__({__Value__: new Uint8Array(view.buffer, view.byteOffset + type.bytes * index + field.offset, field.type.bytes)});
}
function setter(array, index, key, value) {
var field = type.__Fields__[key];
var src = field.type.__Convert__(value).__Value__;
var dst = array.__Value__;
viewCopy(src, dst, field.offset + type.bytes * index, field.type.bytes);
}
var that = this;
Object.keys(type.__Fields__).forEach(function(name) {
Object.defineProperty(that, name, {
get: function() {
if (!this.__Array__) throw Error();
return getter(this.__Array__, this.__Index__, name);
},
set: function(value) {
if (!this.__Array__) throw Error();
setter(this.__Array__, this.__Index__, name, value);
}
});
});
this.__DataType__ = type; // TODO: Not in spec.
Object.defineProperty(this, 'fields', { get: function() { return type.fields; }});
Object.defineProperty(this, 'bytes', { get: function() { return type.bytes; }});
}
StructView.prototype = {};
Object.defineProperty(StructView.prototype, 'setView', { value: function(array, index) {
if (!Object(array).__DataType__) { throw TypeError(); }
if (Object(Object(array).__DataType__).__DataType__ !== 'ArrayType') { throw TypeError(); }
if (Object(Object(array).__DataType__).__ElementType__ !== this.__DataType__) { throw TypeError(); }
index = index >> 0;
if (index < 0 || index >= array.__Length__) { throw RangeError(); }
this.__Array__ = array;
this.__Index__ = index;
}});
global.StructView = StructView;
}(self));