summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/mozilla/tests/webgpu/common/runtime/helper/test_worker.js
blob: 1d653941802d3a45ab05094b654d8e369920d57c (plain)
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
/**
* AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts
**/import { LogMessageWithStack } from '../../internal/logging/log_message.js';


import { kDefaultCTSOptions } from './options.js';

export class TestWorker {


  resolvers = new Map();

  constructor(ctsOptions) {
    this.ctsOptions = { ...(ctsOptions || kDefaultCTSOptions), ...{ worker: true } };
    const selfPath = import.meta.url;
    const selfPathDir = selfPath.substring(0, selfPath.lastIndexOf('/'));
    const workerPath = selfPathDir + '/test_worker-worker.js';
    this.worker = new Worker(workerPath, { type: 'module' });
    this.worker.onmessage = (ev) => {
      const query = ev.data.query;
      const result = ev.data.result;
      if (result.logs) {
        for (const l of result.logs) {
          Object.setPrototypeOf(l, LogMessageWithStack.prototype);
        }
      }
      this.resolvers.get(query)(result);

      // MAINTENANCE_TODO(kainino0x): update the Logger with this result (or don't have a logger and
      // update the entire results JSON somehow at some point).
    };
  }

  async run(
  rec,
  query,
  expectations = [])
  {
    this.worker.postMessage({
      query,
      expectations,
      ctsOptions: this.ctsOptions
    });
    const workerResult = await new Promise((resolve) => {
      this.resolvers.set(query, resolve);
    });
    rec.injectResult(workerResult);
  }
}