-
-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathdataview.ts
More file actions
181 lines (154 loc) · 6.81 KB
/
dataview.ts
File metadata and controls
181 lines (154 loc) · 6.81 KB
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
import { BLOCK_MAXSIZE } from "./rt/common";
import { ArrayBuffer } from "./arraybuffer";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH } from "./util/error";
// TODO: there is probably a smarter way to check byteOffset for accesses larger than 1 byte
export class DataView {
readonly buffer: ArrayBuffer;
@unsafe readonly dataStart: usize;
readonly byteLength: i32;
get byteOffset(): i32 {
return <i32>(this.dataStart - changetype<usize>(this.buffer));
}
constructor(
buffer: ArrayBuffer,
byteOffset: i32 = 0,
byteLength: i32 = buffer.byteLength
) {
if (
i32(<u32>byteLength > <u32>BLOCK_MAXSIZE) |
i32(<u32>byteOffset + byteLength > <u32>buffer.byteLength)
) throw new RangeError(E_INVALIDLENGTH);
this.buffer = buffer; // links
let dataStart = changetype<usize>(buffer) + <usize>byteOffset;
this.dataStart = dataStart;
this.byteLength = byteLength;
}
getFloat32(byteOffset: i32, littleEndian: bool = false): f32 {
if (
(byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
return littleEndian
? load<f32>(this.dataStart + <usize>byteOffset)
: reinterpret<f32>(bswap<u32>(load<u32>(this.dataStart + <usize>byteOffset)));
}
getFloat64(byteOffset: i32, littleEndian: bool = false): f64 {
if (
(byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
return littleEndian
? load<f64>(this.dataStart + <usize>byteOffset)
: reinterpret<f64>(bswap<u64>(load<u64>(this.dataStart + <usize>byteOffset)));
}
getInt8(byteOffset: i32): i8 {
if (<u32>byteOffset >= <u32>this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i8>(this.dataStart + <usize>byteOffset);
}
getInt16(byteOffset: i32, littleEndian: bool = false): i16 {
if (
(byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
let result: i16 = load<i16>(this.dataStart + <usize>byteOffset);
return littleEndian ? result : bswap<u16>(result);
}
getInt32(byteOffset: i32, littleEndian: bool = false): i32 {
if (
(byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
let result: i32 = load<i32>(this.dataStart + <usize>byteOffset);
return littleEndian ? result : bswap<u32>(result);
}
getUint8(byteOffset: i32): u8 {
if (<u32>byteOffset >= <u32>this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u8>(this.dataStart + <usize>byteOffset);
}
getUint16(byteOffset: i32, littleEndian: bool = false): u16 {
if (
(byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
let result: u16 = load<u16>(this.dataStart + <usize>byteOffset);
return littleEndian ? result : bswap<u16>(result);
}
getUint32(byteOffset: i32, littleEndian: bool = false): u32 {
if (
(byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
let result: u32 = load<u32>(this.dataStart + <usize>byteOffset);
return littleEndian ? result : bswap<u32>(result);
}
setFloat32(byteOffset: i32, value: f32, littleEndian: bool = false): void {
if (
(byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
if (littleEndian) store<f32>(this.dataStart + <usize>byteOffset, value);
else store<u32>(this.dataStart + <usize>byteOffset, bswap<u32>(reinterpret<u32>(value)));
}
setFloat64(byteOffset: i32, value: f64, littleEndian: bool = false): void {
if (
(byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
if (littleEndian) store<f64>(this.dataStart + <usize>byteOffset, value);
else store<u64>(this.dataStart + <usize>byteOffset, bswap<u64>(reinterpret<u64>(value)));
}
setInt8(byteOffset: i32, value: i8): void {
if (<u32>byteOffset >= <u32>this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
store<i8>(this.dataStart + <usize>byteOffset, value);
}
setInt16(byteOffset: i32, value: i16, littleEndian: bool = false): void {
if (
(byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
store<i16>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u16>(value));
}
setInt32(byteOffset: i32, value: i32, littleEndian: bool = false): void {
if (
(byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
store<i32>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u32>(value));
}
setUint8(byteOffset: i32, value: u8): void {
if (<u32>byteOffset >= <u32>this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
store<u8>(this.dataStart + <usize>byteOffset, value);
}
setUint16(byteOffset: i32, value: u16, littleEndian: bool = false): void {
if (
(byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
store<u16>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u16>(value));
}
setUint32(byteOffset: i32, value: u32, littleEndian: bool = false): void {
if (
(byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
store<u32>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u32>(value));
}
// Non-standard additions that make sense in WebAssembly, but won't work in JS:
getInt64(byteOffset: i32, littleEndian: bool = false): i64 {
if (
(byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
let result: i64 = load<i64>(this.dataStart + <usize>byteOffset);
return littleEndian ? result : bswap<u64>(result);
}
getUint64(byteOffset: i32, littleEndian: bool = false): u64 {
if (
(byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
let result = load<u64>(this.dataStart + <usize>byteOffset);
return littleEndian ? result : bswap<u64>(result);
}
setInt64(byteOffset: i32, value: i64, littleEndian: bool = false): void {
if (
(byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
store<i64>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u64>(value));
}
setUint64(byteOffset: i32, value: u64, littleEndian: bool = false): void {
if (
(byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
store<u64>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u64>(value));
}
toString(): string {
return "[object DataView]";
}
}