summaryrefslogtreecommitdiff
path: root/lib/internal/v8_prof_processor.js
blob: ddfafca64c45f0ff05a3b4983936346625155a02 (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
50
51
52
53
'use strict';

const {
  ArrayPrototypePush,
  ArrayPrototypeSlice,
  StringPrototypeSlice,
} = primordials;

const Buffer = require('buffer').Buffer;
const console = require('internal/console/global');
const vm = require('vm');
const { SourceTextModule } = require('internal/vm/module');

const natives = internalBinding('natives');

async function linker(specifier, referencingModule) {
  // Transform "./file.mjs" to "file"
  const file = StringPrototypeSlice(specifier, 2, -4);
  const code = natives[`internal/deps/v8/tools/${file}`];
  return new SourceTextModule(code, { context: referencingModule.context });
}

(async () => {
  const tickArguments = [];
  if (process.platform === 'darwin') {
    ArrayPrototypePush(tickArguments, '--mac');
  } else if (process.platform === 'win32') {
    ArrayPrototypePush(tickArguments, '--windows');
  }
  ArrayPrototypePush(tickArguments,
                     ...ArrayPrototypeSlice(process.argv, 1));

  const context = vm.createContext({
    arguments: tickArguments,
    write(s) { process.stdout.write(s); },
    printErr(err) { console.error(err); },
    console,
    process,
    Buffer,
  });

  const polyfill = natives['internal/v8_prof_polyfill'];
  const script = `(function(module, require) {
    ${polyfill}
  })`;

  vm.runInContext(script, context)(module, require);

  const tickProcessor = natives['internal/deps/v8/tools/tickprocessor-driver'];
  const tickprocessorDriver = new SourceTextModule(tickProcessor, { context });
  await tickprocessorDriver.link(linker);
  await tickprocessorDriver.evaluate();
})();