Repository navigation
Expand file tree
/
Copy pathworker.js
More file actions
151 lines (121 loc) · 3.84 KB
/
Copy pathworker.js
File metadata and controls
151 lines (121 loc) · 3.84 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
let glue = null;
function readCString(heapOrArray, idx, maxBytesToRead) {
var endIdx = idx + maxBytesToRead;
var endPtr = idx;
// node endIdx will be NaN if the argument is not supplied
while (heapOrArray[endPtr] && !(endPtr >= endIdx)) ++endPtr;
var str = '';
while (idx < endPtr) {
str += String.fromCharCode(heapOrArray[idx++]);
}
return str;
}
function writeCString(str, heap, outIdx) {
var startIdx = outIdx;
for (var i = 0; i < str.length; ++i) {
var u = str.charCodeAt(i);
heap[outIdx++] = u & 0xff;
}
heap[outIdx] = 0;
return outIdx - startIdx;
}
class Glue {
constructor() {
this.instance = null;
this.memoryBuffer = null;
this.heapu8 = null;
this.importObject = {
env: {
emscripten_notify_memory_growth: () => {
console.log('growing memory');
this.loadHeap();
},
log_msg: (ptr, len) => {
console.log('msg', this.readStringAt(ptr, len))
}
}
};
}
async init(module) {
const result = await WebAssembly.instantiate(module, this.importObject);
this.instance = result;
this.instance.exports._initialize();
this.exports = this.instance.exports;
this.loadHeap();
}
loadHeap() {
this.memoryBuffer = this.exports.memory.buffer;
this.heapu8 = new Uint8Array(this.memoryBuffer);
}
malloc(size) {
return this.instance.exports.malloc(size);
}
free(ptr) {
return this.instance.exports.free(ptr);
}
writeInto(ptr, src) {
// don't copy it twice if it's already an array
this.heapu8.set(new Uint8Array(src.buffer || src), ptr);
}
copyFrom(ptr, length) {
const arr = new Uint8Array(length);
arr.set(this.heapu8.subarray(ptr, ptr+length), 0);
return arr;
}
readStringAt(ptr, maxLen) {
if (!ptr) return '';
return readCString(this.heapu8, ptr, maxLen);
}
allocString(str) {
// only ascii strings!
const ptr = this.malloc(str.length+1);
writeCString(str, this.heapu8, ptr);
return ptr;
}
// for utf8 strings, encode before writing, decode after reading
}
function createMsgHandler(port, cmdHandler, otherHandler) {
port.onmessage = async (event) => {
const data = event.data;
if (data && data.cmd && data.msgId) {
const { cmd, msgId } = data;
if (cmdHandler) {
try {
const transfer = [];
const result = await cmdHandler(cmd, data, transfer);
port.postMessage({ replyFor: msgId, value: result }, transfer);
} catch (error) {
port.postMessage({ replyFor: msgId, error: error.toString() });
}
}
} else if (otherHandler) {
otherHandler(event);
}
}
}
createMsgHandler(self, async (cmd, data, transfer) => {
if (cmd == 'init') {
glue = new Glue();
await glue.init(data.module);
} else if (cmd == 'transcode') {
if (!glue) {
console.error('no glue?');
}
data = data.data; // data data data
const srcPtr = glue.malloc(data.byteLength);
glue.writeInto(srcPtr, data);
const resultPtr = glue.exports.transcode(srcPtr, data.byteLength);
glue.free(srcPtr);
if (!resultPtr) {
return null;
} else {
const result = glue.copyFrom(
glue.exports.ob_get_data(resultPtr),
glue.exports.ob_get_size(resultPtr)
).buffer;
glue.exports.ob_free(resultPtr);
transfer.push(result);
return result;
}
}
})