-
Notifications
You must be signed in to change notification settings - Fork 209
/
geometry-util.js
57 lines (54 loc) · 1.3 KB
/
geometry-util.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
export class Allocator {
constructor(moduleInstance) {
this.moduleInstance = moduleInstance;
this.offsets = []
}
alloc(constructor, size) {
if (size > 0) {
const offset = this.moduleInstance._malloc(
size * constructor.BYTES_PER_ELEMENT
)
const b = new constructor(
this.moduleInstance.HEAP8.buffer,
this.moduleInstance.HEAP8.byteOffset + offset,
size
)
b.offset = offset
this.offsets.push(offset)
return b
} else {
return new constructor(this.moduleInstance.HEAP8.buffer, 0, 0)
}
}
freeAll() {
for (let i = 0; i < this.offsets.length; i++) {
this.moduleInstance._doFree(this.offsets[i])
}
this.offsets.length = 0
}
}
export class ScratchStack {
constructor(moduleInstance, size) {
this.ptr = moduleInstance._malloc(size)
this.u8 = new Uint8Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size
)
this.u32 = new Uint32Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size / 4
)
this.i32 = new Int32Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size / 4
)
this.f32 = new Float32Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size / 4
)
}
}