-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptr.ts
269 lines (244 loc) · 5.35 KB
/
ptr.ts
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
import { LITTLE_ENDIAN } from './endian.ts';
import type {
ArrayBufferReal,
EndianBufferPointer,
MemberInfo,
MemberInfos,
Type,
TypeClass,
} from './type.ts';
import { dataView } from './util.ts';
function index(key: PropertyKey): number | null {
let i;
return key === '-0' ? NaN : (key === '' + (i = +String(key)) ? i : null);
}
const getter = Symbol('getter');
const setter = Symbol('setter');
const handler: ProxyHandler<Ptr<unknown>> = {
deleteProperty(target, key): boolean {
let i;
return Reflect.has(target, key) || (i = index(key)) === null
? Reflect.deleteProperty(target, key)
: !(i === i - i % 1);
},
get(target, key, receiver: Ptr<unknown>): unknown | undefined {
let i;
if (Reflect.has(target, key) || (i = index(key)) === null) {
return Reflect.get(target, key);
}
if ((i === i - i % 1)) {
return receiver[getter](i);
}
},
has(target, key): boolean {
let i;
return (
Reflect.has(target, key) ||
((i = index(key)) !== null && i === i - i % 1)
);
},
set(target, key, value, receiver: Ptr<unknown>): boolean {
let i;
if (Reflect.has(target, key) || (i = index(key)) === null) {
return Reflect.set(target, key, value);
}
if (i === i - i % 1) {
receiver[setter](i, value);
}
return true;
},
};
let members: WeakMap<typeof Ptr, Readonly<MemberInfos>>;
/**
* Pointer to a type.
*/
export class Ptr<T = never> implements EndianBufferPointer {
/**
* Ptr class.
*/
declare public readonly ['constructor']: PtrClass<T>;
/**
* Pointer elements.
*/
[index: number]: T;
/**
* Buffer data.
*/
readonly #buffer: ArrayBufferLike;
/**
* Byte offset into buffer.
*/
readonly #byteOffset: number;
/**
* Little endian, or big.
*/
readonly #littleEndian: boolean;
/**
* ArrayTyped constructor.
*
* @param buffer Buffer data.
* @param byteOffset Byte offset into buffer.
* @param littleEndian Host endian, little endian, big endian.
*/
constructor(
buffer: ArrayBufferReal,
byteOffset = 0,
littleEndian: boolean | null = null,
) {
dataView(this.#buffer = buffer);
if (byteOffset < -0x1fffffffffffff || byteOffset > 0x1fffffffffffff) {
throw new RangeError(`Invalid offset: ${byteOffset}`);
}
this.#byteOffset = byteOffset - byteOffset % 1 || 0;
this.#littleEndian = !!(littleEndian ?? LITTLE_ENDIAN);
return new Proxy(this, handler as ProxyHandler<Ptr<T>>);
}
/**
* @inheritdoc
*/
public get buffer(): ArrayBufferLike {
return this.#buffer;
}
/**
* @inheritdoc
*/
public get byteOffset(): number {
return this.#byteOffset;
}
/**
* @inheritdoc
*/
public get littleEndian(): boolean {
return this.#littleEndian;
}
/**
* Getter accessor.
*
* @param index Pointer index.
* @returns Pointer value.
*/
protected [getter](index: number): T {
throw new TypeError(`Read from void pointer: ${index}`);
}
/**
* Setter accessor.
*
* @param index Pointer index.
* @param value Pointer value.
*/
protected [setter](index: number, value: T): void {
void value;
throw new TypeError(`Write to void pointer: ${index}`);
}
/**
* Getter symbol.
*/
protected static readonly getter: typeof getter = getter;
/**
* Setter symbol.
*/
protected static readonly setter: typeof setter = setter;
/**
* Size of each element.
*/
public static readonly BYTES_PER_ELEMENT: number = 0;
/**
* Members infos.
*/
public static get MEMBERS(): Readonly<MemberInfos> {
let r = (members ??= new WeakMap()).get(this);
if (!r) {
const bpe = this.BYTES_PER_ELEMENT;
members.set(
this,
r = new Proxy(
Object.create(
Object.getPrototypeOf(this).MEMBERS ?? null,
) as Readonly<MemberInfos>,
{
get(target, key): Readonly<MemberInfo> | undefined {
const i = index(key);
if (i === null) {
return Reflect.get(target, key);
}
if (i === i - i % 1) {
return {
byteOffset: i * bpe,
byteLength: bpe,
};
}
},
set(target, key, value): boolean {
return index(key) === null
? Reflect.set(target, key, value)
: false;
},
},
),
);
members.set(this, r);
}
return r;
}
}
/**
* Ptr class.
*/
export interface PtrClass<T = never> extends Omit<typeof Ptr<T>, 'new'> {
/**
* Ptr prototype.
*/
readonly prototype: Ptr<T>;
}
/**
* Ptr constructor.
*/
export interface PtrConstructor<T = never> extends PtrClass<T> {
/**
* Ptr constructor.
*
* @param buffer Buffer data.
* @param byteOffset Byte offset into buffer.
* @param littleEndian Host endian, little endian, big endian.
*/
new (
buffer: ArrayBufferReal,
byteOffset?: number,
littleEndian?: boolean,
): Ptr<T>;
}
/**
* Ptr Type.
*/
export interface PtrType<T = never> extends Ptr<T>, Type {
/**
* Ptr Type constructor.
*/
readonly constructor: PtrClass<T> & TypeClass;
}
/**
* Ptr Type class.
*/
export interface PtrTypeClass<T = never> extends PtrClass<T>, TypeClass {
/**
* Ptr Type prototype.
*/
readonly prototype: PtrType<T>;
}
/**
* Ptr Type constructor.
*/
export interface PtrTypeConstructor<T = never> extends PtrTypeClass<T> {
/**
* Ptr Type constructor.
*
* @param buffer Buffer data.
* @param byteOffset Byte offset into buffer.
* @param littleEndian Host endian, little endian, big endian.
*/
new (
buffer: ArrayBufferReal,
byteOffset?: number,
littleEndian?: boolean,
): PtrType<T>;
}