-
Notifications
You must be signed in to change notification settings - Fork 4
/
worker.js
231 lines (206 loc) · 6.8 KB
/
worker.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
'use strict';
var pid = 0;
var cookie = 0;
var terminated = false;
var zombieChildren = [];
const debugCommands = false;
// Bit patterns for process status
// -------------------------------------------
// **** **** .000 0000 exited, * = exit status
// 0000 0000 ?*** **** signaled, * = non-0 term sig, ? = core dump
// **** **** 0111 1111 stopped, * = non-0 stop sig
// 1111 1111 1111 1111 continued
const SIGKILL = 9;
// Terminate without sending any notifications to service
function terminateWorker() {
terminated = true;
abort();
}
function sendSyncCmd(cmd, requireOk) {
cmd.pid = pid;
cmd.cookie = cookie;
let sendThis = cmd;
while (true) {
if (terminated)
terminateWorker();
if (debugCommands)
console.log('worker', pid, ': send:', sendThis);
var req = new XMLHttpRequest();
req.open('POST', 'service', false);
req.send(JSON.stringify(sendThis));
if (req.status !== 200) {
console.log('worker', pid, ': received status:', req.status, ' for request:', cmd);
terminateWorker();
}
if (debugCommands) {
console.log('worker', pid, ': sent:', cmd);
console.log('worker', pid, ': revd:', req.responseText);
}
var results = JSON.parse(req.responseText);
for(var result of results) {
if (result.command === 'terminate') {
console.log('worker', pid, ': received terminateWorker, reason: "' + result.reason + '" for request:', cmd);
terminateWorker();
} else if (result.command === 'childDied') {
zombieChildren.push(result);
} else if (result.command === 'ok') {
return result;
} else if (result.command !== 'ping') {
console.log('worker', pid, ': received unrecognized result:', result, 'for request:', cmd);
terminateWorker();
}
}
if (!requireOk)
return;
sendThis = { command: 'wait', pid: cmd.pid, cookie: cmd.cookie };
}
}
function print(x) {
sendSyncCmd({ command: 'writeConsole', text: x + '' }, true);
}
function stdin() {
return sendSyncCmd({ command: 'readConsole' }, true).text;
}
let forked = false;
let forkedFromPid = 0;
let forkedFromCookie = 0;
function workerFork() {
let result = sendSyncCmd({ command: 'fork' }, true);
forkedFromPid = pid;
forkedFromCookie = cookie;
pid = result.childPid;
cookie = result.childCookie;
forked = true;
return pid;
}
function workerUnfork(status) {
forked = false;
pid = forkedFromPid;
cookie = forkedFromCookie;
}
// TODO: send environment
// TODO: send open file handles
function workerSpawn(file, argv) {
'use strict';
file = Pointer_stringify(file);
var args = [];
while (true) {
var arg = getValue(argv, '*');
if (arg === 0)
break;
args.push(Pointer_stringify(arg));
argv += 4;
}
let errno = sendSyncCmd({ command: 'spawn', file: file, args: args }, true).errno;
if (debugCommands)
console.log('spawn result:', errno);
if (!errno) {
if (forked) {
pid = forkedFromPid;
cookie = forkedFromCookie;
forked = false;
} else
terminated = true;
}
return errno;
}
function workerWaitpid(childPid, statusPtr, options) {
const WNOHANG = 1;
let beenThroughLoop = false;
//console.log('waitpid', childPid, statusPtr, options);
// TODO: WUNTRACED?, WCONTINUED
// TODO: childPid == 0, childPid < -1; right now it treats them like childPid == -1
// TODO: notify service to remove zombie
while (true) {
for (var i = 0; i < zombieChildren.length; ++i) {
if (childPid < 0 || zombieChildren[i].childPid == childPid) {
if (statusPtr)
setValue(statusPtr, zombieChildren[i].exitCode, 'i32');
let result = zombieChildren[i].childPid;
zombieChildren.splice(i, 1);
return result;
}
}
if (options & WNOHANG) {
if (beenThroughLoop)
return 0;
sendSyncCmd({ command: 'ping' }, false);
beenThroughLoop = true;
} else
sendSyncCmd({ command: 'wait' }, false);
}
}
var consoleInputBuffer = [];
var consoleInputBufferUsed = 0;
var consoleOps = {
open: function (stream) {
stream.tty = true;
stream.seekable = false;
},
close: function (stream) {
},
flush: function (stream) {
},
read: function (stream, buffer, offset, length, pos) {
while (consoleInputBufferUsed >= consoleInputBuffer.length) {
consoleInputBuffer = intArrayFromString(stdin() + '', true);
consoleInputBufferUsed = 0;
}
var bytesRead = Math.min(length, consoleInputBuffer.length - consoleInputBufferUsed);
for (var i = 0; i < bytesRead; ++i)
buffer[offset + i] = consoleInputBuffer[consoleInputBufferUsed + i];
consoleInputBufferUsed += bytesRead;
if (bytesRead)
stream.node.timestamp = Date.now();
return bytesRead;
},
write: function (stream, buffer, offset, length, pos) {
// TODO: is buffer always the heap? If not, is there an alternative to Pointer_stringify?
print(Pointer_stringify(offset, length));
if (length)
stream.node.timestamp = Date.now();
return length;
}
};
var Module;
var exitCode = 0;
addEventListener('message', function (e) {
if (!e.isTrusted || Module)
return;
if (debugCommands)
console.log('new worker: received:', e.data);
pid = e.data.pid;
cookie = e.data.cookie;
if (!pid) {
if (debugCommands)
console.log('worker: fork from pid 0');
var result = sendSyncCmd({ command: 'fork' }, true);
pid = result.childPid;
cookie = result.childCookie;
if (debugCommands)
console.log('worker: pid = ', pid);
}
Module = {
thisProgram: e.data.args.shift(),
arguments: e.data.args,
print: print,
printErr: print,
preInit: function () {
FS.registerDevice(FS.makedev(5, 0), consoleOps);
FS.registerDevice(FS.makedev(6, 0), consoleOps);
}
};
// TODO: this blindly assumes e.data.file loads
sendSyncCmd({ command: 'processStarted', spawnRequest: e.data.spawnRequest, errno: 0 });
try {
importScripts(e.data.file);
exitCode = EXITSTATUS << 8;
} catch (e) {
if (debugCommands)
console.log('worker', pid, ': exception:', e);
exitCode = SIGKILL;
}
if (!terminated)
sendSyncCmd({ command: 'processExited', exitCode: exitCode });
self.close();
});