forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
54 lines (45 loc) · 1.3 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
'use strict';
const { runInThisContext } = require('vm');
const { parentPort, workerData } = require('worker_threads');
const { ResourceLoader } = require(workerData.wptRunner);
const resource = new ResourceLoader(workerData.wptPath);
global.self = global;
global.GLOBAL = {
isWindow() { return false; },
isShadowRealm() { return false; }
};
global.require = require;
// This is a mock, because at the moment fetch is not implemented
// in Node.js, but some tests and harness depend on this to pull
// resources.
global.fetch = function fetch(file) {
return resource.read(workerData.testRelativePath, file, true);
};
if (workerData.initScript) {
runInThisContext(workerData.initScript);
}
runInThisContext(workerData.harness.code, {
filename: workerData.harness.filename
});
// eslint-disable-next-line no-undef
add_result_callback((result) => {
parentPort.postMessage({
type: 'result',
result: {
status: result.status,
name: result.name,
message: result.message,
stack: result.stack,
},
});
});
// eslint-disable-next-line no-undef
add_completion_callback((_, status) => {
parentPort.postMessage({
type: 'completion',
status,
});
});
for (const scriptToRun of workerData.scriptsToRun) {
runInThisContext(scriptToRun.code, { filename: scriptToRun.filename });
}