1'use strict';
2const common = require('../common');
3const tmpdir = require('../common/tmpdir');
4const assert = require('assert');
5
6// Attempts to test that the source map JS code run on process shutdown
7// does not call any user-defined JS code.
8
9const { Worker, workerData, parentPort } = require('worker_threads');
10
11if (!workerData) {
12  tmpdir.refresh();
13  process.env.NODE_V8_COVERAGE = tmpdir.path;
14
15  // Count the number of some calls that should not be made.
16  const callCount = new Int32Array(new SharedArrayBuffer(4));
17  const w = new Worker(__filename, { workerData: { callCount } });
18  w.on('message', common.mustCall(() => w.terminate()));
19  w.on('exit', common.mustCall(() => {
20    assert.strictEqual(callCount[0], 0);
21  }));
22  return;
23}
24
25const { callCount } = workerData;
26
27function increaseCallCount() { callCount[0]++; }
28
29// Increase the call count when a forbidden method is called.
30for (const property of ['_cache', 'lineLengths', 'url']) {
31  Object.defineProperty(Object.prototype, property, {
32    get: increaseCallCount,
33    set: increaseCallCount
34  });
35}
36Object.getPrototypeOf([][Symbol.iterator]()).next = increaseCallCount;
37Object.getPrototypeOf((new Map()).entries()).next = increaseCallCount;
38Array.prototype[Symbol.iterator] = increaseCallCount;
39Map.prototype[Symbol.iterator] = increaseCallCount;
40Map.prototype.entries = increaseCallCount;
41Object.keys = increaseCallCount;
42Object.create = increaseCallCount;
43Object.hasOwnProperty = increaseCallCount;
44
45parentPort.postMessage('done');
46