• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const { runInThisContext } = require('vm');
4const { parentPort, workerData } = require('worker_threads');
5
6const { ResourceLoader } = require(workerData.wptRunner);
7const resource = new ResourceLoader(workerData.wptPath);
8
9global.self = global;
10global.GLOBAL = {
11  isWindow() { return false; },
12  isShadowRealm() { return false; },
13};
14global.require = require;
15
16// This is a mock, because at the moment fetch is not implemented
17// in Node.js, but some tests and harness depend on this to pull
18// resources.
19global.fetch = function fetch(file) {
20  return resource.read(workerData.testRelativePath, file, true);
21};
22
23if (workerData.initScript) {
24  runInThisContext(workerData.initScript);
25}
26
27runInThisContext(workerData.harness.code, {
28  filename: workerData.harness.filename,
29});
30
31// eslint-disable-next-line no-undef
32add_result_callback((result) => {
33  parentPort.postMessage({
34    type: 'result',
35    result: {
36      status: result.status,
37      name: result.name,
38      message: result.message,
39      stack: result.stack,
40    },
41  });
42});
43
44// Keep the event loop alive
45const timeout = setTimeout(() => {
46  parentPort.postMessage({
47    type: 'completion',
48    status: { status: 2 },
49  });
50}, 2 ** 31 - 1); // Max timeout is 2^31-1, when overflown the timeout is set to 1.
51
52// eslint-disable-next-line no-undef
53add_completion_callback((_, status) => {
54  clearTimeout(timeout);
55  parentPort.postMessage({
56    type: 'completion',
57    status,
58  });
59});
60
61for (const scriptToRun of workerData.scriptsToRun) {
62  runInThisContext(scriptToRun.code, { filename: scriptToRun.filename });
63}
64